Browse Source

Enforce constantness of all Objects.

ObjPtr
Michael Uleysky 8 years ago
parent
commit
acc973982a
  1. 4
      include/builtin.h
  2. 34
      include/common.h
  3. 11
      include/globals.h
  4. 8
      include/object.h
  5. 13
      src/deptree.cpp
  6. 6
      src/object.cpp

4
include/builtin.h

@ -35,7 +35,7 @@ template<class T> class OpNeg {public: static T V(T a) {return -a;}};
template<class T> class OpPos {public: static T V(T a) {return +a;}}; template<class T> class OpPos {public: static T V(T a) {return +a;}};
template<template<typename> class Op, bool intArifm=true> template<template<typename> class Op, bool intArifm=true>
ObjectBase* Arifm2(const ObjectList* input) const ObjectBase* Arifm2(const ObjectList* input)
{ {
if(input->Size()!=2) return new ObjectError("Arifmetic binary operator","incorrect number of arguments"); if(input->Size()!=2) return new ObjectError("Arifmetic binary operator","incorrect number of arguments");
const ObjectBase *arg1=input->At(0),*arg2=input->At(1); const ObjectBase *arg1=input->At(0),*arg2=input->At(1);
@ -80,7 +80,7 @@ ObjectBase* Arifm2(const ObjectList* input)
template<template<typename> class Op, bool intArifm=true> template<template<typename> class Op, bool intArifm=true>
ObjectBase* Arifm1(const ObjectList* input) const ObjectBase* Arifm1(const ObjectList* input)
{ {
if(input->Size()!=1) return new ObjectError("Arifmetic unary operator","incorrect number of arguments"); if(input->Size()!=1) return new ObjectError("Arifmetic unary operator","incorrect number of arguments");
const ObjectBase *arg=input->At(0); const ObjectBase *arg=input->At(0);

34
include/common.h

@ -49,7 +49,7 @@ class EXPORT ObjectBase
bool isError() const {return err;} bool isError() const {return err;}
// Pure virtual api // Pure virtual api
virtual ~ObjectBase(){} virtual ~ObjectBase(){}
virtual ObjectBase* Copy() const=0; virtual const ObjectBase* Copy() const=0;
virtual bool Print() const=0; virtual bool Print() const=0;
virtual std::string Type() const=0; virtual std::string Type() const=0;
@ -167,7 +167,7 @@ class EXPORT ObjectError: public ObjectBase
const std::string& Function() const {return function;} const std::string& Function() const {return function;}
const std::string& Reason() const {return reason;} const std::string& Reason() const {return reason;}
// Pure virtual overrides // Pure virtual overrides
ObjectBase* Copy() const override {return new ObjectError(function,reason);} const ObjectBase* Copy() const override {return new ObjectError(function,reason);}
bool Print() const override bool Print() const override
{ {
COUT(NORMAL)<<std::endl<<"Object type: "<<Type()<<std::endl; COUT(NORMAL)<<std::endl<<"Object type: "<<Type()<<std::endl;
@ -193,7 +193,7 @@ public:
ObjectSimple(const T* t):val(*t) {} ObjectSimple(const T* t):val(*t) {}
~ObjectSimple() {} ~ObjectSimple() {}
// Pure virtual overrides // Pure virtual overrides
ObjectBase* Copy() const override {return new ObjectSimple<T>(val);} const ObjectBase* Copy() const override {return new ObjectSimple<T>(val);}
bool Print() const override bool Print() const override
{ {
COUT(NORMAL)<<std::endl<<"Object type: "<<Type()<<std::endl; COUT(NORMAL)<<std::endl<<"Object type: "<<Type()<<std::endl;
@ -236,15 +236,15 @@ class EXPORT ObjectPair: public ObjectBase
{ {
private: private:
std::string name; std::string name;
std::shared_ptr<ObjectBase> val; std::shared_ptr<const ObjectBase> val;
public: public:
ObjectPair() {} ObjectPair() {}
ObjectPair(const std::string& n, ObjectBase* v):name(n) {val.reset(v);} ObjectPair(const std::string& n, const ObjectBase* v):name(n) {val.reset(v);}
ObjectPair(const std::string* n, ObjectBase* v):name(*n) {val.reset(v);} ObjectPair(const std::string* n, const ObjectBase* v):name(*n) {val.reset(v);}
~ObjectPair() {} ~ObjectPair() {}
// Pure virtual overrides // Pure virtual overrides
ObjectBase* Copy() const override const ObjectBase* Copy() const override
{ {
auto ret=new ObjectPair; auto ret=new ObjectPair;
ret->name=name; ret->val=val; ret->name=name; ret->val=val;
@ -265,7 +265,7 @@ public:
// Own functions // Own functions
bool Exist() const {return nullptr!=val.get();} bool Exist() const {return nullptr!=val.get();}
ObjectBase* Get(const std::string& gname) const const ObjectBase* Get(const std::string& gname) const
{ {
if(gname==name) return val->Copy(); if(gname==name) return val->Copy();
else return new ObjectError("ObjectPair Get","pair has name "+name+" not "+gname); else return new ObjectError("ObjectPair Get","pair has name "+name+" not "+gname);
@ -277,7 +277,7 @@ public:
else return nullptr; else return nullptr;
} }
std::string Name() const {return name;} std::string Name() const {return name;}
void SetPair(const std::string& n, ObjectBase* v) {if(!Exist()) {name=n; val.reset(v);}} void SetPair(const std::string& n, const ObjectBase* v) {if(!Exist()) {name=n; val.reset(v);}}
const ObjectBase* Value() const {return val.get();} const ObjectBase* Value() const {return val.get();}
}; };
@ -285,16 +285,16 @@ public:
class EXPORT ObjectList: public ObjectBase class EXPORT ObjectList: public ObjectBase
{ {
public: public:
typedef std::deque<ObjectBase*> ListValues; typedef std::deque<const ObjectBase*> ListValues;
private: private:
std::shared_ptr<ListValues> vals; std::shared_ptr<ListValues> vals;
public: public:
ObjectList() {vals.reset(new ListValues, [](ListValues* p){for(auto& i: *p) delete i; delete p;});} ObjectList() {vals.reset(new ListValues, [](ListValues* p){for(auto& i: *p) delete i; delete p;});}
ObjectList(ObjectBase* o) {vals.reset(new ListValues, [](ListValues* p){for(auto& i: *p) delete i; delete p;}); PushBack(o);} ObjectList(const ObjectBase* o) {vals.reset(new ListValues, [](ListValues* p){for(auto& i: *p) delete i; delete p;}); PushBack(o);}
~ObjectList() {} ~ObjectList() {}
// Pure virtual overrides // Pure virtual overrides
ObjectBase* Copy() const override const ObjectBase* Copy() const override
{ {
auto ret=new ObjectList; auto ret=new ObjectList;
ret->vals=vals; ret->vals=vals;
@ -321,7 +321,7 @@ public:
// Own functions // Own functions
const ObjectBase* At(ListValues::size_type i) const {return (*vals)[i];} const ObjectBase* At(ListValues::size_type i) const {return (*vals)[i];}
bool Exist() const {return 0!=vals->size();} bool Exist() const {return 0!=vals->size();}
ObjectBase* Get(const std::string& gname) const const ObjectBase* Get(const std::string& gname) const
{ {
const ObjectBase* p=Find(gname); const ObjectBase* p=Find(gname);
return (nullptr==p)?new ObjectError("ObjectList Get","name "+gname+" not found in list"):p->Copy(); return (nullptr==p)?new ObjectError("ObjectList Get","name "+gname+" not found in list"):p->Copy();
@ -342,11 +342,11 @@ public:
return nullptr; return nullptr;
} }
ListValues::size_type Size() const {return vals->size();} ListValues::size_type Size() const {return vals->size();}
ObjectList* PushBack(ObjectBase* p) {vals->push_back(p); return this;} ObjectList* PushBack(const ObjectBase* p) {vals->push_back(p); return this;}
ObjectList* PushFront(ObjectBase* p) {vals->push_front(p); return this;} ObjectList* PushFront(const ObjectBase* p) {vals->push_front(p); return this;}
}; };
typedef ObjectBase* (*Func)(const ObjectList*); typedef const ObjectBase* (*Func)(const ObjectList*);
typedef int (*ModuleInitFunc)(const void*); typedef int (*ModuleInitFunc)(const void*);
extern "C" { extern "C" {
@ -355,7 +355,7 @@ EXPORT int LoadModule(const std::string& name, const void* p, const std::string&
} }
template<class T> template<class T>
ObjectBase* Get(const ObjectList* input) const ObjectBase* Get(const ObjectList* input)
{ {
if(input->Size()!=2) return new ObjectError("GET","incorrect number of arguments"); if(input->Size()!=2) return new ObjectError("GET","incorrect number of arguments");
OBType<T> ob(input->At(0)); OBType<T> ob(input->At(0));

11
include/globals.h

@ -14,17 +14,6 @@ extern G_funcsType G_funcs;
typedef std::map<std::string,ExecExpr> VarType; typedef std::map<std::string,ExecExpr> VarType;
typedef std::vector<ExecExpr> ExecType; typedef std::vector<ExecExpr> ExecType;
/*
// Variables definitions
extern G_VarType G_Vars;
// List of objects to save
extern G_ExecType G_ToSave;
// List of objects to print
extern G_ExecType G_ToPrint;
*/
// Loaded modules // Loaded modules
typedef std::vector<void*> G_libsType; typedef std::vector<void*> G_libsType;
extern G_libsType G_libs; extern G_libsType G_libs;

8
include/object.h

@ -55,7 +55,7 @@ public:
const ObjectBase* Object() const {return isObject()?obj:nullptr;} const ObjectBase* Object() const {return isObject()?obj:nullptr;}
// Detach object // Detach object
ObjectBase* PickObject() const ObjectBase* PickObject()
{ {
if(isObject()) if(isObject())
{ {
@ -64,7 +64,7 @@ public:
} }
else return nullptr; else return nullptr;
} }
bool ReplaceByObject(ObjectBase* ob) bool ReplaceByObject(const ObjectBase* ob)
{ {
if(isObject()) {delete ob; return false;} if(isObject()) {delete ob; return false;}
type=TYPE_OBJECT; type=TYPE_OBJECT;
@ -83,7 +83,7 @@ public:
private: private:
Type type; Type type;
ObjectBase* obj; const ObjectBase* obj;
std::string name; std::string name;
struct grammatic_location location; struct grammatic_location location;
}; };
@ -104,7 +104,7 @@ std::string DumpExprE(const ExecExpr& exp);
std::string DumpExpr(const ExecExpr& exp); std::string DumpExpr(const ExecExpr& exp);
UsedType UsedVars(const ExecExpr& exp); UsedType UsedVars(const ExecExpr& exp);
UsedType UsedFuncs(const ExecExpr& exp); UsedType UsedFuncs(const ExecExpr& exp);
ObjectBase* Evaluate(ExecExpr& exp, bool* err); const ObjectBase* Evaluate(ExecExpr& exp, bool* err);
//inline void ReplaceVar(ExecExpr& exp, const std::string& var, ObjectBase* ob) {for(auto& se: exp) if(se.isVar() && var==se.Name()) se.ReplaceByObject(ob);} //inline void ReplaceVar(ExecExpr& exp, const std::string& var, ObjectBase* ob) {for(auto& se: exp) if(se.isVar() && var==se.Name()) se.ReplaceByObject(ob);}
void ReplaceVar(ExecExpr& exp, const std::string& var, const ObjectBase* ob);// {for(auto& se: exp) if(se.isVar() && var==se.Name()) se.ReplaceByObject(ob);} void ReplaceVar(ExecExpr& exp, const std::string& var, const ObjectBase* ob);// {for(auto& se: exp) if(se.isVar() && var==se.Name()) se.ReplaceByObject(ob);}

13
src/deptree.cpp

@ -300,7 +300,7 @@ void* TreeEvaluateM(void* arg)
{ {
err=false; err=false;
std::unique_ptr<ObjectList> ol(dynamic_cast<ObjectList*>(Evaluate(leaf->exe,&err))); // No check for return value, trust in grammatical parser std::unique_ptr<const ObjectList> ol(dynamic_cast<const ObjectList*>(Evaluate(leaf->exe,&err))); // No check for return value, trust in grammatical parser
if(err) if(err)
{ {
const struct grammatic_location& loc=leaf->exe.front().Location(); const struct grammatic_location& loc=leaf->exe.front().Location();
@ -326,7 +326,7 @@ void* TreeEvaluateM(void* arg)
{ {
err=false; err=false;
std::unique_ptr<ObjectList> ol(dynamic_cast<ObjectList*>(Evaluate(leaf->exe,&err))); // No check for return value, trust in grammatical parser std::unique_ptr<const ObjectList> ol(dynamic_cast<const ObjectList*>(Evaluate(leaf->exe,&err))); // No check for return value, trust in grammatical parser
if(err) if(err)
{ {
const struct grammatic_location& loc=leaf->exe.front().Location(); const struct grammatic_location& loc=leaf->exe.front().Location();
@ -347,12 +347,11 @@ void* TreeEvaluateM(void* arg)
(*leaf->parents.begin())->childrens.erase(leaf); (*leaf->parents.begin())->childrens.erase(leaf);
// End critical section // End critical section
pthread_mutex_unlock(&p->root_mtx); pthread_mutex_unlock(&p->root_mtx);
} }
if(DepTree::VAR==leaf->type) if(DepTree::VAR==leaf->type)
{ {
ObjectBase *eob; const ObjectBase *eob;
// Main working call // Main working call
err=false; err=false;
@ -412,7 +411,7 @@ void* TreeEvaluate(void* arg)
{ {
err=false; err=false;
std::unique_ptr<ObjectList> ol(dynamic_cast<ObjectList*>(Evaluate(leaf->exe,&err))); // No check for return value, trust in grammatical parser std::unique_ptr<const ObjectList> ol(dynamic_cast<const ObjectList*>(Evaluate(leaf->exe,&err))); // No check for return value, trust in grammatical parser
if(err) if(err)
{ {
const struct grammatic_location& loc=leaf->exe.front().Location(); const struct grammatic_location& loc=leaf->exe.front().Location();
@ -435,7 +434,7 @@ void* TreeEvaluate(void* arg)
{ {
err=false; err=false;
std::unique_ptr<ObjectList> ol(dynamic_cast<ObjectList*>(Evaluate(leaf->exe,&err))); // No check for return value, trust in grammatical parser std::unique_ptr<const ObjectList> ol(dynamic_cast<const ObjectList*>(Evaluate(leaf->exe,&err))); // No check for return value, trust in grammatical parser
if(err) if(err)
{ {
const struct grammatic_location& loc=leaf->exe.front().Location(); const struct grammatic_location& loc=leaf->exe.front().Location();
@ -455,7 +454,7 @@ void* TreeEvaluate(void* arg)
if(DepTree::VAR==leaf->type) if(DepTree::VAR==leaf->type)
{ {
ObjectBase *eob; const ObjectBase *eob;
// Main working call // Main working call
err=false; err=false;

6
src/object.cpp

@ -114,7 +114,7 @@ UsedType UsedFuncs(const ExecExpr& exp)
return ret; return ret;
} }
ObjectBase* Evaluate(ExecExpr& exp, bool* err) const ObjectBase* Evaluate(ExecExpr& exp, bool* err)
{ {
ExecExpr::iterator cse=exp.begin(); ExecExpr::iterator cse=exp.begin();
while(cse!=exp.end()) while(cse!=exp.end())
@ -153,7 +153,7 @@ ObjectBase* Evaluate(ExecExpr& exp, bool* err)
ExecExpr::iterator pr=cse; ExecExpr::iterator pr=cse;
pr--; pr--;
const ObjectList* arg=dynamic_cast<const ObjectList*>(pr->Object()); const ObjectList* arg=dynamic_cast<const ObjectList*>(pr->Object());
ObjectBase* res; const ObjectBase* res;
// Functions check already done, we know that our function exists. // Functions check already done, we know that our function exists.
// Sequentally call all functions with given name // Sequentally call all functions with given name
@ -192,7 +192,7 @@ ObjectBase* Evaluate(ExecExpr& exp, bool* err)
} }
} }
// After all, our list contains only one object. // After all, our list contains only one object.
ObjectBase* ret=exp.begin()->PickObject(); const ObjectBase* ret=exp.begin()->PickObject();
exp.clear(); exp.clear();
return ret; return ret;
} }

Loading…
Cancel
Save