Note that there are some explanatory texts on larger screens.

plurals
  1. POFIFO type of container - Which STL Container is most suitable and why?
    primarykey
    data
    text
    <p>I was recently tasked on implementing a buffer which will be used as a temporary storage by a logging class. The logging class itself is a singleton and the observer listener pattern is used. You can expect thousands of messages to be logged with this class. </p> <p>Now the problem lies here : </p> <p>We have a trace logging option which is used for deguging purposes. When this option is on, the count of messages/second is increased exponentially. In the release code trace logging is disabled, however a buffer which can store a fixed number of messages e.g. 10000 is dumped into the log <strong>IF</strong> an error occurs, so that the developers can identify the root of the problem. </p> <p>If the buffer is full the oldest message is removed to free space for the newest message. </p> <pre><code>void Log::storeToBuffer(const LogType_E &amp; type_in, const LogDomain_E &amp; domain_in,const int &amp; id_in, const char * msg_in) { if(this-&gt;myEnableTraceBuffer) { if(static_cast&lt;std::list&lt;Message&gt; * &gt;(this-&gt;myRingBuffer)-&gt;size() &lt; this-&gt;myRingBufferMaxSize) { static_cast&lt;std::list&lt;Message&gt; * &gt;(this-&gt;myRingBuffer)-&gt;push_back(Message(type_in, domain_in, id_in, msg_in)); } else { //buffer full so remove oldest element and add new if(static_cast&lt;std::list&lt;Message&gt; * &gt;(this-&gt;myRingBuffer)-&gt;size() &gt; 0) static_cast&lt;std::list&lt;Message&gt; * &gt;(this-&gt;myRingBuffer)-&gt;pop_front(); static_cast&lt;std::list&lt;Message&gt; * &gt;(this-&gt;myRingBuffer)-&gt;push_back(Message(type_in, domain_in, id_in, msg_in)); } } } </code></pre> <p>I implemented this with std::list , very simply using push_back/pop_front to exploit the constant delete/insert execution time. (<em>don't ask for the void casting, not my decision</em>).</p> <p>But since the buffer size is fixed, and not likely to be changed during the lifetime of an object, maybe vector with explicit index manipulation is more suitable? For example there can be two indeces , start/current starting both at position 0. When the vector is full and we add something start moves to position 1 and and current to position 0, thus when printing the result we have the right order. </p> <p>Maybe another STL container is more suitable for this kind of thing?</p> <p>Thank your for your patience to read this long wall of text. I am here to answer any questions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload