Browse Source

Add function Exist() to temaplates OBType and OBTypeM to check if ObjectBase* pointer is non-zero.

ObjPtr
Michael Uleysky 9 years ago
parent
commit
403b2edb62
  1. 12
      include/common.h

12
include/common.h

@ -47,30 +47,34 @@ public:
// Template for checking and using ObjectBase derivative classes // Template for checking and using ObjectBase derivative classes
// Checking if arg is non-zero: if(OBType<Derived>(arg).Exist())
// Checking if arg is pointer on Derived: if(OBType<Derived>(arg)) // Checking if arg is pointer on Derived: if(OBType<Derived>(arg))
// Using const ObjectBase* arg as const Derived*: OBType<Derived>(arg)->SomeCall() // Using const ObjectBase* arg as const Derived*: OBType<Derived>(arg)->SomeCall()
template<class O> template<class O>
class OBType class OBType
{ {
const O* p; const O* p;
bool iszero;
public: public:
OBType() = delete; OBType() = delete;
OBType(OBType&&) = delete; OBType(OBType&&) = delete;
OBType(OBType&) = delete; OBType(OBType&) = delete;
OBType(const ObjectBase* arg) {if(0==arg) p=0; else if(typeid(*arg)==typeid(O)) p=dynamic_cast<const O*>(arg); else p=0;} OBType(const ObjectBase* arg):iszero(0==p) {if(0==arg) p=0; else if(typeid(*arg)==typeid(O)) p=dynamic_cast<const O*>(arg); else p=0;}
const O* operator->() const {return p;} const O* operator->() const {return p;}
operator bool() const {return 0!=p;} operator bool() const {return 0!=p;}
operator const O*() const {return p;} operator const O*() const {return p;}
bool Exist() const {return !iszero;}
}; };
// Template for checking and using several ObjectBase derivative classes // Template for checking and using several ObjectBase derivative classes
// Func is template of class-function template argument of which can be any of Derived classes. // Func is template of class-function template argument of which can be any of Derived classes.
// It must have at least one argument - pointer to constant object of Derived class and return non-void. // It must have at least one argument - pointer to constant object of Derived class and return non-void.
// Checking if arg is pointer on any of Derived classes: if(OBType<Func,Derived>(arg)) // Checking if arg is non-zero: if(OBTypeM<Func,Derived...>(arg).Exist())
// Checking if arg is pointer on any of Derived classes (always false if arg is zero): if(OBTypeM<Func,Derived...>(arg))
// Applying Func can be done by two ways: // Applying Func can be done by two ways:
// 1) Function bool Apply(Res& res, args...). Here res is result of calling Func with arguments args. Result of Func is statically casted to type Res. // 1) Function bool Apply(Res& res, args...). Here res is result of calling Func with arguments args. Result of Func is statically casted to type Res.
// If arg is not a pointer on any of Derived classes, Apply() return false and res is not changed. // If arg is zero or not a pointer on any of Derived classes, Apply() return false and res is not changed.
// 2) Overloaded operator ()(args). It returns result of calling Func with arguments args. Type of returning value is type of Func<Derived1>(). // 2) Overloaded operator ()(args). It returns result of calling Func with arguments args. Type of returning value is type of Func<Derived1>().
// Full definition. Never instantiated. // Full definition. Never instantiated.
template<template<typename> class Func, class... O> template<template<typename> class Func, class... O>
@ -110,6 +114,7 @@ class OBTypeM<Func,O1,O...>: public OBTypeM<Func,O...>
if(right) return Func<O1>()(dynamic_cast<const O1*>(OBTypeM<Func,O...>::P()),args...); if(right) return Func<O1>()(dynamic_cast<const O1*>(OBTypeM<Func,O...>::P()),args...);
else return OBTypeM<Func,O...>::template F<T,Args...>(args...); else return OBTypeM<Func,O...>::template F<T,Args...>(args...);
} }
bool Exist() const {return OBTypeM<Func,O...>::Exist();}
}; };
// Partial instantiation of the bottom of recursion // Partial instantiation of the bottom of recursion
@ -130,6 +135,7 @@ class OBTypeM<Func>
void operator ()(Args... args) const {} void operator ()(Args... args) const {}
template<class Res, class... Args> template<class Res, class... Args>
Res F(Args... args) const {return Res();} Res F(Args... args) const {return Res();}
bool Exist() const {return 0!=p;}
}; };

Loading…
Cancel
Save