Mercurial > hg > orthanc-databases
annotate Framework/Common/ResultBase.cpp @ 328:6a49c495c940
back to mainline
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 22 Jul 2021 20:20:26 +0200 |
parents | 3236894320d6 |
children | 16aac0287485 |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
157
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
0 | 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 #include "ResultBase.h" | |
23 | |
24 #include "../Common/BinaryStringValue.h" | |
25 #include "../Common/Integer64Value.h" | |
26 #include "../Common/NullValue.h" | |
27 #include "../Common/Utf8StringValue.h" | |
28 | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
29 #include <Compatibility.h> // For std::unique_ptr<> |
152 | 30 #include <Logging.h> |
31 #include <OrthancException.h> | |
0 | 32 |
33 #include <cassert> | |
34 #include <memory> | |
35 | |
36 namespace OrthancDatabases | |
37 { | |
38 void ResultBase::ClearFields() | |
39 { | |
40 for (size_t i = 0; i < fields_.size(); i++) | |
41 { | |
42 if (fields_[i] != NULL) | |
43 { | |
44 delete fields_[i]; | |
45 fields_[i] = NULL; | |
46 } | |
47 } | |
48 } | |
49 | |
50 | |
51 void ResultBase::ConvertFields() | |
52 { | |
53 assert(expectedType_.size() == fields_.size() && | |
54 hasExpectedType_.size() == fields_.size()); | |
55 | |
56 for (size_t i = 0; i < fields_.size(); i++) | |
57 { | |
58 if (fields_[i] == NULL) | |
59 { | |
60 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
61 } | |
62 | |
63 ValueType sourceType = fields_[i]->GetType(); | |
64 ValueType targetType = expectedType_[i]; | |
65 | |
66 if (hasExpectedType_[i] && | |
67 sourceType != ValueType_Null && | |
68 sourceType != targetType) | |
69 { | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
70 std::unique_ptr<IValue> converted(fields_[i]->Convert(targetType)); |
0 | 71 |
72 if (converted.get() == NULL) | |
73 { | |
74 LOG(ERROR) << "Cannot convert between data types from a database"; | |
75 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType); | |
76 } | |
77 else | |
78 { | |
79 assert(fields_[i] != NULL); | |
80 delete fields_[i]; | |
81 fields_[i] = converted.release(); | |
82 } | |
83 } | |
84 } | |
85 } | |
86 | |
87 | |
88 void ResultBase::FetchFields() | |
89 { | |
90 ClearFields(); | |
91 | |
92 if (!IsDone()) | |
93 { | |
94 for (size_t i = 0; i < fields_.size(); i++) | |
95 { | |
96 fields_[i] = FetchField(i); | |
97 | |
98 if (fields_[i] == NULL) | |
99 { | |
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
101 } | |
102 } | |
103 | |
104 ConvertFields(); | |
105 } | |
106 } | |
107 | |
108 | |
109 void ResultBase::SetFieldsCount(size_t count) | |
110 { | |
111 if (!fields_.empty()) | |
112 { | |
113 // This method can only be invoked once | |
114 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
115 } | |
116 | |
117 fields_.resize(count); | |
118 expectedType_.resize(count, ValueType_Null); | |
119 hasExpectedType_.resize(count, false); | |
120 } | |
121 | |
122 | |
123 void ResultBase::SetExpectedType(size_t field, | |
124 ValueType type) | |
125 { | |
126 assert(expectedType_.size() == fields_.size() && | |
127 hasExpectedType_.size() == fields_.size()); | |
128 | |
129 if (field < fields_.size()) | |
130 { | |
131 expectedType_[field] = type; | |
132 hasExpectedType_[field] = true; | |
133 | |
134 if (!IsDone()) | |
135 { | |
136 ConvertFields(); | |
137 } | |
138 } | |
139 } | |
140 | |
141 | |
142 const IValue& ResultBase::GetField(size_t index) const | |
143 { | |
144 if (IsDone()) | |
145 { | |
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
147 } | |
148 else if (index >= fields_.size()) | |
149 { | |
150 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
151 } | |
152 else if (fields_[index] == NULL) | |
153 { | |
154 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
155 } | |
156 else | |
157 { | |
158 return *fields_[index]; | |
159 } | |
160 } | |
161 } |