torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Root.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_ROOT_HPP
17 #define TORC_GENERIC_ROOT_HPP
18 
20 
21 #include <list>
22 #include <string>
23 
24 //BOOST
25 #include <boost/shared_ptr.hpp>
26 #ifdef GENOM_SERIALIZATION
27 #include <boost/serialization/access.hpp>
28 #include <boost/serialization/split_member.hpp>
29 #endif //GENOM_SERIALIZATION
33 #include "torc/generic/Error.hpp"
38 #include "torc/generic/SymTab.hpp"
43 
44 #ifdef GENOM_SERIALIZATION
46 #endif //GENOM_SERIALIZATION
47 namespace torc { namespace generic { class Library; } }
48 namespace torc { namespace generic { class BaseVisitor; } }
49 
50 namespace torc {
51 namespace generic {
52 
53 /**
54  * @brief Root of the EDIF Object Model
55  *
56  * The Root class represents and EDIF hierarchy. All parsed EDIF files contribute to the growth of
57  * the object tree rooted here. At the top level, it provides access to the libraries present in
58  * the design.
59  *
60  * @note In case the tree has been programatically created, clients need to decompile this tree, a
61  * name has to be given to the tree, otherwise an exception will be generated. This name will
62  * become the design name of the decompiled output file. For parser generated trees, the design
63  * name of the last file parsed becomes the name of the root. This can obviously be changed by the
64  * client, by calling the set_name() method.
65  */
66 class Root : public Commentable, public Nameable, public Renamable, public Visitable,
67  public SelfReferencing<Root>, public UserDataContainer, public StatusContainer {
68 
69 #ifdef GENOM_SERIALIZATION
70  friend class boost::serialization::access;
71 #endif //GENOM_SERIALIZATION
72  friend class FactoryType<Root> ;
73 
74 public:
75  /**
76  * Convenience typedef for visiting the root
77  */
79 
80  /**
81  * Convenience class for creating a root
82  */
83  class Factory : public FactoryType<Root> {
84  public:
86  /**
87  * Create a root
88  *
89  * @param[in] inName Name of the root to be created.
90  * @param[in] inEdifLevel Edif level.
91  * @param[in] inOriginalName Original name of the root [optional].
92  *
93  * @return Pointer to created root.
94  */
95  virtual RootSharedPtr newRootPtr(const std::string& inName, const EdifLevel& inEdifLevel =
96  eEdifLevel0, const std::string& inOriginalName = std::string()) throw (Error);
97  };
98 
99  /**
100  * Add a library to the list of libraries. If an empty pointer is supplied, it returns without
101  * doing anything.
102  *
103  * @param[in] inLibrary A pointer to a library object.
104  *
105  * @exception Error Library could not be added. because Library name is empty
106  * <ul>
107  * <li>
108  * Id : eMessageIdErrorEmptyItemName
109  * </li>
110  * <li> Context Data
111  * <ul>
112  * <li>Library name - <i>String</i></li>
113  * </ul>
114  * </li>
115  * </ul>
116  *
117  * @exception Error Library could not be added. because Library name is already exists
118  * <ul>
119  * <li>
120  * Id : eMessageIdErrorItemAlreadyExists
121  * </li>
122  * <li> Context Data
123  * <ul>
124  * <li>Library name - <i>String</i></li>
125  * </ul>
126  * </li>
127  * </ul>
128  *
129  */
130  virtual void addLibrary(const LibrarySharedPtr& inLibrary) throw (Error);
131 
132  /**
133  * Remove the specified library from the list of libraries.
134  *
135  * @param inName Name of the object to be delete
136  *
137  * @exception Error Empty Library name
138  * <ul>
139  * <li>
140  * Id : eMessageIdErrorEmptyItemName
141  * </li>
142  * <li> Context Data
143  * <ul>
144  * <li>Library name - <i>String</i></li>
145  * </ul>
146  * </li>
147  * </ul>
148  *
149  * @exception Error Library not preset in collection
150  * <ul>
151  * <li>
152  * Id : eMessageIdErrorItemNotFound
153  * </li>
154  * <li> Context Data
155  * <ul>
156  * <li>Library name - <i>String</i></li>
157  * </ul>
158  * </li>
159  * </ul>
160  *
161  */
162  virtual void removeLibrary(const std::string& inName) throw (Error);
163 
164  /**
165  * Find a library by name, in the list of libraries.
166  *
167  * @param[in] inName String specifying the name of the library.
168  *
169  * @return A pointer to the libary if found, an empty pointer otherwise.
170  * @note If some libraries are yet to be restored, this method is not thread safe. For
171  * non-serializable mode this is thread safe. For dump mode derived classes can override this
172  * to make it thread safe by placing appropriate locks.
173  */
174  virtual LibrarySharedPtr findLibrary(const std::string& inName);
175 
176  /**
177  * Get the list of libraries.
178  *
179  * @param[out] outLibraries List of libraries contained in the root
180  * @note If some libraries are yet to be restored, this method is not thread safe. For
181  * non-serializable mode this is thread safe. For dump mode derived classes can override this
182  * to make it thread safe by placing appropriate locks. In dump mode using this method will
183  * lead to restoration of all children.
184  */
185  virtual void getLibraries(std::vector<LibrarySharedPtr>& outLibraries);
186 
187  /**
188  * Apply action on all Libraries.
189  * @param[in] action Action to be applied
190  * @note If some libraries are yet to be restored, this method is not thread safe. For
191  * non-serializable mode this is thread safe. For dump mode derived classes can write an
192  * appropriate thread safe wrapper method that can be accesed using visitors. However, using
193  * this method in dump mode should be avoided unless under dire situations, as it will lead to
194  * restoration of all libraries under the root.
195  *
196  */
197  template <typename _Action> inline void applyOnAllLibraries(const _Action& action) throw (Error);
198 
199  /**
200  * Add a design to the list of designs. If an empty pointer is supplied, it returns without doing anything.
201  *
202  * @param[in] inDesign A pointer to a design object.
203  *
204  * @exception Error Design could not be added. because design name is empty
205  * <ul>
206  * <li>
207  * Id : eMessageIdErrorEmptyItemName
208  * </li>
209  * </ul>
210  *
211  * @exception Error Design could not be added. because Library name is already exists
212  * <ul>
213  * <li>
214  * Id : eMessageIdErrorItemAlreadyExists
215  * </li>
216  * </ul>
217  *
218  */
219 
220  virtual void addDesign(const DesignSharedPtr& inDesign) throw (Error);
221 
222  /**
223  * Remove the specified design from the list of designs.
224  *
225  * @param inName Name of the object to be delete
226  *
227  * @exception Error Empty design name
228  * <ul>
229  * <li>
230  * Id : eMessageIdErrorEmptyItemName
231  * </li>
232  * </ul>
233  *
234  * @exception Error design not preset in collection
235  * <ul>
236  * <li>
237  * Id : eMessageIdErrorItemNotFound
238  * </li>
239  * </ul>
240  *
241  */
242  virtual void removeDesign(const std::string& inName) throw (Error);
243 
244  /**
245  * Find a design by name, in the list of designs.
246  *
247  * @param[in] inName String specifying the name of the design.
248  *
249  * @return A pointer to the design if found, an empty pointer otherwise.
250  */
251  virtual DesignSharedPtr findDesign(const std::string& inName);
252 
253  /**
254  * Get the list of designs.
255  *
256  * @param[out] outDesigns List of designs contained in the root
257  */
258  virtual void getDesigns(std::vector<DesignSharedPtr>& outDesigns);
259 
260  /**
261  * Apply action on all Designs.
262  * @param[in] action Action to be applied
263  *
264  */
265  template <typename _Action> inline void applyOnAllDesigns(const _Action& action) throw (Error);
266 
267  virtual void accept(BaseVisitor& inoutVisitor) throw (Error);
268 
269  /**
270  * Get the version of EDIF present in the tree.
271  *
272  * @return Version of edif
273  */
274  inline const EdifVersion getVersion() const;
275 
276  /**
277  * Set the version of EDIF being used. This should be 2 0 0 for the current EOM version.
278  *
279  * @param[in] inSource An object of type EdifVersion.
280  */
281  void setVersion(const EdifVersion& inSource);
282 
283  /**
284  * The the level of EDIF file.
285  *
286  * @return The EDIF level for this file
287  */
288  inline const EdifLevel getLevel() const;
289 
290  /**
291  * Set the EDIF level.
292  *
293  * @note Current parser supports LEVEL_0 edif only.
294  *
295  * @param[in] inSource EdifLevel object
296  */
297  void setLevel(const EdifLevel& inSource);
298 
299 #ifdef GENOM_SERIALIZATION
300  inline const DumpRestoreData& getDumpRestoreData() const;
301 
302  void setDumpRestoreData(const DumpRestoreData& inDumpRestoreData);
303  void handleNewDumpRestoreData(const DumpRestoreData& inDumpRestoreData) throw(Error);
304 #endif //GENOM_SERIALIZATION
305  virtual ~Root() throw ();
306 
307 protected:
308  Root();
309 
310 private:
311 
312 #ifdef GENOM_SERIALIZATION
313  template <class Archive> void load(Archive& ar, unsigned int);
314 
315  template <class Archive> void save(Archive& ar, unsigned int) const;
316 
317  LibrarySharedPtr restoreSingleLibrary(const std::string& inName) throw(Error);
318 
319  void restoreAllLibraries() throw(Error);
320 
321  BOOST_SERIALIZATION_SPLIT_MEMBER()
322 #endif //GENOM_SERIALIZATION
323  Root(const Root& source);
324 
325  Root& operator=(const Root& source);
326 
331 #ifdef GENOM_SERIALIZATION
332  DumpRestoreData mDumpRestoreData;
333  mutable std::list< std::string > mDumpedLibraries;
334 #endif //GENOM_SERIALIZATION
335 };
336 
337 /**
338  * Apply action on all Libraries.
339  * @param[in] action Action to be applied
340  *
341  */
342 template <typename _Action> inline void Root::applyOnAllLibraries(const _Action& action)
343  throw (Error) {
344  try {
345 #ifdef GENOM_SERIALIZATION
346  restoreAllLibraries();
347 #endif //GENOM_SERIALIZATION
348  mLibraries.applyOnAll(action);
349  } catch(Error& e) {
350  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
351  throw;
352  }
353 }
354 
355 /**
356  * Apply action on all Designs.
357  * @param[in] action Action to be applied
358  *
359  */
360 template <typename _Action> inline void Root::applyOnAllDesigns(const _Action& action)
361  throw (Error) {
362  try {
363  mDesignSymTab.applyOnAll(action);
364  } catch(Error& e) {
365  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
366  throw;
367  }
368 }
369 
370 /**
371  * Get the version of EDIF present in the tree.
372  *
373  * @return Version of edif
374  */
375 inline const EdifVersion Root::getVersion() const {
376  return mVersion;
377 }
378 
379 /**
380  * The the level of EDIF file.
381  *
382  * @return The EDIF level for this file
383  */
384 inline const EdifLevel Root::getLevel() const {
385  return mLevel;
386 }
387 
388 #ifdef GENOM_SERIALIZATION
389 inline const DumpRestoreData&
390 Root::getDumpRestoreData() const {
391  return mDumpRestoreData;
392 }
393 
394 void dump(const RootSharedPtr& inRoot) throw(Error);
395 
396 void dump( const RootSharedPtr& inRoot,
397  const DumpRestoreData& inData ) throw(Error);
398 
399 RootSharedPtr restore( const std::string& inName,
400  const DumpRestoreData& inData ) throw(Error);
401 
402 #endif //GENOM_SERIALIZATION
403 } // namespace generic
404 } // namespace torc
405 
406 #endif // TORC_GENERIC_ROOT_HPP
An acyclic inoutVisitor implementation.
Definition: VisitorType.hpp:57
virtual void getDesigns(std::vector< DesignSharedPtr > &outDesigns)
Definition: Root.cpp:185
EdifVersion mVersion
Definition: Root.hpp:330
void setVersion(const EdifVersion &inSource)
Definition: Root.cpp:240
SymTab< std::string, LibrarySharedPtr > mLibraries
Definition: Root.hpp:328
const EdifVersion getVersion() const
Definition: Root.hpp:375
Represents all classes that can hold user comments.
Definition: Commentable.hpp:36
Represents class that can hold userData.
std::string string
boost::shared_ptr< Design > DesignSharedPtr
virtual void removeDesign(const std::string &inName)
Definition: Root.cpp:166
void applyOnAllDesigns(const _Action &action)
Definition: Root.hpp:360
VisitorType< Root > Visitor
Definition: Root.hpp:78
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
virtual RootSharedPtr newRootPtr(const std::string &inName, const EdifLevel &inEdifLevel=eEdifLevel0, const std::string &inOriginalName=std::string())
Definition: Root.cpp:211
const EdifLevel getLevel() const
Definition: Root.hpp:384
boost::shared_ptr< Library > LibrarySharedPtr
A base class for Visitor.
Definition: VisitorType.hpp:31
EdifLevel mLevel
Definition: Root.hpp:327
Root of the EDIF Object Model.
Definition: Root.hpp:66
virtual void getLibraries(std::vector< LibrarySharedPtr > &outLibraries)
Definition: Root.cpp:115
virtual void addLibrary(const LibrarySharedPtr &inLibrary)
Definition: Root.cpp:48
virtual LibrarySharedPtr findLibrary(const std::string &inName)
Definition: Root.cpp:129
Root & operator=(const Root &source)
An object that has a name.
Definition: Nameable.hpp:34
virtual ~Root()
Definition: Root.cpp:281
virtual void removeLibrary(const std::string &inName)
Definition: Root.cpp:85
Represents objects that can be renamed.
virtual void addDesign(const DesignSharedPtr &inDesign)
Definition: Root.cpp:143
Represents objects that have status.
virtual DesignSharedPtr findDesign(const std::string &inName)
Definition: Root.cpp:196
void applyOnAllLibraries(const _Action &action)
Definition: Root.hpp:342
A placeholder for a factory method.
Definition: FactoryType.hpp:35
void setLevel(const EdifLevel &inSource)
Definition: Root.cpp:251
virtual void accept(BaseVisitor &inoutVisitor)
Definition: Root.cpp:226
boost::shared_ptr< Root > RootSharedPtr
SymTab< std::string, DesignSharedPtr > mDesignSymTab
Definition: Root.hpp:329
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