SketchPool.h
Go to the documentation of this file.00001
00002 #ifndef INCLUDED_SketchPool_h
00003 #define INCLUDED_SketchPool_h
00004
00005 #include <vector>
00006 #include <iostream>
00007 #include <sstream>
00008
00009 #include "SketchPoolRoot.h"
00010
00011 namespace DualCoding {
00012
00013 class SketchSpace;
00014 class SketchDataRoot;
00015 template<class T> class SketchData;
00016
00017
00018
00019 template<typename T>
00020 class SketchPool : public SketchPoolRoot {
00021 public:
00022
00023 std::vector<SketchData<T>*> elements;
00024
00025
00026 SketchPool<T>(SketchSpace *_space, const std::string& _name, int poolsize = 0);
00027
00028
00029 ~SketchPool<T>();
00030
00031
00032 void deleteElements();
00033
00034
00035 void clear(bool clearRetained);
00036
00037 SketchData<T>* getFreeElement(void);
00038
00039 SketchData<T>* findSketchData(const std::string &name);
00040
00041
00042 std::string getSketchListForGUI();
00043
00044
00045 SketchDataRoot* retrieveSketch(int id);
00046
00047 void dumpPool() const;
00048
00049 private:
00050
00051 typedef typename std::vector<SketchData<T>*>::const_iterator CI;
00052
00053 SketchPool(const SketchPool&);
00054 SketchPool& operator=(const SketchPool&);
00055 };
00056
00057
00058
00059 template <class T>
00060 SketchPool<T>::SketchPool(SketchSpace *_space, const std::string& _name, int poolsize) :
00061 SketchPoolRoot(_space,_name),
00062 elements(std::vector<SketchData<T>*>(poolsize))
00063 {
00064 for (int i=0; i<poolsize; i++) {
00065 elements[i] = new SketchData<T>(space);
00066 };
00067 }
00068
00069 template <class T>
00070 SketchPool<T>::~SketchPool() {
00071 deleteElements();
00072 }
00073
00074 template <class T>
00075 void SketchPool<T>::deleteElements() {
00076 for (unsigned int i = 0; i < elements.size(); i++)
00077 if(elements[i]->refcount > 0)
00078 printf("ERROR in ~SketchPool<T>: Element %d [%p] has ref_count == %d != 0\n",
00079 i,elements[i],elements[i]->refcount);
00080 else
00081 delete elements[i];
00082 elements.clear();
00083 }
00084
00085 template <class T>
00086 void SketchPool<T>::clear(bool clearRetained) {
00087 for (CI it = elements.begin(); it != elements.end(); it++ ) {
00088
00089
00090
00091
00092
00093
00094
00095
00096 if ( clearRetained )
00097 (*it)->retain(false);
00098 if ( !(*it)->retained )
00099 (*it)->setViewable(false);
00100 if ( (*it)->refreshTag < getRefreshCounter() )
00101 (*it)->clearPending = false;
00102 else
00103 (*it)->clearPending = true;
00104 }
00105 }
00106
00107 template <class T>
00108 SketchData<T>* SketchPool<T>::getFreeElement(void)
00109 {
00110 for (CI it = elements.begin(); it != elements.end(); it++ ) {
00111 if ( (*it)->refcount == 0 && !(*it)->retained &&
00112 (*it)->viewable == false && (*it)->clearPending == false )
00113 return *it;
00114 else if ( (*it)->refcount < 0 )
00115 std::cerr << "PROBLEM: negative refcount" << std::endl;
00116 };
00117 SketchData<T>* res = new SketchData<T>(space);
00118 elements.push_back(res);
00119 return res;
00120 }
00121
00122 template<class T>
00123 SketchData<T>* SketchPool<T>::findSketchData(const std::string &sketchname) {
00124 for (CI it = elements.begin(); it != elements.end(); it++ )
00125 if ( ((*it)->refcount > 0 || (*it)->retained || (*it)->viewable) && (*it)->name == sketchname )
00126 return *it;
00127 return NULL;
00128 }
00129
00130
00131 template<class T>
00132 std::string SketchPool<T>::getSketchListForGUI()
00133 {
00134 std::ostringstream liststream;
00135 for ( unsigned int i = 0; i < elements.size(); i++ ) {
00136 if ( elements[i]->clearPending ) {
00137 elements[i]->setViewable(false);
00138 elements[i]->clearPending = false;
00139 }
00140 else if ( elements[i]->isViewable() ) {
00141 elements[i]->refreshTag = getRefreshCounter();
00142 liststream << "sketch" << std::endl;
00143 liststream << "id: " << (elements[i])->id << std::endl;
00144 liststream << "parentId: " << (elements[i])->parentId << std::endl;
00145 liststream << "name: " << (elements[i])->name << std::endl;
00146 liststream << "sketchtype: " << elements[i]->getType() << std::endl;
00147 liststream << "color: " << toString(elements[i]->color) << std::endl;
00148 liststream << "colormap: " << elements[i]->colormap << std::endl;
00149 }
00150 }
00151 return liststream.str();
00152 }
00153
00154 template <class T>
00155 SketchDataRoot* SketchPool<T>::retrieveSketch(int id)
00156 {
00157 for(unsigned int i = 0; i < elements.size(); i++)
00158 if(elements[i]->id == id)
00159 return elements[i];
00160 return NULL;
00161 }
00162
00163 template <class T>
00164 void SketchPool<T>::dumpPool() const {
00165 printf("%4s %2s %4s %5s %3s %2s (%s)\n",
00166 "num", "rf", "id", "pid","vis","cp",
00167 (getSpaceName()+"."+getName()).c_str());
00168 for(unsigned int i = 0; i < elements.size(); i++)
00169 printf("%4d %2d %4d %5d %1c %1c %s\n",
00170 i,
00171 (elements[i])->refcount,
00172 (elements[i])->id,
00173 (elements[i])->parentId,
00174 (elements[i])->viewable ? 'y' : 'n',
00175 (elements[i])->clearPending ? 'y' : 'n',
00176 (elements[i])->name.c_str());
00177 }
00178
00179 }
00180
00181 #endif