iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AnyMessage.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
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 #ifndef INCLUDE_NRT_CORE_BLACKBOARD_ANYMESSAGE_H
36 #define INCLUDE_NRT_CORE_BLACKBOARD_ANYMESSAGE_H
37 
39 #include <nrt/External/cereal/types/string.hpp>
40 
41 namespace nrt
42 {
43  class Blackboard;
44 
45  // ######################################################################
46  //! Generic opaque message that can contain any message data
47  /*! nrt::AnyMessage cannot be directly constructed by users. Rather, it can be received (via a MessageSubscriber
48  port), possibly queued, etc and then re-posted (via a MessagePoster port). nrt::AnyMessage is useful to construct
49  very general modules which do not need to know or look into the actual contents of a message. This includes
50  queues, stacks, temporal sub-samplers or over-samplers, synchronizers, barriers, sequencers, dispatchers, etc.
51 
52  See \ref c_Blackboard for general conceptual motivation for AnyMessage.
53 
54  See FifoQueueModule and other Generic modules in nrtbase for examples.
55 
56  \ingroup message */
57  class AnyMessage : public nrt::MessageBase, public nrt::MessageTypesCreator<AnyMessage>
58  {
59  public:
60  //! Virtual destructor
61  ~AnyMessage();
62 
63  //! Return the underlying native message type as a string
64  /*! Should only be used for debugging/info messages as the underlying string representation may change with versions
65  of the C++ compiler used or with other factors. */
66  std::string const & typeStr() const;
67 
68  private:
69  friend class Blackboard;
70  template <class Posting> friend class MessagePosterCore;
71  template <class Subscription> friend class MessageSubscriberCore;
72  template <class Checking> friend class MessageCheckerCore;
73 
74  //! Constructor from a typed message (private and only accessible to friends)
75  template <class Msg>
76  AnyMessage(std::shared_ptr<Msg const> msg);
77 
78  //! Default constructor, needed by serialization and should otherwise never been used
79  AnyMessage();
80 
81  //! Get the typed message out
82  /*! @throws BlackboardException of sub-type BadAnyMessageCast if trying to get a different type than the one
83  actually in the AnyMessage */
84  template <class Msg>
85  typename std::shared_ptr<Msg const> get() const;
86 
87  std::string msgtype; // message type as a string from nrt::MessageType<Msg>()
88  std::string sermsg; // serialized message
89  mutable std::shared_ptr<nrt::MessageBase const> msgbase; // deserialized/native message
90  std::function<void()> serfunc; // function to serialize msgbase into sermsg
91  mutable std::mutex mtx;
92 
93  friend class cereal::access;
94 
95  template<class Archive>
96  void save(Archive & ar) const;
97 
98  template<class Archive>
99  void load(Archive & ar);
100  };
101 
102 } // namespace nrt
103 
104 // Implementation details of no concern to end users:
106 
107 #endif // INCLUDE_NRT_CORE_BLACKBOARD_ANYMESSAGE_H