Browse Source

Realisation of print() and save()

test
Michael Uleysky 9 years ago
parent
commit
ef2218b91c
  1. 2
      src/debug.h
  2. 69
      src/deptree.cpp
  3. 57
      src/globals.cpp
  4. 3
      src/globals.h
  5. 17
      src/object.h

2
src/debug.h

@ -3,7 +3,7 @@
#include <iostream> #include <iostream>
#include "common.h" #include "common.h"
enum debug_level {INTERNALREQUEST,MOREDEBUG,DEBUG,INFO,WARNING,ERROR}; enum debug_level {INTERNALREQUEST,MOREDEBUG,DEBUG,INFO,NORMAL,WARNING,ERROR};
EXPORT std::ostream& COUT(debug_level dl); EXPORT std::ostream& COUT(debug_level dl);

69
src/deptree.cpp

@ -193,6 +193,7 @@ void TreeEvaluate(std::mutex* mtx, int* errflag, DepTree::LeafVector* leafs, con
// mtx[1] - mutex for manipulating with tree // mtx[1] - mutex for manipulating with tree
DepTree* leaf; DepTree* leaf;
ObjectBase *ob,*eob; ObjectBase *ob,*eob;
ObjectList* ol;
bool err; bool err;
while(true) while(true)
{ {
@ -213,12 +214,42 @@ void TreeEvaluate(std::mutex* mtx, int* errflag, DepTree::LeafVector* leafs, con
if(DepTree::SAVE==leaf->type) if(DepTree::SAVE==leaf->type)
{ {
COUT(INFO)<<"save ("<<G_tosave[leaf->index]->Dump()<<")"<<std::endl; err=false;
ol=G_tosave[leaf->index];
ol->Evaluate(&err); // For list Evaluate always return 0;
if(err)
{
COUT(ERROR)<<" in instruction save"<<ol->Dump()<<")"<<std::endl;
*errflag=1;
return;
}
if(!Save(ol))
{
mtx[0].lock();
*errflag=1;
mtx[0].unlock();
return;
}
} }
if(DepTree::PRINT==leaf->type) if(DepTree::PRINT==leaf->type)
{ {
COUT(INFO)<<"print ("<<G_toprint[leaf->index]->Dump()<<")"<<std::endl; err=false;
ol=G_toprint[leaf->index];
ol->Evaluate(&err); // For list Evaluate always return 0;
if(err)
{
COUT(ERROR)<<" in instruction print"<<ol->Dump()<<")"<<std::endl;
*errflag=1;
return;
}
if(!Print(ol))
{
mtx[0].lock();
*errflag=1;
mtx[0].unlock();
return;
}
} }
if(DepTree::VAR==leaf->type) if(DepTree::VAR==leaf->type)
@ -273,6 +304,7 @@ void TreeEvaluate(int* errflag, DepTree::LeafVector* leafs)
{ {
DepTree* leaf; DepTree* leaf;
ObjectBase *ob,*eob; ObjectBase *ob,*eob;
ObjectList* ol;
bool err; bool err;
while(0!=leafs->size()) while(0!=leafs->size())
{ {
@ -283,12 +315,39 @@ void TreeEvaluate(int* errflag, DepTree::LeafVector* leafs)
if(DepTree::SAVE==leaf->type) if(DepTree::SAVE==leaf->type)
{ {
COUT(INFO)<<"save ("<<G_tosave[leaf->index]->Dump()<<")"<<std::endl; err=false;
ol=G_tosave[leaf->index];
ol->Evaluate(&err); // For list Evaluate always return 0;
if(err)
{
COUT(ERROR)<<" in instruction save"<<ol->Dump()<<")"<<std::endl;
*errflag=1;
return;
}
// eob is evaluated object
if(!Save(ol))
{
*errflag=1;
return;
}
} }
if(DepTree::PRINT==leaf->type) if(DepTree::PRINT==leaf->type)
{ {
COUT(INFO)<<"print ("<<G_toprint[leaf->index]->Dump()<<")"<<std::endl; err=false;
ol=G_toprint[leaf->index];
ol->Evaluate(&err); // For list Evaluate always return 0;
if(err)
{
COUT(ERROR)<<" in instruction print"<<ol->Dump()<<")"<<std::endl;
*errflag=1;
return;
}
if(!Print(ol))
{
*errflag=1;
return;
}
} }
if(DepTree::VAR==leaf->type) if(DepTree::VAR==leaf->type)
@ -307,7 +366,7 @@ void TreeEvaluate(int* errflag, DepTree::LeafVector* leafs)
// eob is evaluated object // eob is evaluated object
if(0!=eob) delete ob; if(0!=eob) delete ob;
else eob=ob; else eob=ob;
G_vars.erase(leaf->name); // Concurrent access is safe G_vars.erase(leaf->name);
for(auto& i:leaf->parents) for(auto& i:leaf->parents)
{ {
// leaf not children of anyone // leaf not children of anyone

57
src/globals.cpp

@ -28,3 +28,60 @@ void RegisterFunction(const std::string& name, Func func)
{ {
G_funcs.emplace(name,func); G_funcs.emplace(name,func);
} }
bool Save(ObjectList* input)
{
ObjectList::ListValues::size_type sz=input->Size(), i;
if(sz<2 || sz%2==1 )
{
COUT(ERROR)<<"Number of save arguments must not be "<<sz<<std::endl;
return false;
}
// Check arguments types
const ObjectBase* arg1;
const ObjectString* arg2;
for(i=0;i<sz/2;i++)
{
arg1=input->At(i*2+1);
if(!IS_OTYPE(arg1,ObjectString))
{
COUT(ERROR)<<"Save format is save(object_1,file_1,...,object_n,file_n) where file_i is string"<<std::endl;
return false;
}
}
// Save
for(i=0;i<sz/2;i++)
{
arg1=input->At(i*2);
arg2=dynamic_cast<const ObjectString*>(input->At(i*2+1));
if(!arg1->Save(arg2->Value().c_str()))
{
COUT(ERROR)<<"Can't save object "<<arg1->Dump()<<" to file "<<arg2->Value()<<std::endl;
return false;
}
}
return true;
}
bool Print(ObjectList* input)
{
ObjectList::ListValues::size_type sz=input->Size(), i;
if(sz==0) return true;
// Print
for(i=0;i<sz;i++)
{
if(!input->At(i)->Print())
{
COUT(ERROR)<<"Unprintable object "<<input->At(i)->Dump()<<std::endl;
return false;
}
}
return true;
}

3
src/globals.h

@ -23,6 +23,9 @@ extern EXPORT G_toType G_toprint;
void ClearGlobals(); void ClearGlobals();
bool Save(ObjectList* input);
bool Print(ObjectList* input);
extern "C" { extern "C" {
EXPORT void RegisterFunction(const std::string& name, Func func); EXPORT void RegisterFunction(const std::string& name, Func func);
} }

17
src/object.h

@ -96,8 +96,8 @@ public:
~ObjectSimple() {} ~ObjectSimple() {}
bool Print() const override bool Print() const override
{ {
COUT(INFO)<<"Object type: "<<Type()<<"."<<std::endl; COUT(NORMAL)<<std::endl<<"Object type: "<<Type()<<std::endl;
COUT(INFO)<<"Value: "<<val<<std::endl; COUT(NORMAL)<<"Value: "<<val<<std::endl;
return true; return true;
} }
std::string Type() const override {return type;} std::string Type() const override {return type;}
@ -145,9 +145,9 @@ public:
bool Print() const override bool Print() const override
{ {
if(!Exist()) return false; if(!Exist()) return false;
COUT(INFO)<<"Object type: "<<Type()<<"."<<std::endl; COUT(NORMAL)<<std::endl<<"Object type: "<<Type()<<std::endl;
COUT(INFO)<<"Name: "<<Name()<<std::endl; COUT(NORMAL)<<"Value: "<<val<<std::endl;
COUT(INFO)<<"Value type: "<<val->Type()<<std::endl; COUT(NORMAL)<<"Value type: "<<val->Type()<<std::endl;
return true; return true;
} }
std::string Type() const override {return "pair";} std::string Type() const override {return "pair";}
@ -192,8 +192,9 @@ public:
// Class for objects list // Class for objects list
class EXPORT ObjectList: public ObjectBase class EXPORT ObjectList: public ObjectBase
{ {
private: public:
typedef std::vector<ObjectBase*> ListValues; typedef std::vector<ObjectBase*> ListValues;
private:
std::shared_ptr<ListValues> vals; std::shared_ptr<ListValues> vals;
public: public:
@ -205,8 +206,8 @@ public:
bool Print() const override bool Print() const override
{ {
if(!Exist()) return false; if(!Exist()) return false;
COUT(INFO)<<"Object type: "<<Type()<<"."<<std::endl; COUT(NORMAL)<<std::endl<<"Object type: "<<Type()<<std::endl;
COUT(INFO)<<"Number of elements: "<<Size()<<std::endl; COUT(NORMAL)<<"Number of elements: "<<Size()<<std::endl;
return true; return true;
} }
ListValues::size_type Size() const {return vals->size();} ListValues::size_type Size() const {return vals->size();}

Loading…
Cancel
Save