Boost logo

Boost :

From: Alexander Nasonov (alnsn-boost_at_[hidden])
Date: 2002-04-08 02:22:08


I implemented the dynany class. I haven't the documentation yet but I can explain its
purpose in few words.
This class is like boost::any. In addition to boost::any functionality it provides
dynamic cast to types specified in its first template parameter. Example:

struct X {};
struct Y : X {};

// dynanyXY can hold any ValueType and cast dynamically to X and Y:
typedef dynany<TYPELIST_2(X, Y)> dynanyXY;

int main()
{
  try
  {
    dynanyXY a = 137;
    int i = dynany_cast<int>(a);
    assert(i == 137);
    a = Y();
    const X & x = dynany_cast<const X &>(a); // cast Y to X
    Y * py = dynany_cast<Y *>(&a);
    assert(py == &x);
  }
  catch(const bad_dynany_cast &)
  {
    assert(false);
  }
}

The dynany class has also Visitor template parameter. You can use accept dynany
member if you specified this parameter:

struct Visitor
{
  void visit(int);
  void visit(char);
};

typedef dynany<Loki::NullType, Visitor> visited_dynany;

int main()
{
  Visitor v;
  visited_dynany a = 137;
  a.accept(v); // v.visit(137) is called
  a = '0';
  a.accept(v); // v.visit('0') is called
}

Source is here:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/cpp-experiment/cpp-experime
nt/include/cppexp/dynany.hpp?rev=1.1
To compile you need Loki library and of course boot library.
Any interest?
-----------------------
Alexander Nasonov


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk