From 403b2edb62e0ccc868cd29bb806a22d0835ce2c3 Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Sun, 11 Oct 2015 17:33:07 +1000 Subject: [PATCH] Add function Exist() to temaplates OBType and OBTypeM to check if ObjectBase* pointer is non-zero. --- include/common.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/common.h b/include/common.h index 9b88488..af08cac 100644 --- a/include/common.h +++ b/include/common.h @@ -47,30 +47,34 @@ public: // Template for checking and using ObjectBase derivative classes +// Checking if arg is non-zero: if(OBType(arg).Exist()) // Checking if arg is pointer on Derived: if(OBType(arg)) // Using const ObjectBase* arg as const Derived*: OBType(arg)->SomeCall() template class OBType { const O* p; + bool iszero; public: 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(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(arg); else p=0;} const O* operator->() const {return p;} operator bool() const {return 0!=p;} operator const O*() const {return p;} + bool Exist() const {return !iszero;} }; // 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. // 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(arg)) +// Checking if arg is non-zero: if(OBTypeM(arg).Exist()) +// Checking if arg is pointer on any of Derived classes (always false if arg is zero): if(OBTypeM(arg)) // 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. -// 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(). // Full definition. Never instantiated. template class Func, class... O> @@ -110,6 +114,7 @@ class OBTypeM: public OBTypeM if(right) return Func()(dynamic_cast(OBTypeM::P()),args...); else return OBTypeM::template F(args...); } + bool Exist() const {return OBTypeM::Exist();} }; // Partial instantiation of the bottom of recursion @@ -130,6 +135,7 @@ class OBTypeM void operator ()(Args... args) const {} template Res F(Args... args) const {return Res();} + bool Exist() const {return 0!=p;} };