From b0969362a7a86b83b2d8e6140f70da120d545a01 Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Tue, 6 Oct 2015 12:46:02 +1000 Subject: [PATCH] OBType and OBTypeM now can accept zero pointer as constructor parameter. OBType can be autoconverted to pointer on template parameter class. --- include/common.h | 60 ++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/include/common.h b/include/common.h index e789374..9b88488 100644 --- a/include/common.h +++ b/include/common.h @@ -18,7 +18,33 @@ EXPORT std::ostream& COUT(debug_level dl); typedef std::set UsedType; -class EXPORT ObjectBase; + +// Base class for all objects +class EXPORT ObjectBase +{ +protected: +// No save by default +virtual const int8_t* Blob(size_t* size) const { *size=0; return 0; } +virtual void DeallocBlob(const int8_t* ptr) const {}; +public: + + ObjectBase() = default; + ObjectBase(const ObjectBase&) = delete; + bool Save(const char* fname) const; + // Pure virtual api + virtual ~ObjectBase(){} + virtual ObjectBase* Copy() const=0; + virtual bool Print() const=0; + virtual std::string Type() const=0; + + // Virtual api with default functions. Modules types must not override them. + virtual std::string Dump() const {return "%"+Type()+"%";} + virtual ObjectBase* Evaluate(bool* err) {*err=false; return 0;} + virtual ObjectBase* ReplaceVar(const std::string& vname, ObjectBase* ob) {return 0;} + virtual void UsedFuncs(UsedType& funcs) const {} + virtual void UsedIdents(UsedType& ids) const {} +}; + // Template for checking and using ObjectBase derivative classes // Checking if arg is pointer on Derived: if(OBType(arg)) @@ -31,9 +57,10 @@ class OBType OBType() = delete; OBType(OBType&&) = delete; OBType(OBType&) = delete; - OBType(const ObjectBase* arg) {if(typeid(*arg)==typeid(O)) p=dynamic_cast(arg); else p=0;} + OBType(const ObjectBase* arg) {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;} }; @@ -67,7 +94,7 @@ class OBTypeM: public OBTypeM else return OBTypeM::template F(args...); } public: - OBTypeM(const ObjectBase* arg):OBTypeM(arg) {right=(typeid(*arg)==typeid(O1));} + OBTypeM(const ObjectBase* arg):OBTypeM(arg) {if(0==arg) right=false; else right=(typeid(*arg)==typeid(O1));} operator bool() const {return right || OBTypeM::operator bool();} template bool Apply(Res& res, Args... args) const @@ -106,33 +133,6 @@ class OBTypeM }; -// Base class for all objects -class EXPORT ObjectBase -{ -protected: -// No save by default -virtual const int8_t* Blob(size_t* size) const { *size=0; return 0; } -virtual void DeallocBlob(const int8_t* ptr) const {}; -public: - - ObjectBase() = default; - ObjectBase(const ObjectBase&) = delete; - bool Save(const char* fname) const; - // Pure virtual api - virtual ~ObjectBase(){} - virtual ObjectBase* Copy() const=0; - virtual bool Print() const=0; - virtual std::string Type() const=0; - - // Virtual api with default functions. Modules types must not override them. - virtual std::string Dump() const {return "%"+Type()+"%";} - virtual ObjectBase* Evaluate(bool* err) {*err=false; return 0;} - virtual ObjectBase* ReplaceVar(const std::string& vname, ObjectBase* ob) {return 0;} - virtual void UsedFuncs(UsedType& funcs) const {} - virtual void UsedIdents(UsedType& ids) const {} -}; - - // Template for objects without specific constructor/destructor template class EXPORT ObjectSimple: public ObjectBase