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
00028
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
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
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