sot-core  4.11.2
Hierarchical task solver plug-in for dynamic-graph.
mailbox.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 #ifndef __SOT_MAILBOX_T_CPP
11 #define __SOT_MAILBOX_T_CPP
12 
13 #include <sot/core/mailbox.hh>
14 
15 namespace dynamicgraph {
16 namespace sot {
17 
18 /* -------------------------------------------------------------------------- */
19 /* --- CONSTRUCTION --------------------------------------------------------- */
20 /* -------------------------------------------------------------------------- */
21 template <class Object>
22 Mailbox<Object>::Mailbox(const std::string &name)
23  : Entity(name), mainObjectMutex(), mainObject(), update(false)
24 
25  ,
26  SOUT(boost::bind(&Mailbox::get, this, _1, _2), sotNOSIGNAL,
27  "Mailbox(" + name + ")::output(Object)::sout"),
28  objSOUT(boost::bind(&Mailbox::getObject, this, _1, _2), SOUT,
29  "Mailbox(" + name + ")::output(Object)::object"),
30  timeSOUT(boost::bind(&Mailbox::getTimestamp, this, _1, _2), SOUT,
31  "Mailbox(" + name + ")::output(Object)::timestamp") {
32  signalRegistration(SOUT << objSOUT << timeSOUT);
33  SOUT.setDependencyType(TimeDependency<int>::BOOL_DEPENDENT);
34 }
35 
36 template <class Object> Mailbox<Object>::~Mailbox(void) {
37  boost::timed_mutex::scoped_lock lockMain(mainObjectMutex);
38 }
39 
40 /* -------------------------------------------------------------------------- */
41 /* --- ACCESS --------------------------------------------------------------- */
42 /* -------------------------------------------------------------------------- */
43 template <class Object> bool Mailbox<Object>::hasBeenUpdated(void) {
44  boost::timed_mutex::scoped_try_lock lockMain(this->mainObjectMutex);
45 
46  if (lockMain.owns_lock()) {
47  return update;
48  } else {
49  return false;
50  }
51 }
52 
53 /* -------------------------------------------------------------------------- */
54 template <class Object>
57  const int & /*dummy*/) {
58  boost::timed_mutex::scoped_try_lock lockMain(this->mainObjectMutex);
59 
60  if (lockMain.owns_lock()) {
61  res.timestamp.tv_sec = this->mainTimeStamp.tv_sec;
62  res.timestamp.tv_usec = this->mainTimeStamp.tv_usec;
63 
64  update = false;
65  res.obj = this->mainObject;
66  }
67 
68  return res;
69 }
70 
71 /* -------------------------------------------------------------------------- */
72 template <class Object> void Mailbox<Object>::post(const Object &value) {
73  boost::timed_mutex::scoped_lock lockMain(this->mainObjectMutex);
74  mainObject = value;
75  gettimeofday(&this->mainTimeStamp, NULL);
76  update = true;
77  SOUT.setReady();
78 
79  return;
80 }
81 
82 template <class Object>
83 Object &Mailbox<Object>::getObject(Object &res, const int &time) {
84  const sotTimestampedObject &data = SOUT(time);
85  res = data.obj;
86  return res;
87 }
88 
89 template <class Object>
90 timeval &Mailbox<Object>::getTimestamp(struct timeval &res, const int &time) {
91  const sotTimestampedObject &data = SOUT(time);
92  res.tv_sec = data.timestamp.tv_sec;
93  res.tv_usec = data.timestamp.tv_usec;
94  return res;
95 }
96 
97 } /* namespace sot */
98 } /* namespace dynamicgraph */
99 /* Macro for template specialization */
100 #ifndef WIN32
101 #define MAILBOX_TEMPLATE_SPE(S) \
102  namespace dynamicgraph { \
103  namespace sot { \
104  template void Mailbox<S>::post(const S &obj); \
105  template dynamicgraph::Vector &Mailbox<S>::getObject(S &res, \
106  const int &time); \
107  template bool Mailbox<S>::hasBeenUpdated(void); \
108  template Mailbox<S>::~Mailbox(); \
109  template Mailbox<S>::sotTimestampedObject & \
110  Mailbox<S>::get(Mailbox<S>::sotTimestampedObject &res, const int &dummy); \
111  template Mailbox<S>::Mailbox(const std::string &name); \
112  } \
113  } // namespace sot namespace dynamicgraph
114 #endif // WIN32
115 
116 #endif // #ifdef __SOT_MAILBOX_T_CPP
Mailbox(const std::string &name)
Definition: mailbox.hxx:22
dynamicgraph::SignalTimeDependent< struct timeval, int > timeSOUT
Definition: mailbox.hh:70
struct timeval timestamp
Definition: mailbox.hh:38
boost::timed_mutex mainObjectMutex
Definition: mailbox.hh:62
bool update
Definition: mailbox.hh:65
void post(const Object &obj)
Definition: mailbox.hxx:72
dynamicgraph::SignalTimeDependent< Object, int > objSOUT
Definition: mailbox.hh:69
~Mailbox(void)
Definition: mailbox.hxx:36
sotTimestampedObject & get(sotTimestampedObject &res, const int &dummy)
Definition: mailbox.hxx:56
Definition: mailbox.hh:41
struct timeval & getTimestamp(struct timeval &res, const int &time)
Definition: mailbox.hxx:90
bool hasBeenUpdated(void)
Definition: mailbox.hxx:43
Object obj
Definition: mailbox.hh:37
struct timeval mainTimeStamp
Definition: mailbox.hh:64
Object mainObject
Definition: mailbox.hh:63
dynamicgraph::SignalTimeDependent< sotTimestampedObject, int > SOUT
Definition: mailbox.hh:68
Definition: abstract-sot-external-interface.hh:17
Object & getObject(Object &res, const int &time)
Definition: mailbox.hxx:83