00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00020 #ifndef _ImageProcessorsDescription_hpp
00021 #define _ImageProcessorsDescription_hpp
00022
00023 #include<ImLib3D/XMLImageProcessorsDescription.hpp>
00024
00025 class ImProcNode;
00027 class ImProcTreeCallBack
00028 {
00029 public:
00031 virtual bool ImProcNodeCallBack(ImProcNode *node,int depth)=0;
00032 virtual ~ImProcTreeCallBack(){;}
00033 };
00035 class ImProcNode
00036 {
00037 public:
00038 string name;
00039 ImProcNode *parent;
00040 vector<ImProcNode *> subnodes;
00041
00042 void Walk(ImProcTreeCallBack *cb,int depth=0)
00043 {
00044 if(!cb->ImProcNodeCallBack(this,depth)){return;}
00045 for(uint i=0;i<subnodes.size();i++){subnodes[i]->Walk(cb,depth+1);}
00046 }
00047 ImProcNode *FindNode(const string &sname)
00048 {
00049 if(name==sname){return this;}
00050 for(uint i=0;i<subnodes.size();i++)
00051 {
00052 ImProcNode *res=subnodes[i]->FindNode(sname);
00053 if(res){return res;}
00054 }
00055 return NULL;
00056 }
00057 ImProcNode(string _name):name(_name),parent(NULL){;}
00058 };
00059
00060
00061
00063 class ImProcTree : public ImProcTreeCallBack
00064 {
00065 public:
00066 enum WalkType {WalkShow,WalkMaxDepth,WalkSetParents};
00067 WalkType walkType;
00068 int maxDepth;
00069 void Walk(ImProcTreeCallBack *cb){root.Walk(cb);}
00070 bool ImProcNodeCallBack(ImProcNode *node,int depth)
00071 {
00072 if(walkType==WalkSetParents)
00073 {
00074 for(uint i=0;i<node->subnodes.size();i++)
00075 {node->subnodes[i]->parent=node;}
00076 }
00077 if(walkType==WalkMaxDepth)
00078 {
00079 maxDepth=max(depth,maxDepth);
00080 }
00081 if(walkType==WalkShow)
00082 {
00083 for(int j=0;j<depth*4;j++){printf(" ");}
00084 printf("%s\n",node->name.c_str());
00085 }
00086 return true;
00087 }
00088 ImProcNode root;
00089 void Show(){walkType=WalkShow;root.Walk(this);}
00090 void SetParents(){walkType=WalkSetParents;root.Walk(this);}
00091 int Depth()
00092 {
00093 maxDepth=0;
00094 walkType=WalkMaxDepth;
00095 root.Walk(this);
00096 return maxDepth;
00097 }
00098 ImProcNode *FindNode(const string &sname)
00099 {
00100 return root.FindNode(sname);
00101 }
00102 ImProcTree():root("ImageProcessor")
00103 {
00104 }
00105 };
00106
00108
00112 class ImageProcessorsDescription : public XIPD::ImageProcessorsDescription
00113 {
00114 protected:
00116 ImProcTree imProcTree;
00118 void CreateTree();
00119 public:
00120 ImProcTree &GetImProcTree(){return imProcTree;}
00121 void PrintTree(){imProcTree.Show();}
00122
00123 template<class T>
00124 int FindName(const vector<T> &vect,const string &name)
00125 {
00126 for(uint i=0;i<vect.size();i++)
00127 {
00128 if(vect[i].name==name){return i;}
00129 }
00130 return -1;
00131 }
00133 XIPD::ArgumentType *GetArgumentType(const string &name,bool fail=true)
00134 {
00135 int pos=FindName(argumentTypes,name);
00136 if(fail && pos<0){ThrowError("ImageProcessorsDescription::GetArgumentType could not find \"%s\"",name);}
00137 return pos>=0 ? &argumentTypes[pos] : NULL;
00138 }
00140 XIPD::TemplateGroup *GetTemplateGroup(const string &name,bool fail=true)
00141 {
00142 int pos=FindName(templateGroups,name);
00143 if(fail && pos<0){ThrowError("ImageProcessorsDescription::GetTemplateGroup could not find \"%s\"",name);}
00144 return pos>=0 ? &templateGroups[pos] : NULL;
00145 }
00147 XIPD::ImageProcessor *GetImageProcessor(const string &name,bool fail=true)
00148 {
00149 int pos=FindName(imageProcessors,name);
00150 if(fail && pos<0){ThrowError("ImageProcessorsDescription::GetImageProcessor could not find \"%s\"",name);}
00151 return pos>=0 ? &imageProcessors[pos] : NULL;
00152 }
00153 public:
00155 vector<string> GetNames(bool imProcsOnly=false)
00156 {
00157 vector<string> res;
00158 for(uint i=0;i<imageProcessors.size();i++)
00159 {
00160 res.push_back(imageProcessors[i].name);
00161 }
00162 if(!imProcsOnly)
00163 {
00164 for(uint i=0;i<imageProcessorNodes.size();i++)
00165 {
00166 res.push_back(imageProcessorNodes[i].name);
00167 }
00168 }
00169 return res;
00170 }
00171
00172 public:
00174 void Pad(vector<string> &s)
00175 {
00176 size_t mxsize=0;
00177 for(size_t i=0;i<s.size();i++){mxsize=std::max(mxsize,s[i].size());}
00178 for(size_t i=0;i<s.size();i++){s[i].insert(s[i].size(), mxsize-s[i].size(), ' ');}
00179 }
00181 void Print(const string &s,uint margin,int width)
00182 {
00183 int linepos=0;
00184 string line="";
00185 for(uint i=0;i<s.size();i++)
00186 {
00187 line+=(s[i]!='\n' ? s[i] : ' ');
00188
00189 if(linepos>=width || i==s.size()-1 || s[i]=='\n')
00190 {
00191
00192 if(i!=s.size()-1)
00193 {
00194 uint j;
00195 for(j=i;j>0 && (s[j]!=' ' && s[j]!='\n');j--);
00196 line=line.substr(0,line.size()-(i-j));
00197 i=j;
00198 }
00199
00200 for(uint j=0;j<margin;j++){printf(" ");}
00201 printf("%s\n",line.c_str());
00202 linepos=0;
00203 line="";
00204 }
00205 linepos++;
00206 }
00207 }
00208 public:
00210
00212
00213 public:
00214 void Merge(XIPD::ImageProcessorsDescription &other);
00215
00216 public:
00218 void ReadFromString(const string &description)
00219 {
00220 xmlpp::DomParser parser;
00221 parser.parse_memory(description);
00222
00223 XIPD::ImageProcessorsDescription tmp(parser.get_document()->get_root_node());
00224 *(XIPD::ImageProcessorsDescription *)this=tmp;
00225
00226
00227
00228
00229
00230 }
00232 void ReadFromFile(const string &fname)
00233 {
00234 XIPD::ImageProcessorsDescription tmp(xmlpp::DomParser(fname).get_document()->get_root_node());
00235 *(XIPD::ImageProcessorsDescription *)this=tmp;
00236
00237
00238 }
00239
00240
00241
00243 virtual void Init(){CreateTree();}
00244 virtual ~ImageProcessorsDescription(){;}
00245 ImageProcessorsDescription(){;}
00246 ImageProcessorsDescription(const string& descr):
00247 XIPD::ImageProcessorsDescription(xmlpp::DomParser(descr).get_document()->get_root_node())
00248 {
00249
00250
00251
00252
00253 }
00254 };
00255
00256
00257 #endif // _ImageProcessorsDescription_hpp