torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Composite.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef TORC_GENERIC_COMPOSITE_HPP
17 #define TORC_GENERIC_COMPOSITE_HPP
18 
19 #include <cstddef>
20 #include <vector>
21 
22 //BOOST
23 #include <boost/shared_ptr.hpp>
24 #include <boost/weak_ptr.hpp>
25 
26 #ifdef GENOM_SERIALIZATION
27 #include <boost/serialization/access.hpp>
28 #include <boost/serialization/base_object.hpp>
29 #include <boost/serialization/weak_ptr.hpp>
30 #endif //GENOM_SERIALIZATION
32 #include "torc/generic/Error.hpp"
34 
35 namespace torc {
36 namespace generic {
37 
38 /**
39  * @brief Interface for objects that can be composed within each other
40  *
41  * The Composite class defines an interface for objects that can occur singly (Scalar), as Arrays
42  * (Vector) or bundles (Bundle). This class defines polymorphic methods that will be overridden by
43  * subsequent implementations.
44  */
45 template <typename _Type> class Composite : public SelfReferencing<_Type> {
46 #ifdef GENOM_SERIALIZATION
47  friend class boost::serialization::access;
48 #endif
49 public:
50  typedef _Type Type;
51 
52  /**
53  * Shared Pointer to object of type Type
54  */
55  typedef boost::shared_ptr<Type> Pointer;
56  typedef boost::weak_ptr<Type> WeakPointer;
57 
58  /**
59  * List of Shared Pointers to objects of type Type
60  */
61  typedef std::vector<Pointer> List;
62  typedef size_t SizeType;
63 
64 protected:
65  virtual ~Composite() throw () {
66  }
67  ;
68 
69 public:
70  Composite();
71 
72  /**
73  * Get the type of this composition.
74  *
75  * @return Composition type for this object
76  */
77  virtual CompositionType getCompositionType() const = 0;
78 
79  /**
80  * Get the total number of bits of the composition
81  * @return Number of bits
82  */
83  virtual size_t getSize() const = 0;
84 
85  /**
86  * Get children of this composition.
87  *
88  * @note This is relevant for Vector and Bundle compositions only. Other compositions return a
89  * NULL pointer
90  *
91  * @param[out] outChildren A list of all children for this composition
92  */
93  virtual void getChildren(List& outChildren) const throw (Error) = 0;
94 
95  /**
96  * Get a specific member of this composition.
97  *
98  * @note This is relevant for Vector composition only. Other compositions return a NULL pointer
99  *
100  * @param[in] inIndices A list of indices to be accessed. The number of indices must be equal
101  * to the number of dimensions.
102  *
103  * @return A pointer to the child situated at the specified indices. For non-relevant types a
104  * NULL pointer is returned
105  *
106  * @exception Error Index dimensions mismatch
107  */
108  virtual const Pointer get(const std::vector<SizeType>& inIndices) const throw (Error) = 0;
109 
110  /**
111  * Set a pointer to a parnt composition.
112  *
113  * @param[in] inParentCollection pointer to the parent collection (Vector/Bundle)
114  */
115  inline virtual void setParentCollection(const Pointer& inParentCollection);
116 
117  /**
118  * Get a pointer to the parent collection
119  *
120  * @return A pointer to the parent collection if present, empty pointer otherwise
121  */
122  inline virtual Pointer getParentCollection() const;
123 
124 private:
125 
126 #ifdef GENOM_SERIALIZATION
127  template <class Archive> void serialize(Archive& ar, unsigned int);
128 #endif //GENOM_SERIALIZATION
129  Composite(const Composite<_Type>& rhs);
130 
132 
134 
135 };
136 
137 template <typename _Type> Composite<_Type>::Composite() : SelfReferencing<_Type>(),
138  mParentCollection() {}
139 
140 /**
141  * Set a pointer to a parnt composition.
142  *
143  * @param[in] parentCollection pointer to the parent collection (Vector/Bundle)
144  */
145 template <typename _Type> inline void Composite<_Type>::setParentCollection(
146  const typename Composite<_Type>::Pointer& inParentCollection) {
147  mParentCollection = inParentCollection;
148 }
149 
150 /**
151  * Get a pointer to the parent collection
152  *
153  * @return A pointer to the parent collection if present, empty pointer otherwise
154  */
155 template <typename _Type> inline typename Composite<_Type>::Pointer
157  return mParentCollection.lock();
158 }
159 
160 #ifdef GENOM_SERIALIZATION
161 template <typename _Type> template <class Archive> void Composite<_Type>::serialize(Archive& ar,
162  unsigned int) {
163  ar & boost::serialization::base_object<SelfReferencing<_Type> >(*this);
164  //ar & mParentCollection;
165 }
166 #endif //GENOM_SERIALIZATION
167 
168 } // namespace generic
169 } // namespace torc
170 
171 #endif // TORC_GENERIC_COMPOSITE_HPP
virtual CompositionType getCompositionType() const =0
Interface for objects that can be composed within each other.
Definition: Composite.hpp:45
boost::shared_ptr< Type > Pointer
Definition: Composite.hpp:55
virtual void getChildren(List &outChildren) const =0
CompositionType
Defines possible Composition types.
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
Composite< _Type > & operator=(const Composite< _Type > &rhs)
virtual Pointer getParentCollection() const
Definition: Composite.hpp:156
virtual size_t getSize() const =0
Contains definition for CompositionType.
WeakPointer mParentCollection
Definition: Composite.hpp:133
boost::weak_ptr< Type > WeakPointer
Definition: Composite.hpp:56
virtual void setParentCollection(const Pointer &inParentCollection)
Definition: Composite.hpp:145
std::vector< Pointer > List
Definition: Composite.hpp:61