Boost logo

Boost :

From: Alexander Nasonov (alnsn-nospam_at_[hidden])
Date: 2002-11-25 11:54:31


I'm currently working on boost.dynamic_any library which covers this issues.
It's almost done. I need to implement some details and write a
documentation. First documentation draft is here:
http://cpp-experiment.sourceforge.net/boost/libs/dynamic_any/doc/

Library can be downloaded from
http://prdownloads.sourceforge.net/cpp-experiment/dynamic_any-1.0.1.tar.gz?download

For your convinience, I put an example (most similar to comparable_any) in
the body of my post.

-- 
Alexander Nasonov
Remove -nospam from my e-mail address for timely response
// File $BOOST_ROOT/libs/dynamic_any/example/print-set.cpp
#include "boost/dynamic_any/operations.hpp"
#include "boost/dynamic_any/function.hpp"
#include "boost/dynamic_any/any.hpp"
#include <boost/mpl/list.hpp>
#include <set>
#include <exception>
#include <iostream>
#include <string>
typedef boost::mpl::list<
  boost::dynamic_any::less,
  boost::dynamic_any::to_ostream<char>
> functions;
typedef boost::dynamic_any::any<functions> any;
typedef std::set<any> any_set;
const char * get_type_name(const std::type_info & info);
int main()
{
  try
  {
    any_set all;
    any a;
    all.insert(a);
    all.insert(any());
    int i = 1;
    all.insert(any(i));
    all.insert(any(&i));
    int j = 2;
    all.insert(any(j));
    all.insert(any(&j));
    const double pi = 3.1415926;
    all.insert(any(pi));
    all.insert(any(pi / 2));
    all.insert(any(-pi));
    all.insert(any(&pi));
    const void * ptr = 0;
    all.insert(any(ptr));
    all.insert(any(&ptr));
    ptr = &i;
    all.insert(any(ptr));
    all.insert(any(&ptr));
    ptr = &j;
    all.insert(any(ptr));
    all.insert(any(&ptr));
    const any b(4);
    all.insert(b);
    all.insert(b);
    a = std::string("a");
    all.insert(a);
    a = std::string("b");
    all.insert(a);
    // Show 'all'
    std::cout << "type / value\n-------------------\n";
    for(any_set::const_iterator it = all.begin(); it != all.end(); ++it)
    {
      std::cout << get_type_name(it->type()) << " / ";
      std::cout << *it << '\n';
    }
    std::cout << std::flush;
  }
  catch(const std::exception & ex)
  {
    std::cerr << ex.what() << std::endl;
  }
  catch(const char * what)
  {
    std::cerr << what << std::endl;
  }
}
const char * get_type_name(const std::type_info & info)
{
  if(info == typeid(void))
    return "void";
  else if(info == typeid(const void *))
    return "const void *";
  else if(info == typeid(const void **))
    return "const void **";
  else if(info == typeid(int))
    return "int";
  else if(info == typeid(int *))
    return "int *";
  else if(info == typeid(double))
    return "double";
  else if(info == typeid(const double *))
    return "const double *";
  else if(info == typeid(std::string))
    return "std::string";
  else
    return info.name();
}

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