tags:

views:

148

answers:

2

Hi, In my application, there is a inheritance hierarchy in which only the classes that are at the end of the inheritance chain are non-abstract classes. Also there is some usage of boost::variant. I want to write a function which takes a pointer and a Type and says whether the object belongs to that type.

For example

#define IsA(nodeptr, type)   ( checkType<type>(nodeptr) ) 

template<typename Type, bool isAbstract, typename PtrType >
class CheckType
{
    bool operator()( PtrType* ptr ) { return ( typeid(*ptr) == typeid(Type) ); }
};

template<typename Type, typename PtrType >
class CheckType < Type, true, PtrType >
{
    bool operator()( PtrType* ptr ) { return ( dynamic_cast<Type*>(ptr) != NULL ); }
};

template<typename Type, BOOST_VARIANT_ENUM_PARAMS(typename T) >
class CheckType< Type, false, boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
{
    bool operator()( boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>* ptr ) 
    { 
        return ( ptr->type() == typeid(Type) ); 
    }
}

template< typename Type, typename PtrType >
bool checkType( PtrType* nodePtr )
{
    CheckType<Type, boost::is_abstract<PtrType>::value, PtrType> check;
    return check(nodePtr);
}

Now if there is a boost variant, i want to find out whether the boost variant stores that particular type. Can someone help me with that? I don't want to add an extra parameter to find out whether it is a variant. Even for finding out the abstractness, i am using boost::is_abstract..

Thanks, Gokul.

+1  A: 

Well there are two direct versions of this:

return (boost::get<Type*>(v) != 0);

And this:

return ( v.type() == typeid(Type) );

I am not sure how to handle that with your template overloading cleanly but you could do something like this:

template< typename Type, bool TypeisAbstract, bool TypeIsVariant,
          typename ptrType >
bool checkType( ptrType* t)
{
    return ( typeid(*t) == typeid(Type) );
}

template< typename Type, typename ptrType >
bool checkType<Type, true, false, ptrType>( ptrType* t)
{
    return ( dynamic_cast<Type*>(t) != NULL );
}

template< typename Type>
bool checkType<Type, false, true, ptrType>(const boost::variant& v)
{
    return ( v.type() == typeid(Type) );
}
Joe
I want to do it without adding a template parameter to say whether it is a variant...
Gokul
oh! i overlooked the type() function in variant thanks
Gokul
+1  A: 

The cleanest way to deal with Boost.Variant is normally to use a Visitor.

template <class Type>
class TypeChecker: boost::static_visitor<>
{
public:
  explicit TypeChecker(bool& result): mResult(result) {}

  template <class T>
  void operator()(const T& t) const { mResult = dynamic_cast<const Type*>(&t); }

private:
  bool& mResult;
};

Then you can wrap it:

template <class Type, class Variant>
bool checkType(const Variant& v)
{
  bool result = false;
  boost::apply_visitor(TypeChecker<Type>(result), v);
  return result;
}

Which can be used like so:

int main(int argc, char* argv[])
{
  typedef boost::variant<Dog,Cat,Kid> variant_type;

  variant_type var = /**/;

  if (checkType<Animal>(var))
  {
  }
  else
  {
  }
}

However this is not the OO-way, and neither it is the variant-way.

A better idea would be to use the full power of Boost.Variant:

struct DoIt: boost::static_visitor<>
{
  void operator()(const Animal& animal) const {}
  void operator()(const Kid& kid) const {}
};


int main(int argc, char* argv[])
{
  typedef boost::variant<Dog,Cat,Kid> variant_type;

  variant_type var = /**/;

  boost::apply_visitor(DoIt(), var);
}

Note how the static_visitor concept naturally handles inheritance.

Matthieu M.
I think, visitor may not suit the exact purpose here, as i want to deal with any variant
Gokul
If you look at the first solution, since the only `operator()` implemented in the visitor is a template one, it'll work with any variant :)
Matthieu M.