torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Permutable.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_PERMUTABLE_HPP
17 #define TORC_GENERIC_PERMUTABLE_HPP
18 
19 #include <vector>
20 
22 #include "torc/generic/Error.hpp"
27 
28 namespace torc { namespace generic { class Port; } }
29 namespace torc { namespace generic { class BaseVisitor; } }
30 
31 namespace torc {
32 namespace generic {
33 
34 /**
35  * @brief Permutable is used to describe a relationship in which ports are interchangeable.
36  *
37  * It may reference individual ports, arrayed ports, bundled ports, permutable and nonpermutable
38  * statements.
39  */
40 class Permutable : public Visitable, public SelfReferencing<Permutable> {
41 
42  friend class FactoryType<Permutable> ;
43 
44 public:
45 
46  /**
47  * @enum PermutableType
48  *
49  * Type of the permutable, Is it parent or child.
50  */
53  };
54 
55  /**
56  * Convenience class to visit a permutable.
57  */
59 
60  /**
61  * Convenience class to create a permutable.
62  */
63  class Factory : public FactoryType<Permutable> {
64  public:
66  /**
67  * Create a permutable.
68  *
69  * @param[in] inPorts Vector of ports to this permutable.
70  * @param[in] inViewPtr Pointer to parented(View) object.
71  * @param[in] inParentPermutable Pointer to parent permutable.
72  *
73  * @return Pointer to created permutable.
74  */
75  PermutableSharedPtr virtual newPermutablePtr(const std::vector<PortSharedPtr>& inPorts,
76  const ViewSharedPtr& inViewPtr,
77  const PermutableSharedPtr& inParentPermutable = PermutableSharedPtr()) throw (Error);
78  };
79 
80  /**
81  * Get the list of ports for this permutable.
82  *
83  * @param[out] outPorts Vector of ports to be appended to
84  */
85  inline void getPorts(std::vector<PortSharedPtr>& outPorts) const;
86 
87  /**
88  * Set the list of ports to this permutable.
89  * It will lead to a linear traversal on the list.
90  * So usage of this API is not recommended.
91  *
92  * @param[in] inSource Vector of ports
93  */
94  void setPorts(const std::vector<PortSharedPtr>& inSource) throw (Error);
95 
96  /**
97  * Add a port to the list of ports. Empty pointer is ignored.
98  *
99  * @param[in] inPort Pointer to port to be added.
100  *
101  * @exception Error Could not add port, because Port name is empty
102  */
103  void addPort(const PortSharedPtr& inPort) throw (Error);
104 
105  /**
106  * Get the nested permutable.
107  *
108  * @param[out] outPermutables Vector of permutable to be appended to
109  */
110  inline void getChildren(std::vector<PermutableSharedPtr>& outPermutables) const;
111 
112  /**
113  * Apply action on all children.
114  * @param[in] action Action to be applied
115  *
116  */
117  template <typename _Action> inline void applyOnAllChildren(const _Action& action) throw (Error);
118 
119  /**
120  * Set the nested permutable.
121  *
122  * @param[in] inSource Vector containing permutables
123  */
124  void setChildren(const std::vector<PermutableSharedPtr> & inSource) throw (Error);
125 
126  /**
127  * Add a nested permutable/nonpermutable to parent permutable.
128  *
129  * @param[in] inPermutable Nested permutable/nonpermutable
130  */
131  bool addChildPermutable(const PermutableSharedPtr& inPermutable) throw (Error);
132 
133  /**
134  * Get the boolean flag whether the current object is permutable/nonpermutable.
135  *
136  * @return Boolean flag
137  */
138  inline const bool getIsNonPermutable() const;
139 
140  void setIsNonPermutable(const bool& value);
141 
142  /**
143  * Get the permutable type.
144  *
145  * @return PermutableType
146  */
147  inline const PermutableType getPermutableType() const;
148 
149  /**
150  * Set the permutable type.
151  *
152  * @param[in] inSource PermutableType
153  */
154  void setPermutableType(const PermutableType& inSource);
155 
156  /**
157  * Get the total number of bits of the composition
158  * @return Number of bits
159  */
160  size_t getSize() const;
161 
162  virtual ~Permutable() throw ();
163 
164  /**
165  * Receive a visitor to this class. The visit method of the visitor is called
166  * and a reference to this object is passed as a parameter. It has to be noted however,
167  * that a dynamic_cast is performed inside this method. If the cast fails,
168  * an appropriate exception is thrown by this method. This situation can arise when
169  * the passed Visitor object does not inherit from the appropriate visitor specialization.
170  * See Visitor documentation for more details.
171  *
172  * @param[in,out] visitor A reference to the visitor object
173  * @exception Error Visitor type inappropriate for visiting this object
174  * or any other error thrown by the Visitor::throw() method.
175  */
176  virtual void accept(BaseVisitor& visitor) throw (Error);
177 
178 protected:
179  Permutable();
180 
181 private:
182  std::vector<PortSharedPtr> mPorts;
186 
187 };
188 
189 /**
190  * Get the list of ports for this permutable.
191  *
192  * @param[out] outPorts Vector of ports to be appended to
193  */
194 inline void Permutable::getPorts(std::vector<PortSharedPtr>& outValues) const {
195  outValues.insert(outValues.end(), mPorts.begin(), mPorts.end());
196 }
197 
198 /**
199  * Get the nested permutable.
200  *
201  * @param[out] outPermutables Vector of permutable to be appended to
202  */
203 inline void Permutable::getChildren(std::vector<PermutableSharedPtr>& outPermutables) const {
204  outPermutables.insert(outPermutables.end(), mChildren.begin(), mChildren.end());
205 }
206 
207 /**
208  * Apply action on all children.
209  * @param[in] action Action to be applied
210  *
211  */
212 template <typename _Action> inline void Permutable::applyOnAllChildren(const _Action& action)
213  throw (Error) {
214  try {
215  std::vector<PermutableSharedPtr>::iterator it = mChildren.begin();
216  for(; it != mChildren.end(); ++it) {
217  action(*it);
218  }
219  } catch(Error& e) {
220  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
221  throw;
222  }
223 }
224 
225 /**
226  * Get the boolean flag whether the current object is permutable/nonpermutable.
227  * @return Boolean flag
228  */
229 inline const bool Permutable::getIsNonPermutable() const {
230  return mIsNonPermutable;
231 }
232 
233 /**
234  * Get the permutable type.
235  *
236  * @return PermutableType
237  */
239  return mPermutableType;
240 }
241 
242 } // namespace generic
243 } // namespace torc
244 
245 #endif // TORC_GENERIC_PERMUTABLE_HPP
An acyclic inoutVisitor implementation.
Definition: VisitorType.hpp:57
void applyOnAllChildren(const _Action &action)
Definition: Permutable.hpp:212
virtual PermutableSharedPtr newPermutablePtr(const std::vector< PortSharedPtr > &inPorts, const ViewSharedPtr &inViewPtr, const PermutableSharedPtr &inParentPermutable=PermutableSharedPtr())
Definition: Permutable.cpp:31
void addPort(const PortSharedPtr &inPort)
Definition: Permutable.cpp:76
boost::shared_ptr< Permutable > PermutableSharedPtr
void setPorts(const std::vector< PortSharedPtr > &inSource)
Definition: Permutable.cpp:56
Permutable is used to describe a relationship in which ports are interchangeable. ...
Definition: Permutable.hpp:40
void setPermutableType(const PermutableType &inSource)
Definition: Permutable.cpp:193
VisitorType< Permutable > Visitor
Definition: Permutable.hpp:58
std::vector< PermutableSharedPtr > mChildren
Definition: Permutable.hpp:183
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
size_t getSize() const
Definition: Permutable.cpp:145
A base class for Visitor.
Definition: VisitorType.hpp:31
virtual void accept(BaseVisitor &visitor)
Definition: Permutable.cpp:132
const bool getIsNonPermutable() const
Definition: Permutable.hpp:229
const PermutableType getPermutableType() const
Definition: Permutable.hpp:238
PermutableType mPermutableType
Definition: Permutable.hpp:185
std::vector< PortSharedPtr > mPorts
Definition: Permutable.hpp:182
void setIsNonPermutable(const bool &value)
Definition: Permutable.cpp:128
void getChildren(std::vector< PermutableSharedPtr > &outPermutables) const
Definition: Permutable.hpp:203
void getPorts(std::vector< PortSharedPtr > &outPorts) const
Definition: Permutable.hpp:194
void setChildren(const std::vector< PermutableSharedPtr > &inSource)
Definition: Permutable.cpp:95
boost::shared_ptr< View > ViewSharedPtr
boost::shared_ptr< Port > PortSharedPtr
bool addChildPermutable(const PermutableSharedPtr &inPermutable)
Definition: Permutable.cpp:114
A placeholder for a factory method.
Definition: FactoryType.hpp:35
An object that receives an inoutVisitor.
Definition: Visitable.hpp:38
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73