Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/Collection.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef Collection_INCLUDE_ONCE
00033 #define Collection_INCLUDE_ONCE
00034 
00035 #include <vlCore/Object.hpp>
00036 #include <vector>
00037 #include <algorithm>
00038 
00039 namespace vl
00040 {
00041   //------------------------------------------------------------------------------
00042   // Collection
00043   //------------------------------------------------------------------------------
00046   template <typename T>
00047   class Collection: public Object
00048   {
00049     VL_INSTRUMENT_CLASS(vl::Collection<T>, Object)
00050 
00051   public:
00052     Collection(const std::vector< ref<T> >& vector)
00053     {
00054       VL_DEBUG_SET_OBJECT_NAME()
00055       mVector = vector;
00056     }
00057     
00058     Collection()
00059     {
00060       VL_DEBUG_SET_OBJECT_NAME()
00061     }
00062     
00063     Collection& operator=(const std::vector< ref<T> >& vector)
00064     {
00065       mVector = vector;
00066       return *this;
00067     }
00068 
00069     operator std::vector< ref<T> >() const
00070     {
00071       return mVector;
00072     }
00073 
00074     void push_back( T* data ) { mVector.push_back(data); }
00075     
00076     void pop_back() { mVector.pop_back(); }
00077     
00078     void resize(int size) { mVector.resize(size); }
00079     
00080     int size() const { return (int)mVector.size(); }
00081     
00082     bool empty() const { return mVector.empty(); }
00083     
00084     void clear() { mVector.clear(); }
00085     
00086     const T* back() const { return mVector.back().get(); }
00087     
00088     T* back() { return mVector.back().get(); }
00089     
00090     void reserve(int capacity) { mVector.reserve(capacity); }
00091     
00092     int capacity() const { return (int)mVector.capacity(); }
00093     
00094     const ref<T>& operator[](int i) const { return mVector[i]; }
00095     
00096     ref<T>& operator[](int i) { return mVector[i]; }
00097     
00098     const T* at(int i) const { return mVector[i].get(); }
00099     
00100     T* at(int i) { return mVector[i].get(); }
00101     
00102     void swap(Collection& other) { mVector.swap(other.mVector); }
00103 
00104     // added functionalities
00105 
00106     void sort()
00107     {
00108       std::sort(mVector.begin(), mVector.end(), less);
00109     }
00110 
00111     int find(T* obj) const
00112     {
00113       typename std::vector< ref<T> >::const_iterator pos = std::find(mVector.begin(), mVector.end(), obj);
00114       if (pos == mVector.end())
00115         return -1;
00116       else
00117         return (int)(pos - mVector.begin());
00118     }
00119     
00120     void shrink()
00121     {
00122       Collection<T>(mVector).swap(mVector);
00123     }
00124     
00125     void push_back(const Collection<T>& objs )
00126     {
00127       mVector.insert(mVector.end(), objs.mVector.begin(), objs.mVector.end());
00128     }
00129     
00130     void insert(int start, const Collection<T>& objs )
00131     {
00132       mVector.insert(mVector.begin()+start, objs.mVector.begin(), objs.mVector.end());
00133     }
00134     
00135     void set(const Collection<T>& objs)
00136     {
00137       mVector = objs.mVector;
00138     }
00139     
00140     void erase(int start, int count)
00141     {
00142       mVector.erase(mVector.begin()+start, mVector.begin()+start+count);
00143     }
00144     
00145     void set(int index, T* obj) { mVector[index] = obj; }
00146     
00147     void insert(int index, T* obj) { mVector.insert(mVector.begin() + index, obj); }
00148     
00149     void erase(const T* data)
00150     {
00151       typename std::vector< ref<T> >::iterator it = std::find(mVector.begin(), mVector.end(), data);
00152       if (it != mVector.end())
00153         mVector.erase(it);
00154     
00155     }
00156 
00157     void eraseAt(int index) { mVector.erase(mVector.begin()+index); }
00158 
00159     const std::vector< ref<T> >& vector() const { return mVector; }
00160     
00161     std::vector< ref<T> >& vector() { return mVector; }
00162 
00163   protected:
00164     bool static less(const ref<T>& a, const ref<T>& b) { return *a < *b; }
00165 
00166   protected:
00167     std::vector< ref<T> > mVector;
00168   };
00169   //-----------------------------------------------------------------------------
00170 }
00171 
00172 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:28.
Permission is granted to use this page to write and publish articles regarding Visualization Library.