[OT] class design question

B

Bart Simpson

I have the need to write a generic Argument class for passing arguments
to an engine. I have come up in the last 5 mins or so with something
like this:

class Argument
{
public:
enum Type { String = 0, RawPointer, Double, Long};

Argument():ptr(0),len(0){}
virtual ~Argument() { if (ptr) delete ptr ; }

//Accessor/Modifiers here ...

private:
union { void * ptr ; // if string, must be null terminated
double ; long ; } data ;

std::string m_name ;
size_t m_len ;
Type m_type ;
};


Questions
==================
1. Am I reinventinting the wheel here - has someone (probably more
experienced) done this already ?

2. Die hard C background showing with the presence of void* - I would
like to make this a template class instead - but cant figure out how ...
 
G

Gennaro Prota

I have the need to write a generic Argument class for passing arguments
to an engine. I have come up in the last 5 mins or so with something
like this:

class Argument
{
public:
enum Type { String = 0, RawPointer, Double, Long};

Argument():ptr(0),len(0){}
virtual ~Argument() { if (ptr) delete ptr ; }

//Accessor/Modifiers here ...

private:
union { void * ptr ; // if string, must be null terminated
double ; long ; } data ;

std::string m_name ;
size_t m_len ;
Type m_type ;
};


Questions
==================
1. Am I reinventinting the wheel here - has someone (probably more
experienced) done this already ?

2. Die hard C background showing with the presence of void* - I would
like to make this a template class instead - but cant figure out how ...

Looks like you are after a discriminated union (aka tagged union).
Boost.Any (http://www.boost.org/libs/any/) should fit your needs; I'd
also recommend googling... there are interesting articles on these
kinds of things, at least one of which by Andrei Alexandrescu. BTW, I
don't think your question is off-topic :)
 
T

Thomas J. Gritzan

Bart said:
I have the need to write a generic Argument class for passing arguments
to an engine. I have come up in the last 5 mins or so with something
like this:

class Argument
{
public:
enum Type { String = 0, RawPointer, Double, Long}; [...]
Questions
==================
1. Am I reinventinting the wheel here - has someone (probably more
experienced) done this already ?

Try boost::variant (or boost::any) if it works for you, i.e.:

struct
{
std::string name;
virtual ~Argument() { if (ptr) delete ptr ; }

private:
union { void * ptr ; // if string, must be null terminated
double ; long ; } data ;

You have to check if ptr is valid in the destructor. When you wrote to a
member in a union, all other members contain garbage (=should not be accessed).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top