m2etis  0.4
M2Message.h
Go to the documentation of this file.
1 /*
2  Copyright (2016) Michael Baer, Daniel Bonrath, All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
22 #ifndef __M2ETIS_MESSAGE_M2MESSAGE_H__
23 #define __M2ETIS_MESSAGE_M2MESSAGE_H__
24 
25 #include "m2etis/config/GeneratedEventTypes.h"
27 
28 #include "boost/make_shared.hpp"
29 #include "boost/serialization/export.hpp"
30 #include "boost/serialization/list.hpp"
31 #include "boost/serialization/map.hpp"
32 #include "boost/serialization/shared_ptr.hpp"
33 #include "boost/serialization/vector.hpp"
34 
35 namespace m2etis {
36 namespace message {
37 
38  template<class EventType>
39  class M2Message {
40  public:
41  // Message Ptr
42  typedef boost::shared_ptr<M2Message<EventType> > Ptr;
43  // Payload Ptr
44  typedef boost::shared_ptr<EventType> PayloadPtr;
45 
47 
51  PayloadPtr payload;
52 
53  M2Message() : typePtr(), payload(boost::make_shared<EventType>()) {
54  }
55 
56  explicit M2Message(MessageType * mt) : typePtr(mt), payload(boost::make_shared<EventType>()) {
57  }
58 
59  M2Message(const EventType & v, MessageType * mt) : typePtr(mt), payload(boost::make_shared<EventType>(v)) {
60  }
61 
62  M2Message(const M2Message & msg, MessageType * mt) : typePtr(mt), payload(boost::make_shared<EventType>(*msg.payload)) {
63  }
64 
65  virtual ~M2Message() {}
66 
67  // TODO: (Michael) remove. won't work for most EventTypes
68  // TODO: (Daniel) currently used by one of the testcases, needs to be rebuilt there
69  inline std::string getPayloadString() {
70  return std::string(payload->begin(), payload->end());
71  }
72 
73  private:
75 
76  template<typename Archive>
77  void serialize(Archive & ar, const unsigned int /*version*/) {
78 #ifndef WITH_SIM
79  ar & payload; // Don't serialize payload for simulator, payload size is set in config
80 #endif /* WITH_SIM */
81  }
82  };
83 
85  std::ostream & operator<<(std::ostream & s, const M2etisMessage::Ptr);
86 
87 #ifdef WITH_SIM
88  typedef M2Message<SimulationEventType> M2SimMessage;
89  std::ostream & operator<<(std::ostream & s, const M2SimMessage::Ptr);
90 #endif /* WITH_SIM */
91 
92 } /* namespace message */
93 } /* namespace m2etis */
94 
95 #endif /* __M2ETIS_MESSAGE_M2MESSAGE_H__ */
96 
boost::shared_ptr< M2Message< EventType > > Ptr
Definition: M2Message.h:42
std::ostream & operator<<(std::ostream &s, const M2etisMessage::Ptr)
PayloadPtr payload
payload
Definition: M2Message.h:51
std::string getPayloadString()
Definition: M2Message.h:69
M2Message< std::vector< unsigned char > > M2etisMessage
Definition: M2Message.h:84
M2Message(const EventType &v, MessageType *mt)
Definition: M2Message.h:59
M2Message(MessageType *mt)
Definition: M2Message.h:56
M2Message(const M2Message &msg, MessageType *mt)
Definition: M2Message.h:62
boost::shared_ptr< EventType > PayloadPtr
Definition: M2Message.h:44
MessageType * typePtr
Definition: M2Message.h:46
uint32_t MessageType
Definition: MessageType.h:35
friend class boost::serialization::access
Definition: M2Message.h:74