Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualBytePtr

Posted 03 November 2012 - 02:52 PM

Seems that i solved this problem, thanks to "boost.any"

http://www.boost.org....html#id1028457
http://sandfly.net.n...art-6-boostany/

But sometimes, very rarely, you actually want to store a whole bunch of messy, unrelated types in a container without trying to ram them into some sort of class hierarchy. Parsers are a good example of this.


Exactly my case! Parser & compiler.


At parsing stage i just "push_back" my structs into custom vector:
using boost::any_cast;
typedef std::vector< boost::any > AnyVector;
AnyVector values;

Later when saving, i just cast them back, get my values and save.
  for ( std::vector< boost::any >::const_iterator p = parser->tab->values.begin();
	p != parser->tab->values.end();
	++p )
  {
   if ( p->type() == typeid(int) )
   {
	cout << "ID: " << any_cast<int>(*p) << endl;
						}
   if ( p->type() == typeid(MY_STRUCT) )
   {
						   // ....
						  fwrite(mystruct, ...);
					  }
			  }
etc etc...

Each struct is saved correctly with correct nr of them.
Just tested and it works.

#1BytePtr

Posted 03 November 2012 - 02:48 PM

Seems that i solved this problem, thanks to "boost.any"

http://www.boost.org/doc/libs/1_51_0/doc/html/any.html#id1028457
http://sandfly.net.nz/blog/2009/12/the-c-boost-libraries-part-6-boostany/

At parsing stage i just "push_back" my structs into custom vector:
using boost::any_cast;
typedef std::vector< boost::any > AnyVector;
AnyVector values;

Later when saving, i just cast them back, get my values and save.
  for ( std::vector< boost::any >::const_iterator p = parser->tab->values.begin();
    p != parser->tab->values.end();
    ++p )
  {
   if ( p->type() == typeid(int) )
   {
    cout << "ID: " << any_cast<int>(*p) << endl;
					    }
   if ( p->type() == typeid(MY_STRUCT) )
   {
						   // ....
						  fwrite(mystruct, ...);
					  }
			  }
etc etc...

Each struct is saved correctly with correct nr of them.
Just tested and it works.

PARTNERS