C++ How to deep copy a struct with unknown datatype?
- by Ewald Peters
hi, i have a "data provider" which stores its output in a struct
of a certain type, for instance
struct DATA_TYPE1{
std::string data_string;
};
then this struct has to be casted into a general datatype,
i thought about void * or char *, because the "intermediate"
object that copies and stores it in its binary tree should
be able to store many different types of such struct data.
struct BINARY_TREE_ENTRY{
void * DATA;
struct BINARY_TREE_ENTRY * next;
};
this void * is then later taken by another object
that casts the void * back into the (struct DATA_TYPE1 *)
to get the original data. so the sender and the receiver
know about the datatype DATA_TYPE1 but not the copying
object inbetween.
but how can the intermidiate object deep copy the contents of
the different structs, when it doesn't know the datatype,
only void * and it has no method to copy the real contents;
dynamic_cast doesn't work for void *;
the "intermediate" object should do something like:
void store_data(void * CASTED_DATA_STRUCT){
void * DATA_COPY = create_a_deepcopy_of(CASTED_DATA_STRUCT);
push_into_bintree(DATA_COPY);
}
a simple solution would be that the sending object doesn't
delete the sent data struct, til the receiving object got it,
but the sending objects are dynamically created and deleted,
before the receiver got the data from the intermediate object,
for asynchronous communication, therefore i want to copy
it.
instead of converting it to void * i also tried converting
to a superclass pointer of which the intermediate copying
object knows about, and which is inherited by all the different
datatypes of the structs:
struct DATA_BASE_OBJECT{
public:
DATA_BASE_OBJECT(){}
DATA_BASE_OBJECT(DATA_BASE_OBJECT * old_ptr){
std::cout << "this should be automatically overridden!" << std::endl;
}
virtual ~DATA_BASE_OBJECT(){}
};
struct DATA_TYPE1 : public DATA_BASE_OBJECT {
public:
string str;
DATA_TYPE1(){}
~DATA_TYPE1(){}
DATA_TYPE1(DATA_TYPE1 * old_ptr){
str = old_ptr->str;
}
};
and the corresponding binary tree entry would then be:
struct BINARY_TREE_ENTRY{
struct DATA_BASE_OBJECT * DATA;
struct BINARY_TREE_ENTRY * next;
};
and to then copy the unknown datatype, i tried in the class
that just gets the unknown datatype as a struct DATA_BASE_OBJECT *
(before it was the void *):
void * copy_data(DATA_BASE_OBJECT * data_that_i_get_in_the_sub_struct){
struct DATA_BASE_OBJECT * copy_sub = new DATA_BASE_OBJECT(data_that_i_get_in_the_sub_struct);
push_into_bintree(copy_sub);
}
i then added a copy constructor to the DATA_BASE_OBJECT, but if
the struct DATA_TYPE1 is first casted to a DATA_BASE_OBJECT and then
copied, the included sub object DATA_TYPE1 is not also copied.
i then thought what about finding out the size of the actual object
to copy and then just memcopy it, but the bytes are not stored in
one row and how do i find out the real size in memory of the
struct DATA_TYPE1 which holds a std::string?
Which other c++ methods are available to deepcopy an unknown datatype
(and to maybe get the datatype information somehow else during
runtime)
thanks Ewald