Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Messages/IMessage.h @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Framework/Messages/IMessage.h@7ec8fea061b9 |
children | 85e117739eca |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #pragma once | |
23 | |
24 #include <boost/lexical_cast.hpp> | |
25 #include <boost/noncopyable.hpp> | |
26 | |
27 #include <string.h> | |
28 | |
29 namespace OrthancStone | |
30 { | |
31 class MessageIdentifier | |
32 { | |
33 private: | |
34 const char* file_; | |
35 int line_; | |
36 | |
37 bool IsEqual(const MessageIdentifier& other) const | |
38 { | |
39 return (line_ == other.line_ && | |
40 strcmp(file_, other.file_) == 0); | |
41 } | |
42 | |
43 public: | |
44 MessageIdentifier(const char* file, | |
45 int line) : | |
46 file_(file), | |
47 line_(line) | |
48 { | |
49 } | |
50 | |
51 MessageIdentifier() : | |
52 file_(NULL), | |
53 line_(0) | |
54 { | |
55 } | |
56 | |
57 std::string AsString() const | |
58 { | |
59 return std::string(file_) + ":" + boost::lexical_cast<std::string>(line_); | |
60 } | |
61 | |
62 bool operator< (const MessageIdentifier& other) const | |
63 { | |
64 if (file_ == NULL) | |
65 { | |
66 return false; | |
67 } | |
68 else if (line_ != other.line_) | |
69 { | |
70 return line_ < other.line_; | |
71 } | |
72 else | |
73 { | |
74 return strcmp(file_, other.file_) < 0; | |
75 } | |
76 } | |
77 | |
78 bool operator== (const MessageIdentifier& other) const | |
79 { | |
80 return IsEqual(other); | |
81 } | |
82 | |
83 bool operator!= (const MessageIdentifier& other) const | |
84 { | |
85 return !IsEqual(other); | |
86 } | |
87 }; | |
88 | |
89 | |
90 /** | |
91 * Base messages that are exchanged between IObservable and | |
92 * IObserver. Messages are distinguished by the "__FILE__" and | |
93 * "__LINE__" macro, as in "Orthanc::SQLite::StatementId". | |
94 **/ | |
95 class IMessage : public boost::noncopyable | |
96 { | |
97 public: | |
98 virtual ~IMessage() | |
99 { | |
100 } | |
101 | |
102 virtual const MessageIdentifier& GetIdentifier() const = 0; | |
103 }; | |
104 | |
105 | |
106 /** | |
107 * Simple message implementation when no payload is needed but the | |
108 * origin is required. Sample usage: | |
109 * typedef OriginMessage<OrthancSlicesLoader> SliceGeometryErrorMessage; | |
110 **/ | |
111 template <typename TOrigin> | |
112 class OriginMessage : public IMessage | |
113 { | |
114 private: | |
115 const TOrigin& origin_; | |
116 | |
117 public: | |
118 OriginMessage(const TOrigin& origin) : | |
119 origin_(origin) | |
120 { | |
121 } | |
122 | |
123 const TOrigin& GetOrigin() const | |
124 { | |
125 return origin_; | |
126 } | |
127 }; | |
128 } | |
129 | |
130 | |
131 #define ORTHANC_STONE_MESSAGE(FILE, LINE) \ | |
132 public: \ | |
133 static const ::OrthancStone::MessageIdentifier& GetStaticIdentifier() \ | |
134 { \ | |
135 static const ::OrthancStone::MessageIdentifier id(FILE, LINE); \ | |
136 return id; \ | |
137 } \ | |
138 \ | |
139 virtual const ::OrthancStone::MessageIdentifier& GetIdentifier() const \ | |
140 { \ | |
141 return GetStaticIdentifier(); \ | |
142 } | |
143 | |
144 | |
145 #define ORTHANC_STONE_DEFINE_ORIGIN_MESSAGE(FILE, LINE, NAME, ORIGIN) \ | |
146 class NAME : public ::OrthancStone::OriginMessage<ORIGIN> \ | |
147 { \ | |
148 ORTHANC_STONE_MESSAGE(FILE, LINE); \ | |
149 \ | |
150 NAME(const ORIGIN& origin) : \ | |
151 OriginMessage(origin) \ | |
152 { \ | |
153 } \ | |
154 }; | |
155 | |
156 | |
157 #define ORTHANC_STONE_DEFINE_EMPTY_MESSAGE(FILE, LINE, NAME) \ | |
158 class NAME : public ::OrthancStone::IMessage \ | |
159 { \ | |
160 ORTHANC_STONE_MESSAGE(FILE, LINE); \ | |
161 }; |