pdf_collection.h
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #ifndef PDF_COLLECTION_H_
00028 #define PDF_COLLECTION_H_
00029 
00030 #include "pdf_define.h"
00031 
00032 namespace pdf
00033 {
00034 
00035 class PDFCollectionBase
00036 {
00037 public:
00038     PDFCollectionBase(){}
00039     virtual ~PDFCollectionBase(){}
00040 
00041 public:
00042     virtual bool get_first_element(void **data_ptr) = 0;
00043     virtual int  get_count() = 0;
00044 };
00045 
00046 template<class T>
00047 class PDFInstanceCollection : public PDFCollectionBase
00048 {
00049 public:
00050     PDFInstanceCollection(): vect() {}
00051     virtual ~PDFInstanceCollection() {clear();}
00052 
00053     virtual bool get_first_element(void **data_ptr)
00054     {
00055         T &t = front();
00056         
00057         *data_ptr = static_cast<void *>(&t);
00058         return true;
00059     }
00060 
00061     virtual int get_count()
00062     {
00063         return static_cast<int>(vect.size());
00064     }
00065 
00066     void add(T t)
00067     {
00068         vect.push_back(t);
00069     }
00070 
00071     void clear()
00072     {
00073         vect.clear();
00074     }
00075 
00076     T& get(int num)
00077     {
00078         assert(num >= 0 && num < static_cast<int>(vect.size()));
00079 
00080         return vect[num];
00081     }
00082 
00083     T& front()
00084     {
00085         return vect[0];
00086     }
00087 
00088     T& back()
00089     {
00090         size_t len = vect.size();
00091         return vect[len - 1];
00092     }
00093 
00094 private:
00095     typedef std::vector<T> Vect;
00096     typedef typename Vect::iterator VectIter;
00097 
00098 private:
00099     Vect vect;
00100 };
00101 
00102 template<class T>
00103 class PDFCollection : public PDFCollectionBase
00104 {
00105 public:
00106     PDFCollection(): vect() {}
00107     virtual ~PDFCollection() {clear();}
00108 
00109     virtual bool get_first_element(void **data_ptr)
00110     {
00111         if (size())
00112         {
00113             *data_ptr = static_cast<void *>(&vect[0]);
00114             return true;
00115         }
00116         return false;
00117     }
00118 
00119     virtual int get_count()
00120     {
00121         return size();
00122     }
00123 
00124     void add(T t)
00125     {
00126         vect.push_back(t);
00127     }
00128 
00129     void clear()
00130     {
00131         VectIter begin = vect.begin();
00132         VectIter end   = vect.end();
00133         VectIter iter  = begin;
00134         for(; iter != end; ++iter)
00135         {
00136             delete *iter;
00137         }
00138         vect.clear();
00139     }
00140 
00141     T get(int num)
00142     {
00143         assert(num >= 0 && num < static_cast<int>(vect.size()));
00144 
00145         return vect[num];
00146     }
00147 
00148     T front()
00149     {
00150         if (size() > 0)
00151         {
00152             return vect[0];
00153         }
00154         return 0;
00155     }
00156 
00157     T back()
00158     {
00159         if (size() > 0)
00160         {
00161             return vect[size()-1];
00162         }
00163         return 0;
00164     }
00165 
00166     int size()
00167     {
00168         return static_cast<int>(vect.size());
00169     }
00170 
00171 private:
00172     typedef std::vector<T> Vect;
00173     typedef typename Vect::iterator VectIter;
00174 
00175 private:
00176     Vect vect;
00177 };
00178 
00179 template <class T, class E>
00180 class PDFElemCollection : public PDFCollection<T>
00181 {
00182 public:
00183     PDFElemCollection()
00184         : PDFCollection<T>()
00185     {}
00186     virtual ~PDFElemCollection() {}
00187 
00188     void set_element(E e) {element = e;}
00189 
00190     E& get_element() {return element;}
00191 
00192 private:
00193     E element;
00194 };
00195 
00196 typedef PDFCollection<PluginRangeImpl*> PDFRangeCollection;
00197 
00198 };
00199 
00200 #endif //PDF_COLLECTION_H_
00201