c++dictionaryfirebreathjsapi

Print value of a JSAPIWeakPtr


I try to print all elements of a std::map<int, FB::JSAPIWeakPtr> apiMap; using the code:

void printMap() {
std::map<int, FB::JSAPIWeakPtr>::iterator p; 
p= apiMap.begin();

for(; p!=apiMap.end(); ++p)
    {
    std::cout << "int is: " << p->first << endl;
    std::cout << "FB::JSAPIWeakPtr is: " << p->second << endl;
    }
}

but I receive the error below:

In function ‘void printMap()’:
error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & std::cout), 
((const char*)"map is: ")) << p.std::_Rb_tree_iterator<_Tp>::operator-> [with _Tp = std::pair<const int, 
boost::weak_ptr<FB::JSAPI> >, std::_Rb_tree_iterator<_Tp>::pointer = std::pair<const int, 
boost::weak_ptr<FB::JSAPI> >*]()->std::pair<const int, boost::weak_ptr<FB::JSAPI> >::second’

Is there any way to print the value that the FB::JSAPIWeakPtr has?


Solution

  • The first thing to realize is that FB::JSAPIWeakPtr is just a typedef for boost::weak_ptr

    It kinda depends on what you are trying to actually accomplish; if you're just trying to figure out if it still has a value, I'd do something like this:

    for(; p!=apiMap.end(); ++p)
    {
        std::cout << "int is: " << p->first << endl;
        FB::JSAPIPtr cur(p->second.lock());
        if (cur) { std::cout << "FB::JSAPIWeakPtr is valid" << endl; }
        else { "FB::JSAPIWeakPtr is invalid" << endl; }
    }
    

    If you actually want the value to print out you can use cur.get() to get the actual pointer value. The main thing to remember is that a weak_ptr has no real valid, it only has a value if you can successfully lock it (which keeps it from being released while you access it).

    Another thing to note is that it is quite rare in plugin development for std::cout to be a useful thing to write to; you might want to read up on FireBreath Logging