开发者

identifying the type

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*>(p开发者_开发问答tr) != 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.


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) );
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