// $Id$ // // Copyright (C) 2003-2008 Greg Landrum and Rational Discovery LLC // // @@ All Rights Reserved @@ // This file is part of the RDKit. // The contents are covered by the terms of the BSD license // which is included in the file license.txt, found at the root // of the RDKit source tree. // #include "Dict.h" #include #include #include #include #include namespace RDKit{ void Dict::getVal(const std::string &what, std::string &res) const { // // We're going to try and be somewhat crafty about this getVal stuff to make these // containers a little bit more generic. The normal behavior here is that the // value being queried must be directly castable to type T. We'll robustify a // little bit by trying that and, if the cast fails, attempting a couple of // other casts, which will then be lexically cast to type T. // if(!hasVal(what) ) throw KeyErrorException(what); const boost::any &val = _data.find(what)->second; try{ res = boost::any_cast(val); } catch (const boost::bad_any_cast &) { if(val.type()==typeid(int)){ res = boost::lexical_cast(boost::any_cast(val)); } else if(val.type()==typeid(unsigned int)){ res = boost::lexical_cast(boost::any_cast(val)); } else if(val.type()==typeid(long)){ res = boost::lexical_cast(boost::any_cast(val)); } else if(val.type()==typeid(unsigned long)){ res = boost::lexical_cast(boost::any_cast(val)); } else if(val.type()==typeid(float)){ res = boost::lexical_cast(boost::any_cast(val)); } else if(val.type()==typeid(double)){ res = boost::lexical_cast(boost::any_cast(val)); } else if(val.type()==typeid(const char *)){ res = std::string(boost::any_cast(val)); } else { throw; } } }; template T Dict::fromany(const boost::any &arg) const { return boost::any_cast(arg); }; template boost::any Dict::toany(T arg) const { return boost::any(arg); }; #define ANY_FORCE(T) {tD.fromany< T >(boost::any(1));tD.toany< T >( T() );} void force_types(){ Dict tD; bool fooBool = tD.fromany(boost::any(1)); tD.toany(false); int fooInt = tD.fromany(boost::any(1)); fooInt += 1; tD.toany(1); unsigned int fooUnsigned = tD.fromany(boost::any(1)); fooUnsigned += 1; tD.toany(1); double fooDouble = tD.fromany(boost::any(1)); tD.toany(1.0); std::string fooString = tD.fromany(boost::any(std::string("1"))); tD.toany(std::string("1")); ANY_FORCE(std::vector); ANY_FORCE(std::vector); ANY_FORCE(std::vector); ANY_FORCE(std::vector); ANY_FORCE(std::vector); ANY_FORCE(std::vector< std::vector >); ANY_FORCE(std::vector< std::vector >); ANY_FORCE(boost::shared_array); ANY_FORCE(boost::shared_array); ANY_FORCE(std::list); // FIX: it's UGLY that we have to include things like this: //ANY_FORCE( boost::tuples::tuple ); tD.fromany< boost::tuples::tuple >(boost::any(1)); tD.toany< boost::tuples::tuple >(boost::tuples::tuple()); tD.fromany< boost::tuples::tuple >(boost::any(1)); tD.toany< boost::tuples::tuple >(boost::tuples::tuple()); } }