Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a nice way to create "split object" in C++?
    primarykey
    data
    text
    <p>First of all let me say that I'm creating software for microcontrollers, so RAM usage matters and it makes sense to put large blocks of const data to non-volatile (flash) memory.</p> <p>What I'm trying to achieve is to find a nice way to create a "split" object in C++. As an example let's assume that there's one byte of data (read/write) and a multibyte "receipt" for accessing it. Let's say that the "receipt" is a long string that is a filename, and the media that it points to is slow, so it makes sense to buffer the single byte in memory, instead of actually reading it on each request.</p> <pre><code>class Data { uint8_t byte; bool valid; const char filename[128]; uint8_t read() { if (!valid) performReallySlowRead(filename, &amp;byte); valid = true; return byte; }; void write(uint8_t new_value) { byte = new_value; performRealWriteToMedia(filename, byte); valid = true; }; } </code></pre> <p>The obvious problem with this approach is that whole 130-bytes end up in RAM, while only two of them need to be changed. So I've come up with an idea of split-object:</p> <pre><code>class DataNonConst { uint8_t byte; bool valid; } class DataConst { const char filename[128]; DataNonConst * const data; } static DataNonConst storage; const DataConst holder("some_long_name_here", &amp;storage); </code></pre> <p>Now the only problem is that if I would like to have a few hundred of such split-objects the process of creating them (so creating TWO objects and linking second to first) gets pretty boring and problematic...</p> <p>So the question is - is there some nice way to make it easier to use, preferably a clever-C++-trick or maybe some template magic? That is - how to create TWO objects, linked together, with a single statement, preferably one object is hidden? I don't think macro-solution is possible here, as there's no easy way to automate creation of the name for storage object... The objects need to be of same type, as I need to embed pointers to such objects in other places (one function deals with writing them, other only cares about reading)... All the solutions that I've thought of either require use of virtual interface to templates (so you make the object bigger by vtable pointer AND probably get a bonus template-bloat) or result in huge template bloat...</p> <p><strong>EDIT:</strong></p> <p>Actually part of the whole problem can be reduced to a simplier question - is there a way to "bind" an anonymous variable to a member field in C++? Sth like:</p> <pre><code>const ConstData holder("...", NonConstData()); // this object HAS TO be ROMable </code></pre> <p>In the above "wishful thinking" holder is const object in ROM and it has a pointer/reference/whatever to an anonymous object NonConstData created "somewhere" in RAM. Or:</p> <pre><code>std:pair&lt;const ConstData &amp;, NonConstData &amp;&gt; holder(ConstData(), NonConstData()); </code></pre> <p>Anything that would allow me NOT to manually create both objects and bind one to another.</p>
    singulars
    1. This table or related slice is empty.
    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