Wolframe, 0.0.3

sharedReference.hpp
Go to the documentation of this file.
1 /************************************************************************
2 Copyright (C) 2011 - 2014 Project Wolframe.
3 All rights reserved.
4 
5 This file is part of Project Wolframe.
6 
7 Commercial Usage
8 Licensees holding valid Project Wolframe Commercial licenses may
9 use this file in accordance with the Project Wolframe
10 Commercial License Agreement provided with the Software or,
11 alternatively, in accordance with the terms contained
12 in a written agreement between the licensee and Project Wolframe.
13 
14 GNU General Public License Usage
15 Alternatively, you can redistribute this file and/or modify it
16 under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
19 
20 Wolframe is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
27 
28 If you have questions regarding the use of this file, please contact
29 Project Wolframe.
30 
31 ************************************************************************/
32 #ifndef _Wolframe_SHARED_REFERENCE_HPP_INCLUDED
33 #define _Wolframe_SHARED_REFERENCE_HPP_INCLUDED
34 
37 #include <cstddef>
38 #include <boost/shared_ptr.hpp>
39 
40 namespace _Wolframe {
41 namespace types {
42 
46 template <class OBJ>
48 {
49 public:
52  struct Pointer
53  {
54  OBJ* m_ptr;
55 
57  explicit Pointer( OBJ* ptr_=0)
58  :m_ptr(ptr_){}
61  {
62  if (m_ptr) delete m_ptr;
63  }
64  };
65 
69  :m_ref(o.m_ref){}
70 
73  SharedReference( OBJ* ptr=0)
74  :m_ref(boost::shared_ptr<Pointer>( new Pointer( ptr))){}
75 
77  virtual ~SharedReference(){}
78 
81  {
82  m_ref = o.m_ref;
83  return *this;
84  }
85 
88  OBJ* get() const
89  {
90  return m_ref->m_ptr;
91  }
92 
95  void reset( OBJ* ptr=0)
96  {
97  if (m_ref->m_ptr && m_ref->m_ptr != ptr) delete m_ref->m_ptr;
98  m_ref->m_ptr = ptr;
99  }
100 
103  OBJ* operator -> () const
104  {
105  return m_ref->m_ptr;
106  }
107 
110  OBJ& operator *() const
111  {
112  return *m_ref->m_ptr;
113  }
114 
115 private:
116  boost::shared_ptr<Pointer> m_ref;
117 };
118 
119 }}//namespace
120 #endif
121 
122 
123 
Pointer(OBJ *ptr_=0)
Constructor.
Definition: sharedReference.hpp:57
OBJ * m_ptr
object pointer
Definition: sharedReference.hpp:54
boost::shared_ptr< Pointer > m_ref
shared reference
Definition: sharedReference.hpp:116
~Pointer()
Destructor.
Definition: sharedReference.hpp:60
Pointer for reference with another indirection to make the exchange of the object possible...
Definition: sharedReference.hpp:52
virtual ~SharedReference()
Destructor.
Definition: sharedReference.hpp:77
Shared reference to an object exchangeable for all owners in a single thread context.
Definition: sharedReference.hpp:47
SharedReference(const SharedReference &o)
Copy constructor.
Definition: sharedReference.hpp:68
SharedReference & operator=(const SharedReference &o)
Assignment.
Definition: sharedReference.hpp:80
SharedReference(OBJ *ptr=0)
Constructor.
Definition: sharedReference.hpp:73
OBJ * operator->() const
Pointer access operator.
Definition: sharedReference.hpp:103
void reset(OBJ *ptr=0)
Exchange the object (not thread safe access)
Definition: sharedReference.hpp:95
OBJ & operator*() const
Reference access operator.
Definition: sharedReference.hpp:110