DataObjectLocked.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 19 14:11:26 CET 2004 DataObjectLocked.hpp
3 
4  DataObjectLocked.hpp - description
5  -------------------
6  begin : Mon January 19 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 #ifndef CORELIB_DATAOBJECT_LOCKED_HPP
39 #define CORELIB_DATAOBJECT_LOCKED_HPP
40 
41 
42 #include "../os/MutexLock.hpp"
43 #include "DataObjectInterface.hpp"
44 
45 namespace RTT
46 { namespace base {
47 
56  template<class T>
58  : public DataObjectInterface<T>
59  {
63 
64  mutable os::Mutex lock;
65 
69  value_t data;
70 
71  mutable FlowStatus status;
73 
74  public:
79  : data(), status(NoData), initialized(false) {}
80 
84  DataObjectLocked( param_t initial_value )
85  : data(initial_value), status(NoData), initialized(true) {}
86 
90  typedef T DataType;
91 
92  virtual FlowStatus Get( reference_t pull, bool copy_old_data = true ) const {
93  os::MutexLock locker(lock);
94  FlowStatus result = status;
95  if (status == NewData) {
96  pull = data;
97  status = OldData;
98  } else if ((status == OldData) && copy_old_data) {
99  pull = data;
100  }
101  return result;
102  }
103 
104  virtual value_t Get() const {
105  DataType cache = DataType();
106  Get(cache);
107  return cache;
108  }
109 
110  virtual bool Set( param_t push ) {
111  os::MutexLock locker(lock);
112  data = push;
113  status = NewData;
114  return true;
115  }
116 
117  virtual bool data_sample( param_t sample, bool reset ) {
118  os::MutexLock locker(lock);
119  if (!initialized || reset) {
120  data = sample;
121  status = NoData;
122  initialized = true;
123  return true;
124  } else {
125  return initialized;
126  }
127  }
128 
132  virtual value_t data_sample() const {
133  os::MutexLock locker(lock);
134  return data;
135  }
136 
137  virtual void clear() {
138  os::MutexLock locker(lock);
139  status = NoData;
140  }
141  };
142 }}
143 
144 #endif
145 
DataObjectLocked(param_t initial_value)
DataObjectInterface< T >::reference_t reference_t
virtual FlowStatus Get(reference_t pull, bool copy_old_data=true) const
FlowStatus
Definition: FlowStatus.hpp:56
virtual value_t data_sample() const
virtual value_t Get() const
boost::call_traits< T >::param_type param_t
virtual bool Set(param_t push)
DataObjectInterface< T >::param_t param_t
A DataObjectInterface implements multi-threaded read/write solutions.
DataObjectInterface< T >::value_t value_t
A class which provides locked/protected access to one typed element of data.
boost::call_traits< T >::reference reference_t
An object oriented wrapper around a non recursive mutex.
Definition: Mutex.hpp:92
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
virtual bool data_sample(param_t sample, bool reset)
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
Definition: MutexLock.hpp:51


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:32