inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_sg_cbks 00005 #define inlib_sg_cbks 00006 00007 #include "cbk" 00008 00009 #include <vector> 00010 00011 namespace inlib { 00012 namespace sg { 00013 00014 class cbks { 00015 public: 00016 static const std::string& s_class() { 00017 static const std::string s_v("inlib::sg::cbks"); 00018 return s_v; 00019 } 00020 public: 00021 virtual void* cast(const std::string& a_class) const { 00022 if(void* p = inlib::cmp_cast<cbks>(this,a_class)) {return p;} 00023 return 0; 00024 } 00025 public: 00026 cbks() {} 00027 virtual ~cbks(){clear_cbks();} 00028 public: 00029 cbks(const cbks& a_from){copy_cbks(a_from);} 00030 cbks& operator=(const cbks& a_from){ 00031 copy_cbks(a_from); 00032 return *this; 00033 } 00034 public: 00035 void add_callback(inlib::sg::cbk* a_cbk) { 00036 //we take ownership of a_cbk 00037 m_cbks.push_back(a_cbk); 00038 } 00039 void copy_cbks(const cbks& a_from,bool a_clear = true) { 00040 if(a_clear) clear_cbks(); 00041 std::vector<inlib::sg::cbk*>::const_iterator it; 00042 for(it=a_from.m_cbks.begin();it!=a_from.m_cbks.end();++it) { 00043 m_cbks.push_back((*it)->copy()); 00044 } 00045 } 00046 void clear_cbks() { 00047 std::vector<inlib::sg::cbk*>::iterator it; 00048 for(it=m_cbks.begin();it!=m_cbks.end();) { 00049 inlib::sg::cbk* cbk = *it; 00050 it = m_cbks.erase(it); 00051 delete cbk; 00052 } 00053 m_cbks.clear(); 00054 } 00055 00056 const std::vector<inlib::sg::cbk*>& callbacks() const {return m_cbks;} 00057 protected: 00058 std::vector<inlib::sg::cbk*> m_cbks; 00059 }; 00060 00061 }} 00062 00063 #endif