pdf_collection.h

Go to the documentation of this file.
00001 /*
00002  * File Name: pdf_collection.h
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-pdf.
00007  *
00008  * uds-plugin-pdf is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-pdf is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
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 };//namespace pdf
00199 
00200 #endif //PDF_COLLECTION_H_
00201 
Generated by  doxygen 1.6.2-20100208