vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Message.h
Go to the documentation of this file.
1 
9 #ifndef MESSAGE_H
10 #define MESSAGE_H
11 
12 
13 //#include <vrecko/BaseClass.h>
14 #include <osg/Referenced>
15 #include <osg/Timer>
16 #include <osg/Vec2>
17 #include <osg/Vec3>
18 #include <osg/Vec4>
19 #include <osg/Quat>
20 #include <osg/Matrix>
21 #include <osg/Node>
22 
23 #include <vrecko/Export>
24 
25 #include <fstream>
26 
27 #include <string>
28 #include <map>
29 #include <vector>
30 
31 #include <xercesc/dom/DOMNode.hpp>
32 //#include <xercesc/util/XercesDefs.hpp>
33 //#include <helpers/XercesXMLUtils.h>
34 
35 
36 namespace vrecko {
37 
38 class VRECKO_EXPORT EnvironmentObject;
39 class VRECKO_EXPORT Scene;
40 
41 
43 enum EventType {
47 };
48 
49 /*
50 class VRECKO_EXPORT Message: public BaseClass {
51 
52  friend class Scene;
53 
54  public:
55  Message();
56  Message(std::string message_id, EnvironmentObject *_new_sender_ptr);
57  Message(void *image_ptr, Scene *scene_ptr);
58  virtual ~Message();
59 
60  typedef struct {
61  void *data_ptr;
62  long int data_size;
63  } MessageData;
64 
65  typedef std::map<std::string, MessageData> DataMap;
66 
67  void setID(const std::string &message_id) {id = message_id;}
68  inline const std::string &getID(void) const {return id;}
69 
70  void setSenderCompetenceLevel(int _new_sender_competence_level)
71  {_sender_competence_level = _new_sender_competence_level;}
72  int getSenderCompetenceLevel(void) {return _sender_competence_level;}
73 
74  long int getTimeStamp(void) {return _time_stamp;}
75  EnvironmentObject *getSenderPtr(void) {return _sender_ptr;}
76  const std::string getIncludedData(void) {return _included_data;}
77 // inline int getApplyTo(void) {return _apply_to;}
78 
79  void setSpecifiedObjectID(long int _nso) {_specified_object_ID = _nso;}
80  inline long int getSpecifiedObjectID(void) {return _specified_object_ID;}
81 
82  void addData(const std::string &_dt, void *_ptr, long int size);
83  bool getDataMessageStruct(const std::string &dt, MessageData &msg_data);
84  void *getDataPtr(const std::string &dt);
85  bool getDataPtr(const std::string &dt, void **data_ptr);
86  bool dataPresent(const std::string &dt);
87  inline bool isEmpty() {return (_data.size() == 0);}
88 
89  bool setImage(void *image_ptr, Scene *scene_ptr);
90  void *getImage(long int &size);
91 
92  //
93  const DataMap::iterator getFirstIterator() {return _data.begin();}
94  const DataMap::iterator getNextIterator(DataMap::iterator &itr) {
95  itr++;
96  if (itr != _data.end()) return itr;
97  else return *((DataMap::iterator*)NULL);
98  }
99 
100  void update(void) {};
101 
102  protected:
103  std::string id;
104  osg::Timer_t _time_stamp;
105  osg::Timer _timer;
106  EnvironmentObject *_sender_ptr;
107  int _sender_competence_level;
108  std::string _included_data; // deprecated
109 // int _apply_to;
110  DataMap _data;
111  long int _specified_object_ID;
112 
113  void setTimeStamp(osg::Timer_t _new_ts) {_time_stamp = _new_ts;}
114 
115 };
116 */
117 
118 
119 /* The base message class, all other messages are derived from this one. */
120 class VreckoMessage : public osg::Referenced {
121 public:
123  virtual ~VreckoMessage() {}; // the destructor should be virtual (see http://blogs.msdn.com/b/oldnewthing/archive/2004/05/07/127826.aspx )
124 
126  virtual const char* getMessageId() { return "VreckoMessage"; }
127 
135  virtual bool setValueFromString(const char *valueAsString) { return false; };
136 
144  virtual bool isEqualTo(VreckoMessage *otherMsg) { return false; };
145 
146 protected:
147 };
148 
149 
150 class MessageVoid : public VreckoMessage {
151 public:
152  virtual const char* getMessageId() { return "MessageVoid"; }
153 
155 
156  virtual bool setValueFromString(const char *valueAsString) { return true; };
157  virtual bool isEqualTo(VreckoMessage *otherMsg) { return true; };
158 };
159 
160 
162 {
163 public:
165  MessageSensorTransformation(int number, osg::Vec3 &position, osg::Quat &orientation) { _sensorNumber = number; _sensorPosition = position; _sensorOrientation = orientation; }
166 
168  osg::Vec3 _sensorPosition;
170 };
171 
172 
173 
184 #define VRECKO_START_MESSAGE_BASE(messageName, dataType) \
185 class messageName : public vrecko::VreckoMessage { \
186 public: \
187  messageName() {}; \
188  virtual const char* getMessageId() { return #messageName; } \
189  dataType data;
190 
191 
200 #define VRECKO_START_MESSAGE_EXTENDED(messageName, dataType) \
201  VRECKO_START_MESSAGE_BASE(messageName, dataType) \
202  messageName(dataType newData) { data = newData; }; \
203  virtual bool isEqualTo(messageName *otherMsg) { return (data == (otherMsg)->data); }; \
204 
205 
211 #define VRECKO_END_MESSAGE \
212  };
213 
214 
215 
216 
217 
218 
219 
235 VRECKO_START_MESSAGE_EXTENDED(MessageBool, bool)
236  virtual bool setValueFromString(const char *valueAsString) { data = (atoi(valueAsString) > 0); return true; };
238 
239 
240 VRECKO_START_MESSAGE_EXTENDED(MessageInt, int)
241  virtual bool setValueFromString(const char *valueAsString) { data = atoi(valueAsString); return true; };
243 
244 
245 VRECKO_START_MESSAGE_EXTENDED(MessageUnsignedInt, unsigned int)
246  virtual bool setValueFromString(const char *valueAsString) { data = (unsigned int)atol(valueAsString); return true; };
248 
249 
250 VRECKO_START_MESSAGE_EXTENDED(MessageFloat, float)
251  virtual bool setValueFromString(const char *valueAsString) { data = atof(valueAsString); return true; };
253 
254 
255 VRECKO_START_MESSAGE_EXTENDED(MessageObjectID, unsigned long)
256  virtual bool setValueFromString(const char *valueAsString) { data = atol(valueAsString); return true; };
258 
259 
261  MessageString(const std::string &_data) { data = _data; };
262  MessageString(const char *_data) { data = _data; };
263  virtual bool setValueFromString(const char *valueAsString) { data = valueAsString; return true; };
264  virtual bool isEqualTo(VreckoMessage *otherMsg) { return !data.compare(((MessageString*)otherMsg)->data); };
266 
267 
269  MessageVec2(const osg::Vec2 &_data) { data = _data; };
270  virtual bool setValueFromString(const char *valueAsString) { return (2 == sscanf_s(valueAsString, "%f %f", &data.x(), &data.y())); };
271  virtual bool isEqualTo(VreckoMessage *otherMsg) { return (data == ((MessageVec2*)otherMsg)->data); };
273 
274 
276  MessageVec3(const osg::Vec3 &_data) { data = _data; };
277  virtual bool setValueFromString(const char *valueAsString) { return (3 == sscanf_s(valueAsString, "%f %f %f", &data.x(), &data.y(), &data.z())); };
278  virtual bool isEqualTo(VreckoMessage *otherMsg) { return (data == ((MessageVec3*)otherMsg)->data); };
280 
281 
283  MessageVec4(const osg::Vec4 &_data) { data = _data; };
284  virtual bool setValueFromString(const char *valueAsString) { return (4 == sscanf_s(valueAsString, "%f %f %f %f", &data.x(), &data.y(), &data.z(), &data.w())); };
285  virtual bool isEqualTo(VreckoMessage *otherMsg) { return (data == ((MessageVec4*)otherMsg)->data); };
287 
288 
290  MessageQuat(const osg::Quat &_data) { data = _data; };
291  virtual bool setValueFromString(const char *valueAsString) { return (4 == sscanf_s(valueAsString, "%f %f %f %f", &data.x(), &data.y(), &data.z(), &data.w())); };
292  virtual bool isEqualTo(VreckoMessage *otherMsg) { return (data == ((MessageQuat*)otherMsg)->data); };
294 
295 
297  MessageMatrix(const osg::Matrix &_data) { data = _data; };
298  virtual bool setValueFromString(const char *valueAsString) { return (16 == sscanf_s(valueAsString, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
299  &data(0, 0), &data(0, 1), &data(0, 2), &data(0, 3),
300  &data(1, 0), &data(1, 1), &data(1, 2), &data(1, 3),
301  &data(2, 0), &data(2, 1), &data(2, 2), &data(2, 3),
302  &data(3, 0), &data(3, 1), &data(3, 2), &data(3, 3)
303  )); };
304  virtual bool isEqualTo(VreckoMessage *otherMsg) { return (data == ((MessageMatrix*)otherMsg)->data); };
306 
307 
308 VRECKO_START_MESSAGE_BASE(MessageNodeList, std::vector<osg::Node*>)
309  MessageNodeList(const std::vector<osg::Node*> &_data) { data = _data; };
311 
312 
313 VRECKO_START_MESSAGE_BASE(MessageNode, osg::ref_ptr<osg::Node>)
314  MessageNode(osg::Node * newNode) { data = newNode; };
316 
317 class XERCES_CPP_NAMESPACE_QUALIFIER DOMNode;
318 
319 VRECKO_START_MESSAGE_BASE(MessageXMLNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *)
320  MessageXMLNode(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode * newNode) { data = newNode; };
322 
323 }
324 
325 
326 #endif // MESSAGE_H
327