Simbody  3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ReferencePtr.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_REFERENCE_PTR_H_
2 #define SimTK_SimTKCOMMON_REFERENCE_PTR_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2012 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
27 namespace SimTK {
28 
52 template <class T> class ReferencePtr {
53 public:
54  typedef T element_type;
55  typedef T* pointer;
56  typedef T& reference;
57 
59  ReferencePtr() : p(0) { }
61  explicit ReferencePtr(T* tp) : p(tp) { }
64  explicit ReferencePtr(T& t) : p(&t) { }
67  ReferencePtr(const ReferencePtr&) : p(0) { }
71  { if (&r != this) clear(); return *this; }
75  { reset(&t); return *this; }
79  { reset(tp); return *this; }
80 
82  ~ReferencePtr() {clear();} // just being tidy
83 
86  T* operator->() const { return &getRef(); }
87 
90  T& operator*() const { return getRef(); }
91 
93  operator T*() const { return p; }
94 
97  operator bool() const { return !empty(); }
98 
100  T* get() const { return p; }
101 
104  T& getRef() const {
105  SimTK_ERRCHK(p!=0, "ReferencePtr::getRef()",
106  "An attempt was made to dereference a null pointer.");
107  return *p;
108  }
109 
111  bool empty() const { return p==0; }
113  void clear() { p=0; }
116  T* release() { T* x=p; p=0; return x; }
119  void reset(T* tp) { p=tp;}
120 
122  void swap(ReferencePtr& other) {
123  T* otherp = other.release();
124  other.reset(p);
125  reset(otherp);
126  }
127 
128 private:
129  // Warning: ReferencePtr must be exactly the same size as type T*. That way
130  // one can reinterpret_cast a T* to a ReferencePtr<T> when needed.
131  T* p;
132 };
133 
134 } // namespace SimTK
135 
136 namespace std {
140 template <class T> inline void
142  p1.swap(p2);
143 }
144 
145 } // namespace std
146 
147 #endif // SimTK_SimTKCOMMON_REFERENCE_PTR_H_
void swap(ReferencePtr &other)
Swap the contents of this ReferencePtr with another one.
Definition: ReferencePtr.h:122
ReferencePtr & operator=(const ReferencePtr &r)
Copy assignment sets the pointer to null (except for a self-assign); see class comments for why...
Definition: ReferencePtr.h:70
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
~ReferencePtr()
Destructor does nothing.
Definition: ReferencePtr.h:82
STL namespace.
void swap(SimTK::ReferencePtr< T > &p1, SimTK::ReferencePtr< T > &p2)
This is a specialization of the STL std::swap() algorithm which uses the cheap built-in swap() member...
Definition: ReferencePtr.h:141
T element_type
Definition: ReferencePtr.h:54
ReferencePtr()
Default constructor creates an empty object.
Definition: ReferencePtr.h:59
T & operator*() const
The "dereference" operator returns a reference to the target object.
Definition: ReferencePtr.h:90
T & getRef() const
Return a reference to the target object.
Definition: ReferencePtr.h:104
ReferencePtr(T &t)
Construct from a reference stores the address of the supplied object.
Definition: ReferencePtr.h:64
ReferencePtr(T *tp)
Construct from a given pointer stores the pointer.
Definition: ReferencePtr.h:61
T * operator->() const
Return the contained pointer.
Definition: ReferencePtr.h:86
T & reference
Definition: ReferencePtr.h:56
T * release()
Extract the pointer from this container, leaving the container empty.
Definition: ReferencePtr.h:116
void reset(T *tp)
Replace the stored pointer with a different one; no destruction occurs.
Definition: ReferencePtr.h:119
ReferencePtr(const ReferencePtr &)
Copy constructor unconditionally sets the pointer to null; see class comments for why...
Definition: ReferencePtr.h:67
#define SimTK_ERRCHK(cond, whereChecked, msg)
Definition: ExceptionMacros.h:324
This is a smart pointer that implements "cross reference" semantics where a pointer data member of so...
Definition: ReferencePtr.h:52
T * pointer
Definition: ReferencePtr.h:55
ReferencePtr & operator=(T &t)
This form of assignment replaces the currently-referenced object by a reference to the source object;...
Definition: ReferencePtr.h:74
ReferencePtr & operator=(T *tp)
This form of assignment replaces the current pointer with the given one; no destruction occurs...
Definition: ReferencePtr.h:78
bool empty() const
Return true if this container is empty.
Definition: ReferencePtr.h:111
void clear()
Make this container empty; no destruction occurs.
Definition: ReferencePtr.h:113