Unable to iterate std::map of Poco::Any
Asked Answered
L

1

5

I have a std::map of Poco::Any which I'm trying to iterate and output to a stream but i'm getting a compiler error. My code is below:

map<string, Poco::Any>::const_iterator it;
map<string, Poco::Any>::const_iterator end = _map.end();
map<string, Poco::Any>::const_iterator begin = _map.begin();
for(it = begin; it != end; ++it) {
    const std::type_info &type = it->second.type();

    // compile error here:
    os << "  " << it->first << " : " << Poco::RefAnyCast<type>(it->second) << endl;
}

2 errors on that line:

'type' cannot appear in a constant-expression
no matching function for call to 'RefAnyCast(Poco::Any&)'

UPDATE:

I understand that templates are compile time whereas type() is runtime so won't work. Thanks for underlining that. Also DynamicAny won't work because it only accepts types which have DynamicAnyHolder implementations, not ideal. The only rule i'd like to impose on the types is that they have << overloaded.

Below is what i'm currently doing, works to a degree, but only dumps known types, which is not what I'm after.

string toJson() const {
    ostringstream os;
    os << endl << "{" << endl;
    map<string, Poco::Any>::const_iterator end = _map.end();
    map<string, Poco::Any>::const_iterator begin = _map.begin();
    for(map<string, Poco::Any>::const_iterator it = begin; it != end; ++it) {
        const std::type_info &type = it->second.type();
        os << "  " << it->first << " : ";

        // ugly, is there a better way?
        if(type == typeid(int)) os << Poco::RefAnyCast<int>(it->second);
        else if(type == typeid(float)) os << Poco::RefAnyCast<float>(it->second);
        else if(type == typeid(char)) os << Poco::RefAnyCast<char>(it->second);
        else if(type == typeid(string)) os << Poco::RefAnyCast<string>(it->second);
        else if(type == typeid(ofPoint)) os << Poco::RefAnyCast<ofPoint>(it->second);
        else if(type == typeid(ofVec2f)) os << Poco::RefAnyCast<ofVec2f>(it->second);
        else if(type == typeid(ofVec3f)) os << Poco::RefAnyCast<ofVec3f>(it->second);
        //else if(type == typeid(ofDictionary)) os << Poco::RefAnyCast<ofDictionary>(it->second);
        else os << "unknown type";

        os << endl;
    }
    os<< "}" << endl;
    return os.str();
}
Leshalesher answered 6/2, 2011 at 10:54 Comment(0)
W
7

Runtime type information cannot be used to instantiate templates. An instance of type_info whose value will only be known when you run the program, doesn't magically turn into a type like int, std::string or struct FooBar when the compiler is compiling this code.

I don't know Poco library, but perhaps you could use their other Any type, DynamicAny (see documentation) which would hopefully allow you to convert the stored value to std::string for outputting:

os << "  " << it->first << " : " << it->second.convert<std::string>() << endl;
Wifehood answered 6/2, 2011 at 11:28 Comment(3)
+1. Perhaps it should be stressed that C++ templates are a compile-time thing and that template parameters have to be known at compile-time. Seeing that the OP could not figure out what was wrong, this might be news to the OP.Shiva
"Perhaps it should be stressed that C++ templates are a compile-time thing and that template parameters have to be known at compile-time". Yes this clear it up perfectly thanks. Unfortunately DynamicAny doesn't really support Any type, only those for which there is a DynamicAnyHolder implementation, so won't support user types (e.g. vectors, matrices etc.) which do have the << operator overloaded, but I would like to avoid making everything extend DynamicAnyHolderImpl if possible.Leshalesher
@Memo: Unfortunately only the concrete valueholding class (in boost holder<T>) under the hood would know how to output the held value. It seems there might not be a way to make that one accept visitors, so the only option might be to add a method to it that outputs the value, or at least a to_string method. codepad.org/NICm5r2r is just boost::any modified to have a to_string method (stringifier<T> is there to add a costumization possibility, but the default implementation with boost::lexical_cast should be fine for all "cout-able" objects except const char*.Wifehood

© 2022 - 2024 — McMap. All rights reserved.