type similar to union

Started by
20 comments, last by Dragon_Strike 16 years, 3 months ago
Quote:Original post by Nypyren
Quote:Original post by Dragon_Strike
Quote:Original post by Nypyren
Would using inheritance work for you instead?


im sorry i forgot to mention the regular types should also be used... i could make wrappers for all the types that i need though... but it seems like a quite ugly solution.. or?

EDIT:: Im using C++


You'll have to make a separate class_function for every single type anyway, so making wrappers isn't THAT ugly.


well ill have to create a class initialization, constructor, variable and set function for every available type... thats for me 7 lines for each type... instead of only one (function)... but sure its acceptable...

but i think a solution with some kind of union type would make the code alot more readable and understandable...

EDIT:: and id also have to add one function each for adding it to the list since i have to know the type of the variable that is sent to the class to create and store the appropriate wrapper
Advertisement
Quote:Original post by Dragon_Strike
Quote:Original post by Nypyren
Quote:Original post by Dragon_Strike
Quote:Original post by Nypyren
Would using inheritance work for you instead?


im sorry i forgot to mention the regular types should also be used... i could make wrappers for all the types that i need though... but it seems like a quite ugly solution.. or?

EDIT:: Im using C++


You'll have to make a separate class_function for every single type anyway, so making wrappers isn't THAT ugly.


well ill have to create a class initialization, constructor, variable and set function for every available type... thats for me 7 lines for each type... instead of only one (function)... but sure its acceptable...

but i think a solution with some kind of union type would make the code alot more readable and understandable...

EDIT:: and id also have to add one function each for adding it to the list since i have to know the type of the variable that is sent to the class to create and store the appropriate wrapper


You could use a template to avoid writing most of that more than once. Make a templated class that derives from Base and write any ctor/dtor/set functions you need, then derive from each specialization of the templated class in order to override class_function.

As for adding one to the list, you don't need any special functions.

main(){  Wrapper<int> *foo = new Wrapper<int>(100);  Wrapper<std::string> *bar = new Wrapper<std::string>("hello, world!");  std::list<Base*> list1;  list1.push_back(foo);  list1.push_back(bar);  for (std::list<Base*>::iterator i = list1.start(); i != list1.end() ++i)  {    (*i)->class_function();  }  // Cleanup code left as an exercise to the reader.}
Here's a solution. I'm surprised boost does not offer anything to solve the problem though.
class Union{  struct UnionBase{    virtual void function_forward()=0;    //used for copy construction    virtual UnionBase *duplicate()=0;     virtual void *getValue()    virtual ~UnionBase(){};  };  template<typename T>  struct UnionChild: public UnionBase{    T m_value;        public:    UnionChild(T value):m_value(value);    void function_forward(){      function(value);    }    UnionBase *duplicate(){       new UnionChild(value);    }    void *getValue(){      return &m_value;    }  }  //this should probably be a smart pointer instead.  UnionBase *m_ptr;  public:  void call_function(){    m_ptr->function_forward();  }  template <typename T> T getValue(){    return *(T*)m_ptr->getValue()  }  template <typename T> void setValue(T val){    delete m_ptr;    m_ptr=new UnionChild<T>(val);  }  }
Quote:Original post by Dragon_Strike
Quote:Original post by King Mir
Is the type X.U compile time constant?


im unsure what you mean...

Is the type of value a "Union" holds determinable at compile time? or can it depend on runtime input?

Cause if it is, it is a simpler problem.
Quote:Original post by King Mir
Quote:Original post by Dragon_Strike
Quote:Original post by King Mir
Is the type X.U compile time constant?


im unsure what you mean...

Is the type of value a "Union" holds determinable at compile time?

Cause if it is, it is a simpler problem.


well what types of values it will hold can be determined... however which type that will actually be used cant be decided until the union is acutally set... ?

i dont rly get where u going... what are u thinking of?
Quote:
You could use a template to avoid writing most of that more than once. Make a templated class that derives from Base and write any ctor/dtor/set functions you need, then derive from each specialization of the templated class in order to override class_function.

As for adding one to the list, you don't need any special functions.

main(){  Wrapper<int> *foo = new Wrapper<int>(100);  Wrapper<std::string> *bar = new Wrapper<std::string>("hello, world!");  std::list<Base*> list1;  list1.push_back(foo);  list1.push_back(bar);  for (std::list<Base*>::iterator i = list1.start(); i != list1.end() ++i)  {    (*i)->class_function();  }  // Cleanup code left as an exercise to the reader.}


thx.. that solved it for me...

	class Param	{	public:		Param() : handle(0) {}		Param(D3DXHANDLE _handle): handle(_handle){}		D3DXHANDLE handle;		virtual HRESULT SetParam(CEffect* effect) = 0;	};	template <class T>	class ParamType : public Param	{	public:		ParamType() : Param(0), value(0){}		ParamType(D3DXHANDLE _handle, T _value) : Param(_handle), value(_value){}		T value;				HRESULT SetParam(CEffect* effect) { return effect->SetParam(handle, value);}	};
Quote:Original post by Dragon_Strike
i have a class which should store alot of objects/variables with different types and have "Set" functions for each type...
Some more context would be useful. Are these variables in a scripting language? Objects in a game? UI components? What is the high-level problem you are trying to solve? From what you have said, it sounds like boost::variant is a suitable candidate, but without more information I can't properly fit it to your problem (if it is a suitable solution).

Σnigma
Quote:Original post by King Mir
Here's a solution. I'm surprised boost does not offer anything to solve the problem though.
*** Source Snippet Removed ***


nice! that can be useful...
Quote:Original post by Enigma
Quote:Original post by Dragon_Strike
i have a class which should store alot of objects/variables with different types and have "Set" functions for each type...
Some more context would be useful. Are these variables in a scripting language? Objects in a game? UI components? What is the high-level problem you are trying to solve? From what you have said, it sounds like boost::variant is a suitable candidate, but without more information I can't properly fit it to your problem (if it is a suitable solution).

Σnigma


im very interested in how boost::variant could solve a problem like this...

they are simple int,float etc.. variables and DirectX "objects" or what you would call them...

what information is it that you need?
Quote:Original post by Dragon_Strike
Quote:Original post by King Mir
Quote:Original post by Dragon_Strike
Quote:Original post by King Mir
Is the type X.U compile time constant?


im unsure what you mean...

Is the type of value a "Union" holds determinable at compile time?

Cause if it is, it is a simpler problem.


well what types of values it will hold can be determined... however which type that will actually be used cant be decided until the union is acutally set... ?

i dont rly get where u going... what are u thinking of?

So no then.

If it were, you could use some kind of template container that holds a list of unrelated types. Then you'd use recursion to apply the function to every type. Or there may be some other solution. But it would change the problem dramatically.

This topic is closed to new replies.

Advertisement