Simbody  3.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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_