torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Bundle.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_BUNDLE_HPP
17 #define TORC_GENERIC_BUNDLE_HPP
18 
19 #include <algorithm>
20 #include <vector>
21 
22 #ifdef GENOM_SERIALIZATION
23 #include <boost/bind.hpp>
24 #include <boost/mem_fn.hpp>
25 #include <boost/serialization/access.hpp>
26 #include <boost/serialization/base_object.hpp>
27 #include <boost/serialization/shared_ptr.hpp>
28 #include <boost/serialization/split_member.hpp>
29 #include <boost/serialization/vector.hpp>
30 #endif //GENOM_SERIALIZATION
33 #include "torc/generic/Error.hpp"
34 
35 namespace torc {
36 namespace generic {
37 
38 /**
39  * @brief Represents a "bundle" in the EDIF sense
40  *
41  * A bundle in EDIF is an ordered collection of items. For example a bundle of ports is a collection of other scalar and vector ports. The Bundle class represents an implementation of functionality for such a collection.
42  */
43 template <typename _Type> class Bundle : public virtual Composite<_Type> {
44 #ifdef GENOM_SERIALIZATION
45  friend class boost::serialization::access;
46 #endif
47 public:
49  typedef typename BaseType::Type Type;
50  typedef typename BaseType::Pointer Pointer;
51  typedef typename BaseType::List List;
52  typedef typename BaseType::SizeType SizeType;
53  typedef Type ChildType;
54 
55 protected:
56  Bundle();
57 
58 public:
59  virtual ~Bundle() throw ();
60 
61  /**
62  * Get composition type for this object
63  *
64  * @return The CompositionType inSource VECTOR is returned
65  */
66  virtual CompositionType getCompositionType() const;
67 
68  /**
69  * Get the total number of bits of the composition
70  * @return Number of bits
71  */
72  virtual SizeType getSize() const;
73 
74  /**
75  * Add a child to the bundle. Note that, the new element is added to the end of the array.
76  *
77  * @param[in] child A pointer to a child
78  *
79  * @exception Error The composition is not a container or the child type is not proper
80  */
81  virtual void addChild(const boost::shared_ptr<Type>& child) throw (Error);
82 
83  /**
84  * Get children of this composition.
85  *
86  * @param[out] outChildren A list of all children for this composition
87  */
88  virtual void getChildren(List& outChildren) const throw (Error);
89 
90  template <typename _Action> void applyOnAllChildren(const _Action& action) throw (Error);
91 
92  /**
93  * Get a specific member of this composition.
94  *
95  * @return An empty pointer is returned
96  *
97  * @exception Error Index dimensions mismatch
98  */
99  virtual const Pointer get(const std::vector<SizeType>& indices) const throw (Error);
100 
101 private:
102 #ifdef GENOM_SERIALIZATION
103  template <class Archive> void load(Archive& ar, unsigned int);
104 
105  template <class Archive> void save(Archive& ar, unsigned int) const;
106 
107  BOOST_SERIALIZATION_SPLIT_MEMBER()
108 #endif //GENOM_SERIALIZATION
110 };
111 
112 template <typename _Type> Bundle<_Type>::Bundle() : Composite<_Type>() {}
113 
114 template <typename _Type> Bundle<_Type>::~Bundle() throw () {}
115 
116 /**
117  * Get composition type for this object
118  *
119  * @return The CompositionType inSource VECTOR is returned
120  */
121 template <typename _Type> CompositionType Bundle<_Type>::getCompositionType() const {
122  return eCompositionTypeBundle;
123 }
124 
125 /**
126  * Get the total number of bits of the composition
127  * @return Number of bits
128  */
129 template <typename _Type> typename Bundle<_Type>::SizeType Bundle<_Type>::getSize() const {
130  size_t size = 0;
131  for(typename Bundle<_Type>::List::const_iterator it = mChildren.begin(); it != mChildren.end();
132  ++it) {
133  size += (*it)->getSize();
134  }
135  return size;
136 }
137 
138 /**
139  * Add a child to the bundle. Note that, the new element is added to the end of the array.
140  *
141  * @param[in] child A pointer to a child
142  *
143  * @exception Error The composition is not a container or the child type is not proper
144  */
145 template <typename _Type> void Bundle<_Type>::addChild(const boost::shared_ptr<Type>& child)
146  throw (Error) {
147  mChildren.push_back(child);
148  child->setParentCollection(SelfReferencing<_Type>::getSharedThis());
149 }
150 
151 /**
152  * Get children of this composition.
153  *
154  * @param[out] outChildren A list of all children for this composition
155  */
156 template <typename _Type> void Bundle<_Type>::getChildren(
157  typename Bundle<_Type>::List& outChildren) const throw (Error) {
158  outChildren.insert(outChildren.end(), mChildren.begin(), mChildren.end());
159  return;
160 }
161 
162 template <typename _Type> template <typename _Action> void Bundle<_Type>::applyOnAllChildren(
163  const _Action& action) throw (Error) {
164  try {
165  std::for_each(mChildren.begin(), mChildren.end(), action);
166  } catch(Error& e) {
167  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
168  throw;
169  }
170 
171 }
172 
173 /**
174  * Get a specific member of this composition.
175  *
176  * @return An empty pointer is returned
177  *
178  * @exception Error Index dimensions mismatch
179  */
180 template <typename _Type> const typename Bundle<_Type>::Pointer Bundle<_Type>::get(
181  const std::vector<typename Bundle<_Type>::SizeType>& indices) const throw (Error) {
182  return Pointer();
183 }
184 
185 #ifdef GENOM_SERIALIZATION
186 template <typename _Type>template <class Archive> void Bundle<_Type>::load(Archive& ar,
187  unsigned int) {
188  ar & boost::serialization::base_object<BaseType>(*this);
189  ar & mChildren;
190  for_each(mChildren.begin(), mChildren.end(),
191  boost::bind(boost::mem_fn(&Composite<_Type>::setParentCollection), _1,
193 }
194 
195 template <typename _Type>template <class Archive> void Bundle<_Type>::save(Archive& ar,
196  unsigned int) const {
197  ar & boost::serialization::base_object<BaseType>(*this);
198  ar & mChildren;
199 }
200 
201 #endif //GENOM_SERIALIZATION
202 
203 } // namespace generic
204 } // namespace torc
205 
206 #endif // TORC_GENERIC_BUNDLE_HPP
virtual SizeType getSize() const
Definition: Bundle.hpp:129
BaseType::SizeType SizeType
Definition: Bundle.hpp:52
Interface for objects that can be composed within each other.
Definition: Composite.hpp:45
BaseType::Type Type
Definition: Bundle.hpp:49
boost::shared_ptr< Type > Pointer
Definition: Composite.hpp:55
virtual void addChild(const boost::shared_ptr< Type > &child)
Definition: Bundle.hpp:145
CompositionType
Defines possible Composition types.
virtual const Pointer get(const std::vector< SizeType > &indices) const
Definition: Bundle.hpp:180
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
Composite< _Type > BaseType
Definition: Bundle.hpp:48
Contains definition for CompositionType.
void applyOnAllChildren(const _Action &action)
Definition: Bundle.hpp:162
virtual void getChildren(List &outChildren) const
Definition: Bundle.hpp:156
BaseType::Pointer Pointer
Definition: Bundle.hpp:50
virtual CompositionType getCompositionType() const
Definition: Bundle.hpp:121
BaseType::List List
Definition: Bundle.hpp:51
Represents a "bundle" in the EDIF sense.
Definition: Bundle.hpp:43
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73
std::vector< Pointer > List
Definition: Composite.hpp:61