How to serialize an object that keeps pointers to other objects in C++?
I'm looking for a way to serialize a large complex object in C++. I've
been thinking of using boost serialization api, but I'm not sure if it's
even possible to serialize an object implemented in such a way.
In my program I have the following objects:
typedef map<float, float> SignalData;
typedef pair<float,float> TimeValuePair;
class SignalDatabaseNG : public SignalDatabase
{
(...)
private:
vector<SingleSignal *> all_signals;
(...)
};
class SingleSignalAsStdMap: public SingleSignal{
private:
SignalData * signalData;
(...)
};
class IntegrationComparator : public Comparator {
private:
map<SignalData *, float> * preComputedIntegrals;
(...)
public:
IntegrationComparator();
float compare(SignalData *a, SignalData *b);
void preComputeIntegralsForAll(SignalDatabase * database);
(...)
};
SignalDatabase is a class whose most important role is to keep all the
data needed for the program. The signal is represented as a map of two
floats (time and value), and all the signals are kept inside a vector of
such maps.
It's being filled with data in the following way:
SignalDatabaseNG * signalDatabase = TestConfiguration::getSignalDatabase();
IntegrationComparator * integrationComparator = new IntegrationComparator();
integrationComparator->preComputeIntegralsForAll(signalDatabase);
TestConfiguration::getSignalDatabase() returns a database object that
contains the data needed for the computation of integrals (its read from
text files at the beginning of the program runtime). Then,
integrationComparator object is created and
preComputeIntegralsForAll(SignalDatabase * db) is called, which performs
the computation. In the end, map<SignalData *, float> *
preComputedIntegrals inside integrationComparator is filled with data.
This computation of integrals takes a huge amount of time (it's about 60
seconds for 10 signals, and I need to have it computed for ~220000
signals).
I'd like to be able to run it once, then serialize it, and then reuse it
with each program run (the data doesn't change often, so it would be a
huge time saver).
The problem is that the map maps the pointer to a signal object to the
integral value. But, in the next run, the database would be created from
scratch and all the addreses inside pointers would change. It's possible
to serialize both signal database and precomputed integrals database, but
then there's also no guarantee that after de-serialization signals in the
database would be at the same places in the memory, so the pointers inside
integralsComparator would also be completely wrong.
Has anybody got any idea how such serialization could be done (preferably,
without having to rewrite the whole structure of classes)?
No comments:
Post a Comment