inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/sg/node
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_sg_node
00005 #define inlib_sg_node
00006 
00007 #include "../scast"
00008 #include "field"
00009 #include "search_action"
00010 #include "write_action"
00011 
00012 #ifdef INLIB_MEM
00013 #include "../mem"
00014 #endif
00015 
00016 #include <vector>
00017 #include <string>
00018 
00019 namespace inlib {
00020 namespace sg {
00021 class render_action;
00022 class pick_action;
00023 class bbox_action;
00024 class event_action;
00025 }}
00026 
00027 namespace inlib {
00028 namespace sg {
00029 
00030 class node {
00031 public:
00032   static const std::string& s_class() {
00033     static const std::string s_v("inlib::sg::node");
00034     return s_v;
00035   }
00036   virtual void* cast(const std::string& a_class) const {
00037     if(void* p = inlib::cmp_cast<node>(this,a_class)) {return p;}
00038     else return 0;
00039   }
00040 public:
00041   virtual node* copy() const = 0;
00042   virtual const std::string& s_cls() const = 0;
00043 
00044   virtual void render(render_action&) {}
00045   virtual void pick(pick_action&) {}
00046   virtual void bbox(bbox_action&) {}
00047   virtual void search(search_action& a_action) {
00048     if(void* p = cast(a_action.sclass())) {
00049       a_action.add_obj(p);
00050       //FIXME : if not ALL : a_action.set_done(true);
00051     }
00052   }
00053   virtual bool write(write_action& a_action) {
00054     if(!a_action.beg_node(*this)) return false;
00055     if(!write_fields(a_action)) return false;
00056     if(!a_action.end_node(*this)) return false;
00057     return true;
00058   }
00059   virtual void event(event_action&) {}
00060 public:
00061   node(){
00062 #ifdef INLIB_MEM
00063     mem::increment(s_class().c_str());
00064 #endif
00065   }
00066   virtual ~node(){
00067 #ifdef INLIB_MEM
00068     mem::decrement(s_class().c_str());
00069 #endif
00070   }
00071 protected:
00072   node(const node&){
00073 #ifdef INLIB_MEM
00074     mem::increment(s_class().c_str());
00075 #endif
00076   }
00077   node& operator=(const node&){return *this;}
00078 protected:
00079   void add_field(field* a_field) {
00080     m_fields.push_back(a_field); //it does not take ownerhship.
00081   }
00082   bool touched() {
00083     std::vector<field*>::iterator it;
00084     for(it=m_fields.begin();it!=m_fields.end();++it) {
00085       if((*it)->touched()) return true;
00086     }
00087     return false;
00088   }
00089   bool write_fields(write_action& a_action) {
00090     unsigned int index = 0;
00091     std::vector<field*>::iterator it;
00092     for(it=m_fields.begin();it!=m_fields.end();++it,index++) {
00093       if(!(*it)->write(a_action.buffer())) {
00094         a_action.out() << "inlib::sg::node::write_fields :"
00095                        << " field.write() failed"
00096                        << " for field index " << index
00097                        << " of node class " << s_cls()
00098                        << "."
00099                        << std::endl;
00100         return false;
00101       }
00102     }
00103     return true;
00104   }
00105 public:
00106   void reset_touched() {
00107     std::vector<field*>::iterator it;
00108     for(it=m_fields.begin();it!=m_fields.end();++it) {
00109       (*it)->reset_touched();
00110     }
00111   }
00112 /*
00113   bool equal(const node& a_node) const {
00114     if(m_fields.size()!=a_node.m_fields.size()) return false;
00115     std::vector<field*>::iterator it = m_fields.begin();
00116     std::vector<field*>::iterator ait = a_node.m_fields.begin();
00117     for(;it!=m_fields.end();++it,++ait) {
00118       if(!(*it)->equal(*(*ait))) return false;
00119     }
00120     return true;
00121   }
00122 */
00123   std::vector<field*>& fields() {return m_fields;}
00124   const std::vector<field*>& fields() const {return m_fields;}
00125 private:
00126   std::vector<field*> m_fields;
00127 };
00128 
00129 }}
00130 
00131 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines