Mercurial > hg > orthanc-databases
annotate Framework/Common/ResultBase.cpp @ 395:a7a029043670 db-protobuf
integration mainline->db-protobuf
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 05 Apr 2023 14:53:57 +0200 |
parents | 3d6886f3e5b3 |
children | 326f8304daa1 ecd0b719cff5 |
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 | |
389
3d6886f3e5b3
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
359
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
3d6886f3e5b3
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
359
diff
changeset
|
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Affero General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Affero General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Affero General Public License | |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
23 #include "ResultBase.h" | |
24 | |
25 #include "../Common/BinaryStringValue.h" | |
26 #include "../Common/Integer64Value.h" | |
27 #include "../Common/NullValue.h" | |
28 #include "../Common/Utf8StringValue.h" | |
29 | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
30 #include <Compatibility.h> // For std::unique_ptr<> |
152 | 31 #include <Logging.h> |
32 #include <OrthancException.h> | |
0 | 33 |
34 #include <cassert> | |
35 #include <memory> | |
36 | |
37 namespace OrthancDatabases | |
38 { | |
39 void ResultBase::ClearFields() | |
40 { | |
41 for (size_t i = 0; i < fields_.size(); i++) | |
42 { | |
43 if (fields_[i] != NULL) | |
44 { | |
45 delete fields_[i]; | |
46 fields_[i] = NULL; | |
47 } | |
48 } | |
49 } | |
50 | |
51 | |
52 void ResultBase::ConvertFields() | |
53 { | |
54 assert(expectedType_.size() == fields_.size() && | |
55 hasExpectedType_.size() == fields_.size()); | |
56 | |
57 for (size_t i = 0; i < fields_.size(); i++) | |
58 { | |
59 if (fields_[i] == NULL) | |
60 { | |
61 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
62 } | |
63 | |
64 ValueType sourceType = fields_[i]->GetType(); | |
65 ValueType targetType = expectedType_[i]; | |
66 | |
67 if (hasExpectedType_[i] && | |
68 sourceType != ValueType_Null && | |
69 sourceType != targetType) | |
70 { | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
71 std::unique_ptr<IValue> converted(fields_[i]->Convert(targetType)); |
0 | 72 |
73 if (converted.get() == NULL) | |
74 { | |
75 LOG(ERROR) << "Cannot convert between data types from a database"; | |
76 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType); | |
77 } | |
78 else | |
79 { | |
80 assert(fields_[i] != NULL); | |
81 delete fields_[i]; | |
82 fields_[i] = converted.release(); | |
83 } | |
84 } | |
85 } | |
86 } | |
87 | |
88 | |
89 void ResultBase::FetchFields() | |
90 { | |
91 ClearFields(); | |
92 | |
93 if (!IsDone()) | |
94 { | |
95 for (size_t i = 0; i < fields_.size(); i++) | |
96 { | |
97 fields_[i] = FetchField(i); | |
98 | |
99 if (fields_[i] == NULL) | |
100 { | |
101 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
102 } | |
103 } | |
104 | |
105 ConvertFields(); | |
106 } | |
107 } | |
108 | |
109 | |
110 void ResultBase::SetFieldsCount(size_t count) | |
111 { | |
112 if (!fields_.empty()) | |
113 { | |
114 // This method can only be invoked once | |
115 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
116 } | |
117 | |
118 fields_.resize(count); | |
119 expectedType_.resize(count, ValueType_Null); | |
120 hasExpectedType_.resize(count, false); | |
121 } | |
122 | |
123 | |
124 void ResultBase::SetExpectedType(size_t field, | |
125 ValueType type) | |
126 { | |
127 assert(expectedType_.size() == fields_.size() && | |
128 hasExpectedType_.size() == fields_.size()); | |
129 | |
130 if (field < fields_.size()) | |
131 { | |
132 expectedType_[field] = type; | |
133 hasExpectedType_[field] = true; | |
134 | |
135 if (!IsDone()) | |
136 { | |
137 ConvertFields(); | |
138 } | |
139 } | |
140 } | |
141 | |
142 | |
143 const IValue& ResultBase::GetField(size_t index) const | |
144 { | |
145 if (IsDone()) | |
146 { | |
147 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
148 } | |
149 else if (index >= fields_.size()) | |
150 { | |
151 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
152 } | |
153 else if (fields_[index] == NULL) | |
154 { | |
155 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
156 } | |
157 else | |
158 { | |
159 return *fields_[index]; | |
160 } | |
161 } | |
162 } |