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