I have a generic data type that is passed by value but does not support type information. We only store pointers and basic data types (e.g. int, float, etc.). Now for the first time we need to save std :: string inside this. Therefore, we decided to convert it to std :: string * and save it. Then the problem of destruction arises. We do not like to copy std :: string every time. Therefore, I am thinking of such an approach. Let's say the data type is as follows:
class Atom
{
public :
enum flags
{
IS_STRING,
IS_EMPTY,
HAS_GOT_COPIED,
MARKER
};
private:
void* m_value;
std::bitset<MARKER> m_flags;
public:
.....
Atom( Atom& atm )
{
atm.m_flags.set( HAS_GOT_COPIED );
.....
}
.....
~Atom()
{
if( m_flags.test(IS_STRING) && !m_flags.test(HAS_GOT_COPIED) )
{
std::string* val = static_cast<std::string*>(m_value);
delete val;
}
}
};
Is this a good approach to find out if there is a link to std :: string *? Any comments ..
I looked boost :: any and poco :: DynamicAny. Since I need serialization, I cannot use them.
Thanks, Gokul.
Gokul source
share