Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  
igtlMessageHandlerMacro.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: OpenIGTLink Library
4 Module: git@github.com:openigtlink/OpenIGTLink.git
5 Language: C++
6
7 Copyright (c) Insight Software Consortium. All rights reserved.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
12
13=========================================================================*/
14
15#ifndef __igtlMessageHandlerMacro_h
16#define __igtlMessageHandlerMacro_h
17
18#include "igtlMessageHandler.h"
19
20// Description:
21// The igtlMessageHandlerClassMacro() macro is to help developers to
22// define message handler class. It generates a chlid class of igtl::MessageHandler.
23// The developer only needs to implement ProcessMessage() after calling this macro.
24// The following code shows how to define a handler that processes IMAGE message:
25//
26// igtlMessageHandlerClassMacro(igtl::ImageMessage, TestImageMessageHandler);
27// void TestImageMessageHandler::Process(igtl::ImageMessage * message)
28// {
29// // do something
30// }
31
32//#define igtlMessageHandlerClassMacro(messagetype, classname) \
33// template <typename messagetype> \
34// class classname : public MessageHandler<messagetype> \
35// { \
36// typedef classname Self; \
37// typedef MessageHandler<messagetype> Superclass; \
38// typedef SmartPointer<Self> Pointer; \
39// typedef SmartPointer<const Self> ConstPointer; \
40// igtlTypeMacro(classname, MessageHandler<messagetype>); \
41// igtlNewMacro(classname); \
42// public: \
43// virtual void Process(messagetype*); \
44// };
45
46#define igtlMessageHandlerClassMacro(messagetype, classname, datatype) \
47 class classname : public ::igtl::MessageHandler \
48 { \
49 public: \
50 typedef classname Self; \
51 typedef ::igtl::MessageHandler Superclass; \
52 typedef igtl::SmartPointer<Self> Pointer; \
53 typedef igtl::SmartPointer<const Self> ConstPointer; \
54 igtlTypeMacro(classname, ::igtl::MessageHandler); \
55 igtlNewMacro(classname); \
56 public: \
57 virtual const char* GetMessageType() \
58 { \
59 return this->m_Message->GetDeviceType(); \
60 } \
61 virtual int Process(messagetype*, datatype*); \
62 int ReceiveMessage(::igtl::Socket* socket, ::igtl::MessageBase* header, int pos) \
63 { \
64 if (pos == 0) /* New body */ \
65 { \
66 this->m_Message->SetMessageHeader(header); \
67 this->m_Message->AllocatePack(); \
68 } \
69 int s = socket->Receive((void*)((char*)this->m_Message->GetPackBodyPointer()+pos), \
70 this->m_Message->GetPackBodySize()-pos); \
71 if (s < 0) /* Time out */ \
72 { \
73 return pos; \
74 } \
75 if (s+pos >= this->m_Message->GetPackBodySize()) \
76 { \
77 int r = this->m_Message->Unpack(this->m_CheckCRC); \
78 if (r) \
79 { \
80 Process(this->m_Message, this->m_Data); \
81 } \
82 else \
83 { \
84 return -1; \
85 } \
86 } \
87 return s + pos; /* return current position in the body */ \
88 } \
89 virtual void CheckCRC(int i) \
90 { \
91 if (i == 0) \
92 { \
93 this->m_CheckCRC = 0; \
94 } \
95 else \
96 { \
97 this->m_CheckCRC = 1; \
98 } \
99 } \
100 void SetData(datatype* p) \
101 { \
102 this->m_Data = p; \
103 } \
104 datatype* GetData() \
105 { \
106 return this->m_Data; \
107 } \
108 protected: \
109 classname() \
110 { \
111 this->m_Message = messagetype::New(); \
112 this->m_CheckCRC = 1; \
113 this->m_Data = NULL; \
114 } \
115 ~classname() {} \
116 protected: \
117 int m_CheckCRC; \
118 messagetype::Pointer m_Message; \
119 datatype* m_Data; \
120 };
121
122#endif // __igtlMessageHandlerMacro_h

Generated for OpenIGTLink by Doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2012