iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MessageImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_BLACKBOARD_DETAILS_MESSAGEIMPL_H
37 #define INCLUDE_NRT_CORE_BLACKBOARD_DETAILS_MESSAGEIMPL_H
38 
40 
41 // ######################################################################
42 // Implementation of inline functions
43 // ######################################################################
45 { }
46 
48 { }
49 
50 inline std::string nrt::MessageBase::description() const
51 { return nrt::demangle(typeid(*this).name()); }
52 
53 inline std::shared_ptr<nrt::MessageBase::SplitPostersTuple>
54 nrt::MessageBase::createSubPosters(std::string const & module, std::string const & descriptor)
55 { return std::shared_ptr<nrt::MessageBase::SplitPostersTuple>(); }
56 
57 inline std::shared_ptr<nrt::MessageBase::SplitSubscribersTuple>
58 nrt::MessageBase::createSubSubscribers(nrt::MessageSubscriberCoreBase * parent,
59  std::string const & module, std::string const & descriptor)
60 { return std::shared_ptr<nrt::MessageBase::SplitSubscribersTuple>(); }
61 
62 template <class X> inline
63 void nrt::MessageBase::doSplitPost(nrt::MessageBase::SplitPostersTuple const & sp,
64  std::list<std::future<X> > & fut) const
65 { }
66 
67 // ######################################################################
68 // MessageBase free functions
69 // ######################################################################
70 template <> inline
71 std::string const nrt::MessageType<void>()
72 { return "void"; }
73 
74 template <class Msg> inline
75 std::string const nrt::MessageType()
76 {
77  static_assert(std::is_base_of<nrt::MessageBase, Msg>::value, "Msg must be void or derive from nrt::MessageBase");
78  return nrt::demangle(typeid(Msg).name());
79 }
80 
81 // ######################################################################
82 // Message implementation
83 // ######################################################################
84 
85 template <class T> inline
86 nrt::Message<T>::Message() : value_(new T())
87 { }
88 
89 template <class T> inline
90 nrt::Message<T>::Message(T const & val) : value_(new T(val))
91 { }
92 
93 template <class T> inline
94 nrt::Message<T>::Message(std::shared_ptr<T> val) : value_(val)
95 { }
96 
97 template <class T> inline
99 { }
100 
101 template <class T> inline
103 { return *value_; }
104 
105 template <class T> inline
106 T const & nrt::Message<T>::value() const
107 { return *value_; }
108 
109 template <class T> inline
110 std::shared_ptr<T> nrt::Message<T>::valuePtr()
111 { return value_; }
112 
113 template <class T> inline
114 std::shared_ptr<T const> nrt::Message<T>::valuePtr() const
115 { return value_; }
116 
117 // ######################################################################
118 // MessageData implementation
119 // ######################################################################
120 template <class Field, class ... Tail> template <class Field2> inline
121 typename std::enable_if<std::is_same<Field, Field2>::value, typename Field2::DataType &>::type
122 nrt::MessageData<Field, Tail ...>::fieldValue()
123 { return *(Field::value_); }
124 
125 template <class Field, class ... Tail> template <class Field2> inline
126 typename std::enable_if< ! std::is_same<Field, Field2>::value, typename Field2::DataType &>::type
127 nrt::MessageData<Field, Tail ...>::fieldValue()
128 { return nrt::MessageData<Tail ...>::template fieldValue<Field2>(); }
129 
130 template <class Field, class ... Tail> template <class Field2> inline
131 typename std::enable_if<std::is_same<Field, Field2>::value, typename Field2::DataType const &>::type
132 nrt::MessageData<Field, Tail ...>::fieldValue() const
133 { return *(Field::value_); }
134 
135 template <class Field, class ... Tail> template <class Field2> inline
136 typename std::enable_if< ! std::is_same<Field, Field2>::value, typename Field2::DataType const &>::type
137 nrt::MessageData<Field, Tail ...>::fieldValue() const
138 { return nrt::MessageData<Tail ...>::template fieldValue<Field2>(); }
139 
140 template <class Field, class ... Tail> template <class Field2> inline
141 typename std::enable_if<std::is_same<Field, Field2>::value,
142  typename std::shared_ptr<typename Field2::DataType> >::type
143 nrt::MessageData<Field, Tail ...>::fieldValuePtr()
144 { return Field::value_; }
145 
146 template <class Field, class ... Tail> template <class Field2> inline
147 typename std::enable_if< ! std::is_same<Field, Field2>::value,
148  typename std::shared_ptr<typename Field2::DataType> >::type
149 nrt::MessageData<Field, Tail ...>::fieldValuePtr()
150 { return MessageData<Tail ...>::template fieldValuePtr<Field2>(); }
151 
152 template <class Field, class ... Tail> template <class Field2> inline
153 typename std::enable_if<std::is_same<Field, Field2>::value,
154  typename std::shared_ptr<typename Field2::DataType const> >::type
155 nrt::MessageData<Field, Tail ...>::fieldValuePtr() const
156 { return Field::value_; }
157 
158 template <class Field, class ... Tail> template <class Field2> inline
159 typename std::enable_if< ! std::is_same<Field, Field2>::value,
160  typename std::shared_ptr<typename Field2::DataType const> >::type
161 nrt::MessageData<Field, Tail ...>::fieldValuePtr() const
162 { return MessageData<Tail ...>::template fieldValuePtr<Field2>(); }
163 
164 template <class Field, class ... Tail> template <class Field2> inline
165 typename std::enable_if<std::is_same<Field, Field2>::value, char const *>::type
166 nrt::MessageData<Field, Tail ...>::fieldName() const
167 { return Field::name(); }
168 
169 template <class Field, class ... Tail> template <class Field2> inline
170 typename std::enable_if< ! std::is_same<Field, Field2>::value, char const *>::type
171 nrt::MessageData<Field, Tail ...>::fieldName() const
172 { return MessageData<Tail ...>::template fieldName<Field2>(); }
173 
174 template <class Field, class ... Tail> template <class Field2> inline
175 typename std::enable_if<std::is_same<Field, Field2>::value, char const *>::type
176 nrt::MessageData<Field, Tail ...>::fieldDescription() const
177 { return Field::description(); }
178 
179 template <class Field, class ... Tail> template <class Field2> inline
180 typename std::enable_if< ! std::is_same<Field, Field2>::value, char const *>::type
181 nrt::MessageData<Field, Tail ...>::fieldDescription() const
182 { return MessageData<Tail ...>::template fieldDescription<Field2>(); }
183 
184 // ######################################################################
185 // CompositeMessage implementation
186 // ######################################################################
187 template <class ... Fields> template <size_t I> inline
188 typename std::enable_if<I == nrt::CompositeMessage<Fields ...>::msgsize, void>::type
191  std::string const & descriptor)
192 { }
193 
194 template <class ... Fields> template <size_t I> inline
195 typename std::enable_if<I < nrt::CompositeMessage<Fields ...>::msgsize, void>::type
198  std::string const & descriptor)
199 {
200  typedef typename std::tuple_element<I, TupleType>::type IType;
201  std::string const des = descriptor + '.' + IType::name();
202 
203  std::get<I>(sp).reset(new nrt::SplitMessagePoster<typename IType::DataType>
204  (module, des, std::string("Poster for field ") + des + " [" +
205  IType::description() + ']'));
206  createSubPostersInternal<I + 1>(sp, module, descriptor);
207 }
208 
209 template <class ... Fields> inline
210 std::shared_ptr<typename nrt::CompositeMessage<Fields ...>::SplitPostersTuple>
212 createSubPosters(std::string const & module, std::string const & descriptor)
213 {
214  std::shared_ptr<SplitPostersTuple> ret(new SplitPostersTuple);
215  createSubPostersInternal(*ret, module, descriptor);
216  return ret;
217 }
218 
219 
220 // ######################################################################
221 template <class ... Fields> template <size_t I> inline
222 typename std::enable_if<I == nrt::CompositeMessage<Fields ...>::msgsize, void>::type
224 doSplitPost(nrt::CompositeMessage<Fields ...>::SplitPostersTuple const & sp, std::list<std::future<void> > & fut) const
225 { }
226 
227 template <class ... Fields> template <size_t I> inline
228 typename std::enable_if<I < nrt::CompositeMessage<Fields ...>::msgsize, void>::type
230 doSplitPost(nrt::CompositeMessage<Fields ...>::SplitPostersTuple const & sp, std::list<std::future<void> > & fut) const
231 {
232  typedef typename std::tuple_element<I, TupleType>::type IType;
233  std::get<I>(sp)->doPostInternalWrapper(fut, IType::FieldType::value_);
234  doSplitPost<I+1>(sp, fut);
235 }
236 
237 // ######################################################################
238 template <class ... Fields> template <size_t I> inline
239 typename std::enable_if<I == nrt::CompositeMessage<Fields ...>::msgsize, void>::type
243  std::string const & module, std::string const & descriptor)
244 { }
245 
246 template <class ... Fields> template <size_t I> inline
247 typename std::enable_if<I < nrt::CompositeMessage<Fields ...>::msgsize, void>::type
251  std::string const & module, std::string const & descriptor)
252 {
253  typedef typename std::tuple_element<I, TupleType>::type IType;
254  std::string const des = descriptor + '.' + IType::name();
255 
256  std::get<I>(ss).reset(new nrt::SplitMessageSubscriber<typename IType::DataType>
257  (parent, this->IType::value_, I, module, des,
258  std::string("Subscriber for field ") + des + " [" + IType::description() + ']'));
259  createSubSubscribersInternal<I + 1>(ss, parent, module, descriptor);
260 }
261 
262 template <class ... Fields>
263 std::shared_ptr<typename nrt::CompositeMessage<Fields ...>::SplitSubscribersTuple>
265 createSubSubscribers(nrt::MessageSubscriberCoreBase * parent, std::string const & module,
266  std::string const & descriptor)
267 {
268  std::shared_ptr<SplitSubscribersTuple> ret(new SplitSubscribersTuple);
269  createSubSubscribersInternal(*ret, parent, module, descriptor);
270  return ret;
271 }
272 
273 #endif // INCLUDE_NRT_CORE_BLACKBOARD_DETAILS_MESSAGEIMPL_H