8

I am using a container to hold a list of pointers to anything:

struct Example {
    std::vector<boost::any> elements;
}

To insert elements in this container, I had written a couple of helper functions (members of the struct Example):

void add_any(boost::any& a) {
    elements.push_back(a);
}

template<typename T>
void add_to_list(T& a) {
    boost::any bany = &a;
    add_any(bany);
}

Now, I would like to insert elements only when they are not present in this container. To do this, I thought that I would only need to call search over elements with an appropriate comparator function. However, I do not know how to compare the boost::any instances.

My question: Knowing that my boost::any instances always contain a pointer to something; is it possible to compare two boost::any values?


update

I thank you for your answers. I have also managed to do this in a probably unsafe way: using boost::unsafe_any_cast to obtain a void** and comparing the underlying pointer.

For the moment, this is working fine. I would, however, appreciate your comments: maybe this is a big mistake!

#include <boost/any.hpp>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

bool any_compare(const boost::any& a1, const boost::any& a2) {
    cout << "compare " << *boost::unsafe_any_cast<void*>(&a1)
         << " with:  " << *boost::unsafe_any_cast<void*>(&a2);
    return (*boost::unsafe_any_cast<void*>(&a1)) ==
        (*boost::unsafe_any_cast<void*>(&a2));
}

struct A {};

class Example {
public:
    Example() : elements(0),
                m_1(3.14),
                m_2(42),
                m_3("hello"),
                m_4() {};
    virtual ~Example() {};

    void test_insert() {
        add_to_list(m_1);
        add_to_list(m_2);
        add_to_list(m_3);
        add_to_list(m_4);
        add_to_list(m_1); // should not insert
        add_to_list(m_2); // should not insert
        add_to_list(m_3); // should not insert 
        add_to_list(m_4); // should not insert
    };

    template <typename T>
    void add_to_list(T& a) { 
        boost::any bany = &a;
        add_any(bany);
    }

private:
    vector<boost::any> elements;
    double m_1;
    int    m_2;
    string m_3;
    A      m_4;


    void add_any(const boost::any& a) {
        cout << "Trying to insert " << (*boost::unsafe_any_cast<void*>(&a)) << endl;
        vector<boost::any>::const_iterator it;
        for (it =  elements.begin();
             it != elements.end();
             ++it) {
            if ( any_compare(a,*it) ) {
                cout << " : not inserting, already in list" << endl;
                return;
            }
            cout << endl;
        }
        cout << "Inserting " << (*boost::unsafe_any_cast<void*>(&a)) << endl;
        elements.push_back(a);
    };


};



int main(int argc, char *argv[]) {

    Example ex;
    ex.test_insert();
    unsigned char c;
    ex.add_to_list(c);
    ex.add_to_list(c); // should not insert

    return 0;
}
d83
  • 195
  • 1
  • 1
  • 8
  • 2
    Side note: Your `add_any` and `add_to_list` functions should take a const-reference as parameter. Also, you might consider using a `set` instead of a `vector` if you want uniqueness. You'd still need a comparison function of course. – Björn Pollex May 17 '11 at 09:53
  • Good question. It is probably not possible. However, if you're willing to implement *your own* any, there is a way to do it (with *run time fail* if the underlying types are both the same, but do not support comparison semantics) – Alexandre C. May 17 '11 at 10:23
  • Are you sure that you want to store the address of the parameter to the `add_to_list` helper? Note that `boost::any` will copy the object, but in this case the object is a pointer. – David Rodríguez - dribeas May 17 '11 at 10:59
  • @David: Yes, this is exactly what I intended. I do not wish to copy the object (which may be big or un-copyable). – d83 May 17 '11 at 11:05
  • any reason for not using a `void*` then ? – Matthieu M. May 17 '11 at 11:37

5 Answers5

4

You cannot directly provide it, but you can actually use any as the underlying type... though for pointers it's pointless (ah!)

struct any {
  std::type_info const& _info;
  void* _address;
};

