signal.hh
Go to the documentation of this file.
1 
37 #ifndef LibMultiSense_details_signal_hh
38 #define LibMultiSense_details_signal_hh
39 
41 #include "details/wire/Protocol.h"
43 
44 #include <map>
45 
46 namespace crl {
47 namespace multisense {
48 namespace details {
49 
50 //
51 // Here we provide a thread-safe, blocking, signaling
52 // interface for sensor message RX.
53 
54 class MessageWatch {
55 public:
56 
58  Status status=Status_Ok) {
60 
61  Map::iterator it = m_map.find(id);
62 
63  if (m_map.end() != it)
64  it->second->post(status);
65  };
66 
67  void signal(const wire::Ack& ack) {
68  signal(ack.command, ack.status);
69  };
70 
71 private:
72 
73  friend class ScopedWatch;
74 
76  typedef std::map<wire::IdType, Signal*> Map;
77 
78  void insert(wire::IdType type,
79  Signal *signalP) {
81 
82  Map::const_iterator it = m_map.find(type);
83 
84  //
85  // Hmm.. this will prohibit multiple threads
86  // simultaneously commanding the sensor with this
87  // message ID.
88 
89  if (m_map.end() != it)
90  CRL_EXCEPTION("ack signal already set for id=%d", type);
91 
92  m_map[type] = signalP;
93  };
94 
95  void remove(wire::IdType type) {
97 
98  Map::iterator it = m_map.find(type);
99 
100  if (m_map.end() == it)
101  CRL_EXCEPTION("ack signal not found for id=%d\n", type);
102 
103  m_map.erase(it);
104  };
105 
107  Map m_map;
108 };
109 
110  //
111  // Exception-safe [de]registration of signal handlers
112 
113 class ScopedWatch {
114 public:
115 
117  MessageWatch& m) : m_id(t), m_map(m) {
118  m_map.insert(m_id, &m_signal);
119  };
120 
122  m_map.remove(m_id);
123  };
124 
125  bool wait(Status& status,
126  const double& timeout) {
127  return m_signal.timedWait(status, timeout);
128  };
129 
130 private:
131 
135 };
136 
137 } // namespace details
138 } // namespace multisense
139 } // namespace crl
140 
141 
142 #endif // LibMultiSense_details_signal_hh
#define CRL_EXCEPTION(fmt,...)
Definition: Exception.hh:71
void insert(wire::IdType type, Signal *signalP)
Definition: signal.hh:78
MessageWatch::Signal m_signal
Definition: signal.hh:134
void signal(wire::IdType id, Status status=Status_Ok)
Definition: signal.hh:57
utility::WaitVar< Status > Signal
Definition: signal.hh:75
std::map< wire::IdType, Signal * > Map
Definition: signal.hh:76
Definition: channel.cc:56
static CRL_CONSTEXPR Status Status_Ok
ScopedWatch(wire::IdType t, MessageWatch &m)
Definition: signal.hh:116
bool wait(Status &status, const double &timeout)
Definition: signal.hh:125
void signal(const wire::Ack &ack)
Definition: signal.hh:67


multisense_lib
Author(s):
autogenerated on Sun Mar 14 2021 02:34:50