From: Marc D. <bdu...@gm...> - 2005-01-20 03:39:23
|
I just had the idea that one could unify P::Util::Any and P::Util::Variant. They do basically both the same, but Any uses typeid and casting, while Variant (the class formerly known as LexT) uses stream based conversion. This would simply require to add a convertTo member function to Any that allows stream based conversion additionally to the any_casts: template <typename ValueType> ValueType convertTo(const ValueType& defVal = ValueType()) const { Any::holder<ValueType>* valHolder = 0; valHolder = dynamic_cast< Any::holder<ValueType>* >(m_data); if(valHolder) { return valHolder->value(); } ValueType retVal(defVal); std::stringstream ss; m_data->output(ss); ss >> retVal; return retVal; } The dynamic_cast is a short cut. In code you would do this: Any myAny(5); string s = myAny.convertTo<std::string>(); or we add a type conversion member to Any that does this automatically: template <typename T> operator T() const throw() { return this->template convertTo<T>(); } Does anybody see a problem with this? Marc |