#ifndef _utl_MultiObject_h_ #define _utl_MultiObject_h_ // $Id$ #include #include namespace utl { /*! \class LabeledObject MultiObject.h utl/MultiObject.h \brief Class for attaching a label to an object \author Darko Veberic \version $Id$ \ingroup stl */ template class LabeledObject { public: LabeledObject(const int label, T& obj) : fLabel(label), fObject(&obj) { } LabeledObject(const std::pair pair) : fLabel(pair.first), fObject(pair.second) { } /*LabeledObject(const LabeledObject& lobj) : fLabel(lobj.fLabel), fObject(lobj.fObject) { }*/ /*LabeledObject& operator=(const Labeledobject& lobj) { fLabel = lobj.fLabel; fObject = lobj.fObject; return *this; }*/ /// Return the label for the object /*! The label typically specifies the what signal component a utl::Trace or utl::TimeDistribution corresponds to */ int GetLabel() const { return fLabel; } bool operator==(const LabeledObject& lobj) const { return fLabel == lobj.fLabel && *fObject == *lobj.fObject; } bool operator!=(const LabeledObject& lobj) const { return !operator==(lobj); } protected: T& GetObject() { return *fObject; } const T& GetObject() const { return *fObject; } private: int fLabel; T* fObject; }; namespace detail { template struct LabeledObjectFunctor { LabeledObjectType operator()(const typename std::map::value_type& pair) const { return LabeledObjectType(pair); } }; } /*! \class MultiObject MultiObject.h utl/MultiObject.h \brief container for object and associated labels \author Darko Veberic \version $Id$ \ingroup stl */ template class MultiObject { public: typedef typename std::map MultiObjectContainer; typedef typename utl::detail::LabeledObjectFunctor LabeledObjectFunctor; typedef boost::transform_iterator Iterator; typedef boost::transform_iterator ConstIterator; Iterator Begin() { return Iterator(fObjects.begin()); } ConstIterator Begin() const { return ConstIterator(fObjects.begin()); } Iterator End() { return Iterator(fObjects.end()); } ConstIterator End() const { return ConstIterator(fObjects.end()); } unsigned int GetNLabels() const { return fObjects.size(); } bool HasLabel(const int label) const { return fObjects.find(label) != fObjects.end(); } bool operator==(const MultiObject& mobj) const; bool operator!=(const MultiObject& mobj) const { return !operator==(mobj); } void Clear(); protected: MultiObject() { } MultiObject(const MultiObject& mobj) { *this = mobj; } MultiObject& operator=(const MultiObject& mobj); ~MultiObject(); T& GetObject(const int label = 0); const T& GetObject(const int label = 0) const; /// add with deep copy of the object void AddObject(const T& obj, const int label) { AddObject(new T(obj), label); } /// add that takes the ownership void AddObject(T* const obj, const int label); void RemoveObject(const int label); private: typedef typename MultiObjectContainer::iterator InternalIterator; typedef typename MultiObjectContainer::const_iterator InternalConstIterator; static std::string GetObjectName(); MultiObjectContainer fObjects; }; } #endif // Configure (x)emacs for this file ... // Local Variables: // mode: c++ // End: