sot-core  4.11.8
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),
24  mainObjectMutex(),
25  mainObject(),
26  update(false)
27 
28  ,
29  SOUT(boost::bind(&Mailbox::get, this, _1, _2), sotNOSIGNAL,
30  "Mailbox(" + name + ")::output(Object)::sout"),
31  objSOUT(boost::bind(&Mailbox::getObject, this, _1, _2), SOUT,
32  "Mailbox(" + name + ")::output(Object)::object"),
33  timeSOUT(boost::bind(&Mailbox::getTimestamp, this, _1, _2), SOUT,
34  "Mailbox(" + name + ")::output(Object)::timestamp") {
35  signalRegistration(SOUT << objSOUT << timeSOUT);
36  SOUT.setDependencyType(TimeDependency<int>::BOOL_DEPENDENT);
37 }
38 
39 template <class Object>
41  boost::timed_mutex::scoped_lock lockMain(mainObjectMutex);
42 }
43 
44 /* -------------------------------------------------------------------------- */
45 /* --- ACCESS --------------------------------------------------------------- */
46 /* -------------------------------------------------------------------------- */
47 template <class Object>
49  boost::timed_mutex::scoped_try_lock lockMain(this->mainObjectMutex);
50 
51  if (lockMain.owns_lock()) {
52  return update;
53  } else {
54  return false;
55  }
56 }
57 
58 /* -------------------------------------------------------------------------- */
59 template <class Object>
62  const int & /*dummy*/) {
63  boost::timed_mutex::scoped_try_lock lockMain(this->mainObjectMutex);
64 
65  if (lockMain.owns_lock()) {
66  res.timestamp.tv_sec = this->mainTimeStamp.tv_sec;
67  res.timestamp.tv_usec = this->mainTimeStamp.tv_usec;
68 
69  update = false;
70  res.obj = this->mainObject;
71  }
72 
73  return res;
74 }
75 
76 /* -------------------------------------------------------------------------- */
77 template <class Object>
78 void Mailbox<Object>::post(const Object &value) {
79  boost::timed_mutex::scoped_lock lockMain(this->mainObjectMutex);
80  mainObject = value;
81  gettimeofday(&this->mainTimeStamp, NULL);
82  update = true;
83  SOUT.setReady();
84 
85  return;
86 }
87 
88 template <class Object>
89 Object &Mailbox<Object>::getObject(Object &res, const int &time) {
90  const sotTimestampedObject &data = SOUT(time);
91  res = data.obj;
92  return res;
93 }
94 
95 template <class Object>
96 timeval &Mailbox<Object>::getTimestamp(struct timeval &res, const int &time) {
97  const sotTimestampedObject &data = SOUT(time);
98  res.tv_sec = data.timestamp.tv_sec;
99  res.tv_usec = data.timestamp.tv_usec;
100  return res;
101 }
102 
103 } /* namespace sot */
104 } /* namespace dynamicgraph */
105 /* Macro for template specialization */
106 #ifndef WIN32
107 #define MAILBOX_TEMPLATE_SPE(S) \
108  namespace dynamicgraph { \
109  namespace sot { \
110  template void Mailbox<S>::post(const S &obj); \
111  template dynamicgraph::Vector &Mailbox<S>::getObject(S &res, \
112  const int &time); \
113  template bool Mailbox<S>::hasBeenUpdated(void); \
114  template Mailbox<S>::~Mailbox(); \
115  template Mailbox<S>::sotTimestampedObject &Mailbox<S>::get( \
116  Mailbox<S>::sotTimestampedObject &res, const int &dummy); \
117  template Mailbox<S>::Mailbox(const std::string &name); \
118  } \
119  } // namespace sot namespace dynamicgraph
120 #endif // WIN32
121 
122 #endif // #ifdef __SOT_MAILBOX_T_CPP
Definition: mailbox.hh:43
struct timeval & getTimestamp(struct timeval &res, const int &time)
Definition: mailbox.hxx:96
sotTimestampedObject & get(sotTimestampedObject &res, const int &dummy)
Definition: mailbox.hxx:60
bool hasBeenUpdated(void)
Definition: mailbox.hxx:48
void post(const Object &obj)
Definition: mailbox.hxx:78
~Mailbox(void)
Definition: mailbox.hxx:40
Mailbox(const std::string &name)
Definition: mailbox.hxx:22
dynamicgraph::SignalTimeDependent< struct timeval, int > timeSOUT
Definition: mailbox.hh:72
Object & getObject(Object &res, const int &time)
Definition: mailbox.hxx:89
dynamicgraph::SignalTimeDependent< Object, int > objSOUT
Definition: mailbox.hh:71
dynamicgraph::SignalTimeDependent< sotTimestampedObject, int > SOUT
Definition: mailbox.hh:70
Definition: abstract-sot-external-interface.hh:17
struct timeval timestamp
Definition: mailbox.hh:39
Object obj
Definition: mailbox.hh:38