And a templated constructor:

template <typename T>
any::any(T* t):
   _info(typeid(*t)),
   _address(dynamic_cast<void*>(t))
{
}

This is, basically, boost::any.

Now we need to "augment" it with our comparison mechanism.

In order to do so, we'll "capture" the implementation of std::less.

typedef bool (*Comparer)(void*,void*);

template <typename T>
bool compare(void* lhs, void* rhs) const {
  return std::less<T>()(*reinterpret_cast<T*>(lhs), *reinterpret_cast<T*>(rhs));
}

template <typename T>
Comparer make_comparer(T*) { return compare<T>; }

And augment the constructor of any.

struct any {
  std::type_info const& _info;
  void* _address;
  Comparer _comparer;
};

template <typename T>
any::any(T* t):
  _info(typeid(*t)),
  _address(dynamic_cast<void*>(t)),
  _comparer(make_comparer(t))
{
}

Then, we provided a specialization of less (or operator<)

bool operator<(any const& lhs, any const& rhs) {
  if (lhs._info.before(rhs._info)) { return true; }
  if (rhs._info.before(lhs._info)) { return false; }
  return (*lhs._comparer)(lhs._address, rhs._address);
}

Note: encapsulation, etc... are left as an exercise to the reader

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
3

If you can change type in container, there is Boost.TypeErasure. It provides easy way to customize any. For example I'm using such typedef for similar purpose:

#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/operators.hpp>

using Foo = boost::type_erasure::any<
    boost::mpl::vector<
        boost::type_erasure::copy_constructible<>,
        boost::type_erasure::equality_comparable<>,
        boost::type_erasure::typeid_<>,
        boost::type_erasure::relaxed
    >
>;

Foo behaves exactly the same as boost::any, except that it can be compared for equality and use boost::type_erasure::any_cast instead of boost::any_cast.

magras
  • 1,709
  • 21
  • 32
3

The only easy way to do this I can think of involves hardcoding support for the types that you're storing in the any instances, undermining much of the usefulness of any...

bool equal(const boost::any& lhs, const boost::any& rhs)
{
    if (lhs.type() != rhs.type())
        return false;

    if (lhs.type() == typeid(std::string))
        return any_cast<std::string>(lhs) == any_cast<std::string>(rhs);

    if (lhs.type() == typeid(int))
        return any_cast<int>(lhs) == any_cast<int>(rhs);

    // ...

    throw std::runtime_error("comparison of any unimplemented for type");
}

With C++11's type_index you could use a std::map or std::unordered_map keyed on std::type_index(some_boost_any_object.type()) - similar to what Alexandre suggests in his comment below.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • 1
    a `map` should be more scalable. – Alexandre C. May 17 '11 at 10:45
  • @Alexandre: won't work reliably though... my recollection is that you can have multiple type_info objects for the same type (per translation unit, not guaranteed to be removed by the linker), so you need to be comparing the `type_info`s with the provided `operator==`... even mapping from `type_info->name()` is non-portable because implementations are allowed to provide any value including an empty string or a constant value across types. `type_info`'s just so special, you've got to love it! – Tony Delroy May 17 '11 at 10:51
  • 3
    @Alexandre: `std::type_info::before` – Matthieu M. May 17 '11 at 11:39
1

There is no need to create new class. Try to use xany https://sourceforge.net/projects/extendableany/?source=directory xany class allows to add new methods to any's existing functionality. By the way there is a example in documentation which does exactly what you want (creates comparable_any).

ArmanHunanyan
  • 905
  • 3
  • 11
  • 24
  • Doesn't seem to compile on clang Apple LLVM version 5.0 (clang-500.2.78) (based on LLVM 3.3svn) Target: x86_64-apple-darwin13.0.0 Also what is the license for this code? – berkus Jan 03 '14 at 16:57
1

Maybe this algorithm come in handy > http://signmotion.blogspot.com/2011/12/boostany.html

Compare two any-values by type and content. Attempt convert string to number for equals.