C++ Design struct with content preferences specified at creation time -


i create data structure capture time series of production, sales, , inventory data. however, cases, don't need track data. exact data track (for example sales , inventory not production) specified @ time series constructed/initiated.

one approach following.

struct productiondataentry { ... }; struct salesdataentry { ... }; struct inventorydataentry { ... }; // each of above struct arbitrarily large  struct dataentryv1 {   productiondataentry pde_;   salesdataentry      sde_;   inventorydataentry  ide_; };  typedef std::chrono::system_clock::time_point timepoint;  struct timeseriesentry {   timepoint timepoint_;   dataentry entry_; };  std::deque<timeseriesentry> time_series; 

the drawback of above approach following. in use case, sales , inventory data necessary, not production, data structure still consume space productiondataentry.

i looking approach can avoid such space wastage.

two options come mind:

  1. create separate time series each kind of data , populate time series necessary. however, copies timepoint data multiple times , spoils locality of data spreading collected data on multiple data structures.

  2. organize dataentry pointers individual data entries, like

    struct dataentryv2 {   productiondataentry * pde_{nullptr};   salesdataentry      * sde_{nullptr};   inventorydataentry  * ide_{nullptr}; }; 

    and construct data entry objects necessary. however, fragments memory , introduces additional overhead of allocation , deallocation avoid if possible.

  3. organize dataentry std::optional, like

    struct dataentryv3 {   std::optional<productiondataentry> pde_;   std::optional<salesdataentry>      sde_;   std::optional<inventorydataentry>  ide_; }; 

    i think requires 1 word per entry type. , still consume space of unnecessary data.

i aware, there other options in design space?

(note: dataentry may need extended include new kind of data, e.g. preorderdata.)

create separate time series each kind of data , populate time series necessary. however, copies timepoint data multiple times , spoils locality of data spreading collected data on multiple data structures.

it's possible acomplish through inheritance. example:

struct dataentryv1: public productiondataentry, public salesdataentry {}; 

you still need define each data type use, wouldn't "spoil locality of data". bonus, @ reuse of code gain via polymorphism.

i present option , should read this link multiple inheritance before decide this.


Comments