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