Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

ImageProcessorsDescription.hpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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             // check for end of line
00189             if(linepos>=width || i==s.size()-1 || s[i]=='\n')
00190             {
00191                 // if not end of string then wrap
00192                 if(i!=s.size()-1)
00193                 {
00194                     uint j;
00195                     for(j=i;j>0 && (s[j]!=' ' && s[j]!='\n');j--);//find previous ' '
00196                     line=line.substr(0,line.size()-(i-j));// remove wraped word from line
00197                     i=j;// reset for next line
00198                 }               
00199                 // output margin and line
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 //  void PrintDoxyDoc(const string& name,const string& type="class head");
00212 //  void PrintHelp(const string &name);
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 //      XMLTree tmpTree;
00227 //      tmpTree.read_buffer(description);
00228 //      XIPD::ImageProcessorsDescription tmp(tmpTree.root());
00229 //      *(XIPD::ImageProcessorsDescription *)this=tmp;
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 //      XIPD::ImageProcessorsDescription tmp(XMLTree(fname).root());
00237 //      *(XIPD::ImageProcessorsDescription *)this=tmp;
00238     }
00239 //      {
00240 //          if(!tree.read(fname)){ThrowError("failed reading %s",fname);}
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 //           tree;
00250 //          tree.read_buffer(descr);
00251 //          XIPD::ImageProcessorsDescription tmp(tree.root);
00252 //      *this=tmp;
00253     }
00254 };
00255 
00256 
00257 #endif // _ImageProcessorsDescription_hpp

Generated on Fri Jun 17 13:36:04 2005 for ImLib3D by  doxygen 1.4.2