Go to the documentation of this file.00001 #ifndef Fireworks_Core_FWItemRandomAccessor_h
00002 #define Fireworks_Core_FWItemRandomAccessor_h
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "Reflex/Object.h"
00015
00016
00017 #include "Fireworks/Core/interface/FWItemAccessorBase.h"
00018 #include "Fireworks/Core/interface/FWItemAccessorRegistry.h"
00019
00020
00021
00029 class FWItemRandomAccessorBase : public FWItemAccessorBase
00030 {
00031 public:
00032 virtual ~FWItemRandomAccessorBase();
00033
00034 const void* data() const;
00035 const TClass* type() const;
00036 const TClass* modelType() const;
00037
00038 bool isCollection() const;
00039
00040 void setData(const Reflex::Object&);
00041 virtual void reset();
00042
00043 protected:
00044 void *getDataPtr() const;
00045 FWItemRandomAccessorBase(const TClass *type, const type_info &modelTypeName);
00046 const TClass* m_type;
00047 const TClass* m_modelType;
00048 mutable void* m_data;
00049
00050 private:
00051 FWItemRandomAccessorBase(const FWItemRandomAccessorBase&);
00052
00053 const FWItemRandomAccessorBase& operator=(const FWItemRandomAccessorBase&);
00054 };
00055
00073 template <class C, class V = typename C::value_type>
00074 class FWItemRandomAccessor : public FWItemRandomAccessorBase
00075 {
00076 typedef C container_type;
00077 typedef V container_value_type;
00078 public:
00079 FWItemRandomAccessor(const TClass *iClass)
00080 : FWItemRandomAccessorBase(iClass, typeid(container_value_type))
00081 {}
00082
00083 REGISTER_FWITEMACCESSOR_METHODS();
00084
00085
00086 const void* modelData(int iIndex) const
00087 {
00088 if (!getDataPtr())
00089 return 0;
00090 return &(reinterpret_cast<container_type *>(getDataPtr())->operator[](iIndex));
00091 }
00092
00093 unsigned int size() const
00094 {
00095 if (!getDataPtr())
00096 return 0;
00097 return reinterpret_cast<const container_type*>(getDataPtr())->size();
00098 }
00099 };
00100
00108 template <class C, class COLL = typename C::value_type, class V = typename COLL::value_type >
00109 class FWItemDetSetAccessor : public FWItemRandomAccessorBase
00110 {
00111 public:
00112 typedef C container_type;
00113 typedef COLL collection_type;
00114 typedef V collection_value_type;
00115
00116 FWItemDetSetAccessor(const TClass *iClass)
00117 : FWItemRandomAccessorBase(iClass, typeid(collection_value_type))
00118 {}
00119
00120 REGISTER_FWITEMACCESSOR_METHODS();
00121
00122 const void* modelData(int iIndex) const
00123 {
00124 if (!getDataPtr())
00125 return 0;
00126 const container_type *c = reinterpret_cast<const container_type*>(getDataPtr());
00127 size_t collectionOffset = 0;
00128 for (typename container_type::const_iterator ci = c->begin(), ce = c->end(); ci != ce; ++ci)
00129 {
00130 size_t i = iIndex - collectionOffset;
00131 if (i < ci->size())
00132 return &(ci->operator[](i));
00133 collectionOffset += ci->size();
00134 }
00135
00136 return 0;
00137 }
00138
00139 unsigned int size() const
00140 {
00141 if (!getDataPtr())
00142 return 0;
00143 const container_type *c = reinterpret_cast<const container_type*>(getDataPtr());
00144 size_t finalSize = 0;
00145
00146 for (typename container_type::const_iterator i = c->begin(), e = c->end(); i != e; ++i)
00147 finalSize += i->size();
00148
00149 return finalSize;
00150 }
00151 };
00152
00155 template <class C, class COLL = typename C::value_type, class V = typename COLL::value_type >
00156 class FWItemNewDetSetAccessor : public FWItemRandomAccessorBase
00157 {
00158 public:
00159 typedef C container_type;
00160 typedef COLL collection_type;
00161 typedef V collection_value_type;
00162
00163 FWItemNewDetSetAccessor(const TClass *iClass)
00164 : FWItemRandomAccessorBase(iClass, typeid(collection_value_type))
00165 {}
00166
00167 REGISTER_FWITEMACCESSOR_METHODS();
00168
00169 const void* modelData(int iIndex) const
00170 {
00171 if (!getDataPtr())
00172 return 0;
00173 const container_type *c = reinterpret_cast<const container_type*>(getDataPtr());
00174 if (iIndex < 0)
00175 return 0;
00176
00177 return &(c->data().operator[](iIndex));
00178 }
00179
00180 unsigned int size() const
00181 {
00182 if (!getDataPtr())
00183 return 0;
00184 const container_type *c = reinterpret_cast<const container_type*>(getDataPtr());
00185 return c->dataSize();
00186 }
00187 };
00188
00189 template <class C, class R = typename C::Range, class V = typename R::value_type>
00190 class FWItemRangeAccessor : public FWItemRandomAccessorBase
00191 {
00192 public:
00193 typedef C container_type;
00194 typedef R range_type;
00195 typedef V value_type;
00196
00197 FWItemRangeAccessor(const TClass *iClass)
00198 : FWItemRandomAccessorBase(iClass, typeid(value_type))
00199 {}
00200
00201 REGISTER_FWITEMACCESSOR_METHODS();
00202
00203 const void* modelData(int iIndex) const
00204 {
00205 if (!getDataPtr())
00206 return 0;
00207 const container_type *c = reinterpret_cast<const container_type*>(getDataPtr());
00208 size_t collectionOffset = 0;
00209 for (typename container_type::const_iterator ci = c->begin(), ce = c->end(); ci != ce; ++ci)
00210 {
00211 size_t i = iIndex - collectionOffset;
00212 if (i < std::distance(ci->first, ci->second))
00213 return &(*(ci + i));
00214 collectionOffset += ci->size();
00215 }
00216
00217 return 0;
00218 }
00219
00220 unsigned int size() const
00221 {
00222 if (!getDataPtr())
00223 return 0;
00224 const container_type *c = reinterpret_cast<const container_type*>(getDataPtr());
00225 size_t finalSize = 0;
00226
00227 for (typename range_type::const_iterator ci = c->begin(), ce = c->end(); ci != ce; ++ci)
00228 finalSize += std::distance(ci->first, ci->second);
00229
00230 return finalSize;
00231 }
00232 };
00233
00234 template <class C, class V>
00235 class FWItemMuonDigiAccessor : public FWItemRandomAccessorBase
00236 {
00237 public:
00238 typedef C container_type;
00239 typedef V value_type;
00240
00241 FWItemMuonDigiAccessor(const TClass *iClass)
00242 : FWItemRandomAccessorBase(iClass, typeid(value_type))
00243 {}
00244
00245 REGISTER_FWITEMACCESSOR_METHODS();
00246
00247 const void* modelData(int iIndex) const
00248 {
00249 if (!getDataPtr())
00250 return 0;
00251 const container_type *c = reinterpret_cast<const container_type*>(getDataPtr());
00252 size_t collectionOffset = 0;
00253
00254 for (typename container_type::DigiRangeIterator ci = c->begin(), ce = c->end(); ci != ce; ++ci)
00255 {
00256 int i = iIndex - collectionOffset;
00257
00258 typename container_type::DigiRangeIterator::value_type vt = *ci;
00259
00260
00261 if (i < std::distance(vt.second.first, vt.second.second))
00262 return &(*(vt.second.first + i));
00263 collectionOffset += std::distance(vt.second.first, vt.second.second);
00264 }
00265
00266 return 0;
00267 }
00268
00269 unsigned int size() const
00270 {
00271 if (!getDataPtr())
00272 return 0;
00273 const container_type *c = reinterpret_cast<const container_type*>(getDataPtr());
00274 size_t finalSize = 0;
00275
00276 for (typename container_type::DigiRangeIterator ci = c->begin(), ce = c->end(); ci != ce; ++ci)
00277 {
00278 typename container_type::DigiRangeIterator::value_type vt = *ci;
00279 finalSize += std::distance(vt.second.first, vt.second.second);
00280 }
00281
00282 return finalSize;
00283 }
00284 };
00285
00286 #endif