File: igtlMessageHandlerMacro.h

package info (click to toggle)
openigtlink 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 20,076; ansic: 6,704; sh: 227; perl: 74; makefile: 46
file content (194 lines) | stat: -rw-r--r-- 12,611 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*=========================================================================

  Program:   OpenIGTLink Library
  Module:    git@github.com:openigtlink/OpenIGTLink.git
  Language:  C++

  Copyright (c) Insight Software Consortium. All rights reserved.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#ifndef __igtlMessageHandlerMacro_h
#define __igtlMessageHandlerMacro_h

#include "igtlMessageHandler.h"

// Description:
// The igtlMessageHandlerClassMacro() macro is to help developers to
// define message handler class. It generates a child class of igtl::MessageHandler. 
// The developer only needs to implement ProcessMessage() after calling this macro.
// The following code shows how to define a handler that processes IMAGE message:
//
//   igtlMessageHandlerClassMacro(igtl::ImageMessage, TestImageMessageHandler);
//   void TestImageMessageHandler::Process(igtl::ImageMessage * message)
//   {
//     // do something
//   }

#if OpenIGTLink_PROTOCOL_VERSION >= 3
#define igtlMessageHandlerClassMacro(messagetype, classname, datatype)     \
  class classname : public ::igtl::MessageHandler                 \
  {                                                               \
  public:                                                         \
    typedef classname                      Self;                  \
    typedef ::igtl::MessageHandler    Superclass;                 \
    typedef igtl::SmartPointer<Self>             Pointer;         \
    typedef igtl::SmartPointer<const Self>       ConstPointer;    \
    igtlTypeMacro(classname, ::igtl::MessageHandler);             \
    igtlNewMacro(classname);                                      \
  public:                                                         \
    virtual const char* GetMessageType()                          \
    {                                                             \
      return this->m_Message->GetDeviceType();                    \
    }                                                             \
    virtual int Process(messagetype*, datatype*);                 \
    int ReceiveMessage(::igtl::Socket* socket, ::igtl::MessageBase* header, int pos) \
    {                                                                   \
      if (pos == 0) /* New body */                                      \
        {                                                               \
        this->m_Message->SetMessageHeader(header);                      \
        this->m_Message->InitBuffer();                                  \
        }                                                               \
      int s = socket->Receive((void*)((char*)this->m_Message->GetBufferBodyPointer()+pos), \
                              this->m_Message->GetBufferBodySize()-pos);  \
      if (s < 0) /* Time out */                                         \
        {                                                               \
        return pos;                                                     \
        }                                                               \
      if (s+pos >= this->m_Message->GetBufferBodySize())                \
        {                                                               \
        int r = this->m_Message->Unpack(this->m_CheckCRC);              \
        if (r)                                                          \
          {                                                             \
          Process(this->m_Message, this->m_Data);                       \
          }                                                             \
        else                                                            \
          {                                                             \
          return -1;                                                    \
          }                                                             \
        }                                                               \
      return s + pos;  /* return current position in the body */        \
    }                                                                   \
    virtual void CheckCRC(int i)                                  \
    {                                                             \
      if (i == 0)                                                 \
        {                                                         \
        this->m_CheckCRC = 0;                                     \
        }                                                         \
      else                                                        \
        {                                                         \
        this->m_CheckCRC = 1;                                     \
        }                                                         \
    }                                                             \
    void SetData(datatype* p)                                     \
    {                                                             \
      this->m_Data = p;                                           \
    }                                                             \
    datatype* GetData()                                           \
    {                                                             \
      return this->m_Data;                                        \
    }                                                             \
  protected:                                                      \
    classname()                                                   \
    {                                                             \
      this->m_Message  = messagetype::New();                      \
      this->m_CheckCRC = 1;                                       \
      this->m_Data = NULL;                                        \
    }                                                             \
    ~classname() {}                                               \
  protected:                                                      \
    int         m_CheckCRC;                                       \
    messagetype::Pointer m_Message;                               \
    datatype*   m_Data;                                           \
  }; 
#else
  #define igtlMessageHandlerClassMacro(messagetype, classname, datatype)  \
  class classname : public ::igtl::MessageHandler                         \
  {                                                                     \
  public:                                                               \
    typedef classname                      Self;                        \
    typedef ::igtl::MessageHandler    Superclass;                       \
    typedef igtl::SmartPointer<Self>             Pointer;               \
    typedef igtl::SmartPointer<const Self>       ConstPointer;          \
    igtlTypeMacro(classname, ::igtl::MessageHandler);                   \
    igtlNewMacro(classname);                                            \
  public:                                                               \
    virtual std::string GetMessageType() const                          \
    {                                                                   \
      return this->m_Message->GetMessageType();                         \
    }                                                                   \
    virtual const char* GetMessageType()                                \
    {                                                                   \
      return this->m_Message->GetType();                                \
    }                                                                   \
    virtual const char* GetMessageType()                                \
    {                                                                   \
      return this->m_Message->GetDeviceType();                          \
    }                                                                   \
    virtual int Process(messagetype*, datatype*);                       \
    int ReceiveMessage(::igtl::Socket* socket, ::igtl::MessageBase* header, int pos) \
    {                                                                   \
      if (pos == 0) /* New body */                                      \
        {                                                               \
        this->m_Message->SetMessageHeader(header);                      \
        this->m_Message->AllocateBuffer();                              \
        }                                                               \
      int s = socket->Receive((void*)((char*)this->m_Message->GetBufferBodyPointer()+pos), \
                              this->m_Message->GetBufferBodySize()-pos); \
      if (s < 0) /* Time out */                                         \
        {                                                               \
        return pos;                                                     \
        }                                                               \
      if (s+pos >= this->m_Message->GetBufferBodySize())                \
        {                                                               \
        int r = this->m_Message->Unpack(this->m_CheckCRC);              \
        if (r)                                                          \
          {                                                             \
          Process(this->m_Message, this->m_Data);                       \
          }                                                             \
        else                                                            \
          {                                                             \
          return -1;                                                    \
          }                                                             \
        }                                                               \
      return s + pos;  /* return current position in the body */        \
    }                                                                   \
    virtual void CheckCRC(int i)                                        \
    {                                                                   \
      if (i == 0)                                                       \
        {                                                               \
        this->m_CheckCRC = 0;                                           \
        }                                                               \
      else                                                              \
        {                                                               \
        this->m_CheckCRC = 1;                                           \
        }                                                               \
    }                                                                   \
    void SetData(datatype* p)                                           \
    {                                                                   \
      this->m_Data = p;                                                 \
    }                                                                   \
    datatype* GetData()                                                 \
    {                                                                   \
      return this->m_Data;                                              \
    }                                                                   \
  protected:                                                            \
    classname()                                                         \
      {                                                                 \
      this->m_Message  = messagetype::New();                            \
      this->m_CheckCRC = 1;                                             \
      this->m_Data = NULL;                                              \
      }                                                                 \
    ~classname() {}                                                     \
  protected:                                                            \
    int         m_CheckCRC;                                             \
    messagetype::Pointer m_Message;                                     \
    datatype*   m_Data;                                                 \
  };
#endif

#endif // __igtlMessageHandlerMacro_h