iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MessageHelpers.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_MESSAGEHELPERS_H
37 #define INCLUDE_NRT_CORE_BLACKBOARD_DETAILS_MESSAGEHELPERS_H
38 
39 #include <nrt/External/cereal/archives/binary.hpp>
40 
41 namespace nrt
42 {
43 
44  //! Simple class used to contain common types for a given message
45  /*! This class is used in NRT_DECLARE_MESSAGE to automatically expose the some convenient typedefs */
46  template<class __Type>
48  {
49  public:
50  //! A shared constant pointer to this message type (messages are always received as this type)
51  typedef std::shared_ptr<__Type const> const_ptr;
52 
53  //! A shared unique pointer to this message type (messages are always sent and returned as this type)
54  typedef std::unique_ptr<__Type> unique_ptr;
55  };
56 
57  //! The default output archive type for NRT
58  typedef cereal::BinaryOutputArchive oarchive;
59 
60  //! The default input archive type fo NRT
61  typedef cereal::BinaryInputArchive iarchive;
62 
63  class MessageBase;
64 
65  //! Wrap an std::unique_ptr around the given object pointer
66  /*! Note that this cannot be used to do:
67  @code
68  Port::post( make_unique( new Message ) );
69  @endcode
70  Instead, it's necessary to do:
71  @code
72  auto message = make_unique( new Message() ) );
73  Port::post( message );
74  @endcode
75  \ingroup message */
76  template <typename T> inline
77  std::unique_ptr<T> make_unique(T * const object)
78  { return std::unique_ptr<T>( object ); }
79 
80  // ######################################################################
81  template <class ... Fields> class MessageData;
82  template <> class MessageData<> { };
83 
84  template <class Field, class Type>
85  class MessageField
86  {
87  public:
88  MessageField() : value_(new Type()) { }
89 
90  typedef Field FieldType;
91  typedef Type DataType;
92 
93  protected:
94  template <class ... Fields> friend class MessageData;
95  std::shared_ptr<Type> value_;
96  };
97 }
98 
99 // Convenience macro to define a MessageField
100 #define NRT_DECLARE_MESSAGEFIELD(FieldName, Type, Description) \
101  struct FieldName##_NRT__ : public nrt::MessageField<FieldName##_NRT__, Type> { \
102  constexpr static char const* description() { return Description; } \
103  constexpr static char const* name() { return #FieldName; } \
104  Type & FieldName() { return *value_; } \
105  Type const & FieldName() const { return *value_; } \
106  };
107 
108 // Convenience macro to access a MessageField
109 #define NRT_MESSAGEFIELD(x) x##_NRT__
110 
111 
112 #endif // INCLUDE_NRT_CORE_BLACKBOARD_DETAILS_MESSAGEHELPERS_H
113