Mercurial > hg > orthanc-stone
comparison Framework/Messages/IMessage.h @ 643:f0008c55e5f7
getting rid of MessageType enumeration
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 10 May 2019 17:34:48 +0200 |
parents | 9cd19b28f011 |
children | e713f1a99861 2d8ab34c8c91 |
comparison
equal
deleted
inserted
replaced
642:7ca8dc7ec17b | 643:f0008c55e5f7 |
---|---|
19 **/ | 19 **/ |
20 | 20 |
21 | 21 |
22 #pragma once | 22 #pragma once |
23 | 23 |
24 #include "../StoneEnumerations.h" | 24 #include <boost/noncopyable.hpp> |
25 | 25 |
26 #include <boost/noncopyable.hpp> | 26 #include <string.h> |
27 | 27 |
28 namespace OrthancStone | 28 namespace OrthancStone |
29 { | 29 { |
30 // base message that are exchanged between IObservable and IObserver | 30 class MessageIdentifier |
31 { | |
32 private: | |
33 const char* file_; | |
34 int line_; | |
35 | |
36 public: | |
37 MessageIdentifier(const char* file, | |
38 int line) : | |
39 file_(file), | |
40 line_(line) | |
41 { | |
42 } | |
43 | |
44 MessageIdentifier() : | |
45 file_(NULL), | |
46 line_(0) | |
47 { | |
48 } | |
49 | |
50 bool operator< (const MessageIdentifier& other) const | |
51 { | |
52 if (file_ == NULL) | |
53 { | |
54 return false; | |
55 } | |
56 else if (line_ != other.line_) | |
57 { | |
58 return line_ < other.line_; | |
59 } | |
60 else | |
61 { | |
62 return strcmp(file_, other.file_) < 0; | |
63 } | |
64 } | |
65 }; | |
66 | |
67 | |
68 /** | |
69 * Base messages that are exchanged between IObservable and | |
70 * IObserver. Messages are distinguished by the "__FILE__" and | |
71 * "__LINE__" macro, as in "Orthanc::SQLite::StatementId". | |
72 **/ | |
31 class IMessage : public boost::noncopyable | 73 class IMessage : public boost::noncopyable |
32 { | 74 { |
33 private: | |
34 MessageType messageType_; | |
35 | |
36 protected: | |
37 IMessage(MessageType messageType) : | |
38 messageType_(messageType) | |
39 { | |
40 } | |
41 | |
42 public: | 75 public: |
43 virtual ~IMessage() | 76 virtual ~IMessage() |
44 { | 77 { |
45 } | 78 } |
46 | 79 |
47 virtual MessageType GetType() const | 80 virtual const MessageIdentifier& GetIdentifier() const = 0; |
48 { | |
49 return messageType_; | |
50 } | |
51 }; | 81 }; |
52 | 82 |
53 | 83 |
54 // base class to derive from to implement your own messages | 84 /** |
55 // it handles the message type for you | 85 * Simple message implementation when no payload is needed but the |
56 template <MessageType type> | 86 * origin is required. Sample usage: |
57 class BaseMessage : public IMessage | 87 * typedef OriginMessage<OrthancSlicesLoader> SliceGeometryErrorMessage; |
58 { | 88 **/ |
59 public: | 89 template <typename TOrigin> |
60 enum | 90 class OriginMessage : public IMessage |
61 { | |
62 Type = type | |
63 }; | |
64 | |
65 BaseMessage() : | |
66 IMessage(static_cast<MessageType>(Type)) | |
67 { | |
68 } | |
69 }; | |
70 | |
71 | |
72 // simple message implementation when no payload is needed | |
73 // sample usage: | |
74 // typedef NoPayloadMessage<MessageType_VolumeSlicer_GeometryReady> GeometryReadyMessage; | |
75 template <MessageType type> | |
76 class NoPayloadMessage : public BaseMessage<type> | |
77 { | |
78 public: | |
79 NoPayloadMessage() : | |
80 BaseMessage<type>() | |
81 { | |
82 } | |
83 }; | |
84 | |
85 // simple message implementation when no payload is needed but the origin is required | |
86 // sample usage: | |
87 // typedef OriginMessage<MessageType_SliceLoader_GeometryError, OrthancSlicesLoader> SliceGeometryErrorMessage; | |
88 template <MessageType type, typename TOrigin> | |
89 class OriginMessage : public BaseMessage<type> | |
90 { | 91 { |
91 private: | 92 private: |
92 const TOrigin& origin_; | 93 const TOrigin& origin_; |
93 | 94 |
94 public: | 95 public: |
95 OriginMessage(const TOrigin& origin) : | 96 OriginMessage(const TOrigin& origin) : |
96 BaseMessage<type>(), | |
97 origin_(origin) | 97 origin_(origin) |
98 { | 98 { |
99 } | 99 } |
100 | 100 |
101 const TOrigin& GetOrigin() const | 101 const TOrigin& GetOrigin() const |
102 { | 102 { |
103 return origin_; | 103 return origin_; |
104 } | 104 } |
105 }; | 105 }; |
106 } | 106 } |
107 | |
108 | |
109 #define ORTHANC_STONE_MESSAGE(FILE, LINE) \ | |
110 public: \ | |
111 static const ::OrthancStone::MessageIdentifier& GetStaticIdentifier() \ | |
112 { \ | |
113 static const ::OrthancStone::MessageIdentifier id(FILE, LINE); \ | |
114 return id; \ | |
115 } \ | |
116 \ | |
117 virtual const ::OrthancStone::MessageIdentifier& GetIdentifier() const \ | |
118 { \ | |
119 return GetStaticIdentifier(); \ | |
120 } | |
121 | |
122 | |
123 #define ORTHANC_STONE_DEFINE_ORIGIN_MESSAGE(FILE, LINE, NAME, ORIGIN) \ | |
124 class NAME : public ::OrthancStone::OriginMessage<ORIGIN> \ | |
125 { \ | |
126 ORTHANC_STONE_MESSAGE(FILE, LINE); \ | |
127 \ | |
128 NAME(const ORIGIN& origin) : \ | |
129 OriginMessage(origin) \ | |
130 { \ | |
131 } \ | |
132 }; | |
133 | |
134 | |
135 #define ORTHANC_STONE_DEFINE_EMPTY_MESSAGE(FILE, LINE, NAME) \ | |
136 class NAME : public ::OrthancStone::IMessage \ | |
137 { \ | |
138 ORTHANC_STONE_MESSAGE(FILE, LINE); \ | |
139 }; |