0

I am working on a software stack where it is composed of three different entities:

  1. abc_data - holding the data as "plain" data structure
  2. abc_instance - acting as an API
  3. abc_repo - for internal conversions from abc_data to abc_instance. It includes methods using logic for an easy access and queries of the data (e.g., get_a() or get_b())

When creating a new abc_instance from abc_data, the abc_repo converts the abc_data to a different internal representation for fast access (e.g., vectors, maps) without holding the original abc_data representation anymore. However, this way it is not possible to go back to abc_data from a abc_instance.

What would be the best and elegant way to retrieve the abc_data back from a formerly created abc_instance, ideally without any further conversion.

NOTE: Please understand that I don't want to use de/serialization here. Also keeping a copy of the abc_data is not an option.

Any pointers and help would be appreciated. Thanks in advance!

soupso
  • 592
  • 1
  • 4
  • 20
  • Does your implementation allow keeping a private const reference to the `abc_data`? – gabrielbjg May 27 '20 at 11:38
  • Yes that could be an option, but how about the lifetime what it points to? I mean will abc_data persist until the end of life time of the private const reference variable ? – soupso May 27 '20 at 12:23
  • That's what I was trying to figure out by asking if your implementation allows it. You provided no information regarding the way you allocate `abc_data`. One simple way is to keep a `shared_ptr `and create `abc_data` using `make_shared`. By doing so, you could have a `get` method for the private `shared_ptr` and the lifetime of your variable will be managed by the `shared_ptr`. – gabrielbjg May 27 '20 at 12:26
  • Thanks @gabrielbjg I think shared_ptr could be the option. – soupso May 27 '20 at 12:42
  • You are welcome! Also, if you are not in a multi-threaded application you could also consider the use of `unique_ptr` and your `get` method returns an observation pointer as explained [here](https://stackoverflow.com/questions/15648844/using-smart-pointers-for-class-members) – gabrielbjg May 27 '20 at 12:47

0 Answers0