torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VisitorType.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_VISITORTYPE_HPP
17 #define TORC_GENERIC_VISITORTYPE_HPP
18 
19 #include "torc/generic/Error.hpp"
20 
21 namespace torc {
22 namespace generic {
23 
24 /**
25  * @brief A base class for Visitor
26  *
27  * This is the base class for Visitor template. This class is used to provide a concrete handle of
28  * an inoutVisitor to a visitable class. This class therefore does not contain any methods other
29  * than a (protected) constructor and a destructor.
30  */
31 class BaseVisitor {
32 protected:
33  BaseVisitor();
34 
35 public:
36  virtual
37  ~BaseVisitor() throw ();
38 
39 private:
40  BaseVisitor(const BaseVisitor&);
42 };
43 
44 /**
45  * @brief An acyclic inoutVisitor implementation
46  *
47  * The VisitorType template acts as a template that can be derived by clients to add extrinsic
48  * virtual functions on class hierarchies. This is useful in situations where the user does not
49  * have direct handle to a derived class, but has a pointer/reference to a base class. This class
50  * defines a polymorphic method visit() that can be used to get a handle to the actual derived
51  * object and can therefore be programmed to perform arbitrary operations on it using public
52  * methods specific to the derived class object. For more information on the Visitor design pattern
53  * see: http://en.wikipedia.org/wiki/Visitor_pattern
54  * The inoutVisitor implementation in EOM follows the acyclic inoutVisitor implementation detailed
55  * in Andrei Alexandrescu's <i>Modern C++ Design</i>.
56  */
57 template <typename _Tp> class VisitorType : virtual public BaseVisitor {
58 protected:
59  explicit VisitorType();
60 
61 public:
62  virtual ~VisitorType() throw ();
63 
64  /**
65  * Visit the target object. This will typically be a derived leaf type.
66  *
67  * @param[in,out] client A reference to the target object
68  * @exception Error Exception generated by any of the functions called from inside visit()
69  */
70  virtual void visit(_Tp& client) throw (Error) = 0;
71 
72 };
73 
74 template <typename _Tp> VisitorType<_Tp>::VisitorType() {}
75 
76 template <typename _Tp> VisitorType<_Tp>::~VisitorType() throw () {}
77 
78 template <typename _Tp> void runVisitor(_Tp& inoutVisited, BaseVisitor& inoutVisitor)
79  throw (Error) {
80  typedef VisitorType<_Tp> ConcreteVisitor;
81 
82  if(ConcreteVisitor *p = dynamic_cast<ConcreteVisitor *>(&inoutVisitor)) {
83  try {
84  p->visit(inoutVisited);
85  } catch(Error& e) {
86  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
87  throw;
88  }
89  return;
90  }
91  //TBD::Error?Fallback?
92  return;
93 }
94 
95 } // namespace generic
96 } // namespace torc
97 
98 #endif // TORC_GENERIC_VISITORTYPE_HPP
An acyclic inoutVisitor implementation.
Definition: VisitorType.hpp:57
void runVisitor(_Tp &inoutVisited, BaseVisitor &inoutVisitor)
Definition: VisitorType.hpp:78
BaseVisitor & operator=(const BaseVisitor &)
virtual void visit(_Tp &client)=0
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
A base class for Visitor.
Definition: VisitorType.hpp:31
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73