dimanche 26 juillet 2015

How to get integer out of Poco::Any

I use Poco::Any with various integer types, e.g.:

Poco::Any x = 15;
Poco::Any y = (size_t)15;
Poco::Any z = 15L;

Later on I want to work with an integer value:

void func(Poco::Any &a)
{

How can I extract any integer out of this? One way would be:

long n;
if ( auto *p = Poco::AnyCast<int>(&a) )
    n = *p;
else if ( auto *p = Poco::AnyCast<long>(&a) )
    n = *p;

and repeat for every integral type. But this is ugly. Is there a better way?

I have tried:

Poco::Dynamic::Var var(a);
auto n = var.convert<int>(var);

however this throws an exception which comes from VarHolder.h:

virtual void convert(Int32& val) const;
    /// Throws BadCastException. Must be overriden in a type
    /// specialization in order to suport the conversion.

Aucun commentaire:

Enregistrer un commentaire