4S Device Communication Module Collection  Version 0.6-SNAPSHOT
Full documentation of the modules in the 4SDC collection (aimed at 4SDC module developers)
msgsender.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2015 The 4S Foundation (www.4s-online.dk)
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 
31 #ifndef MSGSENDER_H
32 #define MSGSENDER_H
33 
34 #include "msgsenderbase.h"
35 #include "msgaddrgenerator.h"
36 
37 #include <memory>
38 
39 namespace FSYS
40 {
60  template<class parentClass> class MsgSender : private MsgSenderBase,
61  virtual public MsgAddrGenerator<parentClass>
62  {
63  private:
64 
65  public:
89  template<class T> void broadcast(T &msg)
90  {
91  std::shared_ptr<BaseMsg> pMsg(new T(msg));
92  MsgAddr destination = static_cast<MsgAddr>(*this);
93  MsgSenderBase::broadcast(pMsg, typeid(T), destination);
94  }
95 
144  template<class T> void respond(T &msg, BaseMsg &received)
145  {
146  std::shared_ptr<BaseMsg> pMsg(new T(msg));
147  MsgAddr destination = received.getOriginAddr();
148  MsgSenderBase::send(pMsg, typeid(T), *this, destination);
149  }
150 
151  template<class T> void send(T &msg, const MsgAddr &destination)
152  {
153  std::shared_ptr<BaseMsg> pMsg(new T(msg));
154  MsgSenderBase::send(pMsg, typeid(T), *this, destination);
155  }
156 
157  };
158 }
159 #endif // MSGSENDER_H
Constructor to MsgAddrGenerator object.
Definition: msgaddr.h:43
The BaseMsg class is the base class for all messages.
Definition: basemsg.h:54
Type neutral message sender class.
Definition: msgsenderbase.h:51
Definition: basemsg.h:38
Contains interface declaration for the FSYS::MsgSenderBase class.
void send(std::shared_ptr< BaseMsg > &msg, const std::type_info &typeOfMsg, const FSYS::MsgAddr &sender, const MsgAddr &destination)
Type independent respond function.
Definition: msgsenderbase.cpp:61
void broadcast(T &msg)
Template function to broadcast messages.
Definition: msgsender.h:89
MsgAddr & getOriginAddr(void)
Get/return the origin address of the message.
Definition: basemsg.cpp:41
void respond(T &msg, BaseMsg &received)
Template function to respond to messages.
Definition: msgsender.h:144
Class enabling other classes to send messages.
Definition: msgsender.h:60
void broadcast(std::shared_ptr< BaseMsg > &msg, const std::type_info &typeOfMsg, const MsgAddr &sender)
Broadcast function, to broadcast messages.
Definition: msgsenderbase.cpp:53
Contains interface for a message address generator class.
The MsgAddr class contains a unique address in the message system.
Definition: msgaddr.h:52