Mercurial > hg > orthanc-databases
annotate Framework/Common/ResultBase.cpp @ 489:e8b4bb6a33e7
introduction of ORTHANC_SDK_COMPATIBLE_VERSIONS in CMake
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 22 Mar 2024 14:27:36 +0100 |
parents | f0976163dbe1 |
children | 54d518dcd74a |
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 | |
459
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium |
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
6 * Copyright (C) 2021-2024 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 ValueType sourceType = fields_[i]->GetType(); | |
60 ValueType targetType = expectedType_[i]; | |
61 | |
62 if (hasExpectedType_[i] && | |
63 sourceType != ValueType_Null && | |
64 sourceType != targetType) | |
65 { | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
66 std::unique_ptr<IValue> converted(fields_[i]->Convert(targetType)); |
0 | 67 |
68 if (converted.get() == NULL) | |
69 { | |
70 LOG(ERROR) << "Cannot convert between data types from a database"; | |
71 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType); | |
72 } | |
73 else | |
74 { | |
75 assert(fields_[i] != NULL); | |
76 delete fields_[i]; | |
77 fields_[i] = converted.release(); | |
78 } | |
79 } | |
80 } | |
81 } | |
82 | |
83 | |
84 void ResultBase::FetchFields() | |
85 { | |
86 ClearFields(); | |
87 | |
88 if (!IsDone()) | |
89 { | |
90 for (size_t i = 0; i < fields_.size(); i++) | |
91 { | |
92 fields_[i] = FetchField(i); | |
435
326f8304daa1
new creating temporary tables inside functions
Alain Mazy <am@osimis.io>
parents:
389
diff
changeset
|
93 } |
0 | 94 |
435
326f8304daa1
new creating temporary tables inside functions
Alain Mazy <am@osimis.io>
parents:
389
diff
changeset
|
95 if (fields_.size() == 1 && fields_[0] == NULL) // this is a "void" result |
326f8304daa1
new creating temporary tables inside functions
Alain Mazy <am@osimis.io>
parents:
389
diff
changeset
|
96 { |
326f8304daa1
new creating temporary tables inside functions
Alain Mazy <am@osimis.io>
parents:
389
diff
changeset
|
97 return; |
0 | 98 } |
99 | |
100 ConvertFields(); | |
101 } | |
102 } | |
103 | |
104 | |
105 void ResultBase::SetFieldsCount(size_t count) | |
106 { | |
107 if (!fields_.empty()) | |
108 { | |
109 // This method can only be invoked once | |
110 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
111 } | |
112 | |
113 fields_.resize(count); | |
114 expectedType_.resize(count, ValueType_Null); | |
115 hasExpectedType_.resize(count, false); | |
116 } | |
117 | |
118 | |
119 void ResultBase::SetExpectedType(size_t field, | |
120 ValueType type) | |
121 { | |
122 assert(expectedType_.size() == fields_.size() && | |
123 hasExpectedType_.size() == fields_.size()); | |
124 | |
125 if (field < fields_.size()) | |
126 { | |
127 expectedType_[field] = type; | |
128 hasExpectedType_[field] = true; | |
129 | |
130 if (!IsDone()) | |
131 { | |
132 ConvertFields(); | |
133 } | |
134 } | |
135 } | |
136 | |
137 | |
138 const IValue& ResultBase::GetField(size_t index) const | |
139 { | |
140 if (IsDone()) | |
141 { | |
142 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
143 } | |
144 else if (index >= fields_.size()) | |
145 { | |
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
147 } | |
148 else if (fields_[index] == NULL) | |
149 { | |
150 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
151 } | |
152 else | |
153 { | |
154 return *fields_[index]; | |
155 } | |
156 } | |
157 } |