From: Christian P. <cp...@se...> - 2005-01-20 18:58:40
|
Am Donnerstag 20 Januar 2005 04:41 schrieb Marc Duerner: > 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. I also had this idea. > 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>(); this rocks! > 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? No - please merge them. Maybe we should also move Any to the P namespace. > Marc Greetings, Christian |