OpenWalnut  1.4.0
WSharedObject.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSHAREDOBJECT_H
26 #define WSHAREDOBJECT_H
27 
28 #ifndef Q_MOC_RUN
29 #include <boost/thread.hpp>
30 #endif
31 
32 #include "WCondition.h"
33 #include "WSharedObjectTicket.h"
34 #include "WSharedObjectTicketRead.h"
35 #include "WSharedObjectTicketWrite.h"
36 
37 /**
38  * Wrapper around an object/type for thread safe sharing of objects among multiple threads. The advantage of this class over WFlag
39  * is, that WFlag just protects simple get/set operations, while this class can protect a whole bunch of operations on the
40  * encapsulated object.
41  */
42 template < typename T >
44 {
45 public:
46  /**
47  * Default constructor.
48  */
49  WSharedObject();
50 
51  /**
52  * Destructor.
53  */
54  virtual ~WSharedObject();
55 
56  /**
57  * The type protected by this shared object class
58  */
59  typedef T ValueT;
60 
61  /**
62  * Type for read tickets.
63  */
64  typedef boost::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket;
65 
66  /**
67  * Type for write tickets.
68  */
69  typedef boost::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket;
70 
71  /**
72  * Shared pointer abbreviation.
73  */
74  typedef boost::shared_ptr< WSharedObject< T > > SPtr;
75 
76  /**
77  * Const shared ptr abbreviation.
78  */
79  typedef boost::shared_ptr< WSharedObject< T > > ConstSPtr;
80 
81  /**
82  * Returns a ticket to get read access to the contained data. After the ticket is freed, the read lock vanishes.
83  *
84  * \return the read ticket
85  */
86  ReadTicket getReadTicket() const;
87 
88  /**
89  * Returns a ticket to get write access to the contained data. After the ticket is freed, the write lock vanishes.
90  *
91  * \param suppressNotify true if no notification should be send after unlocking.
92  *
93  * \return the ticket
94  */
95  WriteTicket getWriteTicket( bool suppressNotify = false ) const;
96 
97  /**
98  * This condition fires whenever the encapsulated object changed. This is fired automatically by endWrite().
99  *
100  * \return the condition
101  */
102  boost::shared_ptr< WCondition > getChangeCondition() const;
103 
104 protected:
105  /**
106  * The object wrapped by this class. This member is mutable as the \ref getReadTicket and \ref getWriteTicket functions are const but need a
107  * non-const reference to m_object.
108  */
109  mutable T m_object;
110 
111  /**
112  * The lock to ensure thread safe access. This member is mutable as the \ref getReadTicket and \ref getWriteTicket functions are const but need a
113  * non-const reference to m_lock.
114  */
115  mutable boost::shared_ptr< boost::shared_mutex > m_lock;
116 
117  /**
118  * This condition set fires whenever the contained object changes. This corresponds to the Observable pattern.
119  */
120  boost::shared_ptr< WCondition > m_changeCondition;
121 
122 private:
123 };
124 
125 template < typename T >
127  m_lock( new boost::shared_mutex ),
128  m_changeCondition( new WCondition() )
129 {
130  // init members
131 }
132 
133 template < typename T >
135 {
136  // clean up
137 }
138 
139 template < typename T >
140 boost::shared_ptr< WCondition > WSharedObject< T >::getChangeCondition() const
141 {
142  return m_changeCondition;
143 }
144 
145 template < typename T >
147 {
148  return boost::shared_ptr< WSharedObjectTicketRead< T > >(
149  new WSharedObjectTicketRead< T >( m_object, m_lock, boost::shared_ptr< WCondition >() )
150  );
151 }
152 
153 template < typename T >
155 {
156  if( suppressNotify )
157  {
158  return boost::shared_ptr< WSharedObjectTicketWrite< T > >(
159  new WSharedObjectTicketWrite< T >( m_object, m_lock, boost::shared_ptr< WCondition >() )
160  );
161  }
162  else
163  {
164  return boost::shared_ptr< WSharedObjectTicketWrite< T > >(
165  new WSharedObjectTicketWrite< T >( m_object, m_lock, m_changeCondition )
166  );
167  }
168 }
169 
170 #endif // WSHAREDOBJECT_H
171 
boost::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket
Type for write tickets.
Definition: WSharedObject.h:69
T ValueT
The type protected by this shared object class.
Definition: WSharedObject.h:59
T m_object
The object wrapped by this class.
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
WSharedObject()
Default constructor.
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.
Class which represents granted access to a locked object.
Wrapper around an object/type for thread safe sharing of objects among multiple threads.
Definition: WSharedObject.h:43
boost::shared_ptr< WSharedObject< T > > ConstSPtr
Const shared ptr abbreviation.
Definition: WSharedObject.h:79
boost::shared_ptr< boost::shared_mutex > m_lock
The lock to ensure thread safe access.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:47
boost::shared_ptr< WSharedObject< T > > SPtr
Shared pointer abbreviation.
Definition: WSharedObject.h:74
virtual ~WSharedObject()
Destructor.
boost::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:64
boost::shared_ptr< WCondition > m_changeCondition
This condition set fires whenever the contained object changes.
boost::shared_ptr< WCondition > getChangeCondition() const
This condition fires whenever the encapsulated object changed.
Class which represents granted access to a locked object.