torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SelfReferencing.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_SELFREFERENCING_HPP
17 #define TORC_GENERIC_SELFREFERENCING_HPP
18 
19 //BOOST
20 #include <boost/shared_ptr.hpp>
21 #include <boost/weak_ptr.hpp>
22 
23 #ifdef GENOM_SERIALIZATION
24 #include <boost/serialization/access.hpp>
25 #include <boost/serialization/weak_ptr.hpp>
26 #endif //GENOM_SERIALIZATION
27 namespace torc {
28 namespace generic {
29 
30 template <typename _Tp> class SelfReferencing {
31 #ifdef GENOM_SERIALIZATION
32  friend class boost::serialization::access;
33 #endif //GENOM_SERIALIZATION
34 public:
35  typedef _Tp Type;
36  typedef boost::shared_ptr<Type> Pointer;
37  typedef boost::weak_ptr<Type> WeakPointer;
38 
39  /**
40  * Set a weak pointer to this object. This will be used later to get a shared pointer to this
41  * object from within other member methods if required. This should be called by the Factory
42  * creating the object.
43  *
44  * @param[in] weakThis A weak pointer to this object
45  */
46  inline void setWeakThis(const WeakPointer& inWeakThis);
47 
48  /**
49  * Get a shared pointer to this object. A valid pointer is returned if weakThis was set.
50  * Otherwise this returns a NULL pointer. For Vector types, the second scenario is an exception
51  * condition and should be treated as such.
52  *
53  * @return A shared pointer to this object
54  */
55  inline Pointer getSharedThis() const;
56 
57 protected:
59 
60 public:
61  ~SelfReferencing() throw ();
62 
63 private:
64 #ifdef GENOM_SERIALIZATION
65  template <class Archive> void serialize(Archive& ar, unsigned int);
66 #endif //GENOM_SERIALIZATION
68 };
69 
70 template <typename _Type> SelfReferencing<_Type>::SelfReferencing() : mWeakThis() {}
71 
72 template <typename _Type> SelfReferencing<_Type>::~SelfReferencing() throw () {}
73 
74 /**
75  * Set a weak pointer to this object. This will be used later to get a shared pointer to this
76  * object from within other member methods if required. This should be called by the Factory
77  * creating the object.
78  *
79  * @param[in] weakThis A weak pointer to this object
80  */
81 template <typename _Type> inline void SelfReferencing<_Type>::setWeakThis(
82  const SelfReferencing<_Type>::WeakPointer& inWeakThis) {
83  mWeakThis = inWeakThis;
84 }
85 
86 /**
87  * Get a shared pointer to this object. A valid pointer is returned if weakThis was set. Otherwise
88  * this returns a NULL pointer. For Vector types, the second scenario is an exception condition and
89  * should be treated as such.
90  *
91  * @param[out] ptr Set to a shared pointer to this object
92  */
93 template <typename _Type> inline typename SelfReferencing<_Type>::Pointer
95  return mWeakThis.lock();
96 }
97 
98 #ifdef GENOM_SERIALIZATION
99 template <typename _Type> template <class Archive> void
100  SelfReferencing<_Type>::serialize(Archive& ar, unsigned int) {
101  ar & mWeakThis;
102 }
103 #endif //GENOM_SERIALIZATION
104 
105 } //namespace generic
106 } //namespace torc
107 
108 #endif // TORC_GENERIC_SELFREFERENCING_HPP
boost::shared_ptr< Type > Pointer
boost::weak_ptr< Type > WeakPointer
void setWeakThis(const WeakPointer &inWeakThis)