54 template <
typename T1,
typename T2>
57 using T1Stamped = std::pair<unsigned long, T1>;
58 using T2Stamped = std::pair<unsigned long, T2>;
61 std::mutex publish_mutex_;
62 std::deque<T1Stamped> queueT1;
63 std::deque<T2Stamped> queueT2;
65 using CallbackFunction = std::function<void(T1, T2,
unsigned long,
unsigned long)>;
67 std::map<int, CallbackFunction> cb_;
68 int callback_counter = 0;
74 std::unique_lock<std::mutex> publish_lock (publish_mutex_);
75 cb_[callback_counter] = callback;
76 return callback_counter++;
82 std::unique_lock<std::mutex> publish_lock (publish_mutex_);
87 add0 (
const T1& t,
unsigned long time)
90 queueT1.push_back (T1Stamped (time, t));
96 add1 (
const T2& t,
unsigned long time)
99 queueT2.push_back (T2Stamped (time, t));
109 std::unique_lock<std::mutex> lock1 (mutex1_);
110 std::unique_lock<std::mutex> lock2 (mutex2_);
112 for (
const auto& cb: cb_)
116 cb.second.operator()(queueT1.front ().second, queueT2.front ().second, queueT1.front ().first, queueT2.front ().first);
120 queueT1.pop_front ();
121 queueT2.pop_front ();
128 std::unique_lock<std::mutex> publish_lock (publish_mutex_);
130 std::unique_lock<std::mutex> lock1 (mutex1_);
131 if (queueT1.empty ())
133 T1Stamped t1 = queueT1.front ();
136 std::unique_lock<std::mutex> lock2 (mutex2_);
137 if (queueT2.empty ())
139 T2Stamped t2 = queueT2.front ();
142 bool do_publish =
false;
144 if (t1.first <= t2.first)
147 while (queueT1.size () > 1 && queueT1[1].first <= t2.first)
148 queueT1.pop_front ();
150 if (queueT1.size () > 1)
152 if ( (t2.first << 1) > (queueT1[0].first + queueT1[1].first) )
153 queueT1.pop_front ();
162 while (queueT2.size () > 1 && (queueT2[1].first <= t1.first) )
163 queueT2.pop_front ();
165 if (queueT2.size () > 1)
167 if ( (t1.first << 1) > queueT2[0].first + queueT2[1].first )
168 queueT2.pop_front ();
/brief This template class synchronizes two data streams of different types.
void removeCallback(int i)
int addCallback(const CallbackFunction &callback)
void add1(const T2 &t, unsigned long time)
void add0(const T1 &t, unsigned long time)