Mercurial > hg > orthanc-databases
annotate Framework/Common/Dictionary.cpp @ 349:2014b59d2ef3 OrthancPostgreSQL-3.2
closing OrthancPostgreSQL-3.2
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 12 Aug 2021 16:09:40 +0200 |
parents | 714c5d2bee76 |
children | 4cd7e45b671e |
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 | |
67 | 5 * Copyright (C) 2017-2019 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 "Dictionary.h" | |
23 | |
24 #include "BinaryStringValue.h" | |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
25 #include "FileValue.h" |
0 | 26 #include "Integer64Value.h" |
27 #include "NullValue.h" | |
28 #include "Utf8StringValue.h" | |
29 | |
30 #include <Core/Logging.h> | |
31 #include <Core/OrthancException.h> | |
32 | |
33 #include <cassert> | |
34 | |
35 namespace OrthancDatabases | |
36 { | |
37 Dictionary::~Dictionary() | |
38 { | |
39 for (Values::iterator it = values_.begin(); | |
40 it != values_.end(); ++it) | |
41 { | |
42 assert(it->second != NULL); | |
43 delete it->second; | |
44 } | |
45 } | |
46 | |
47 | |
48 bool Dictionary::HasKey(const std::string& key) const | |
49 { | |
50 return values_.find(key) != values_.end(); | |
51 } | |
52 | |
53 | |
54 void Dictionary::Remove(const std::string& key) | |
55 { | |
56 Values::iterator found = values_.find(key); | |
57 | |
58 if (found != values_.end()) | |
59 { | |
60 assert(found->second != NULL); | |
61 delete found->second; | |
62 values_.erase(found); | |
63 } | |
64 } | |
65 | |
66 | |
67 void Dictionary::SetValue(const std::string& key, | |
68 IValue* value) // Takes ownership | |
69 { | |
70 if (value == NULL) | |
71 { | |
72 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
73 } | |
74 | |
75 Values::iterator found = values_.find(key); | |
76 | |
77 if (found == values_.end()) | |
78 { | |
79 values_[key] = value; | |
80 } | |
81 else | |
82 { | |
83 assert(found->second != NULL); | |
84 delete found->second; | |
85 found->second = value; | |
86 } | |
87 } | |
88 | |
89 | |
90 void Dictionary::SetUtf8Value(const std::string& key, | |
91 const std::string& utf8) | |
92 { | |
93 SetValue(key, new Utf8StringValue(utf8)); | |
94 } | |
95 | |
96 | |
97 void Dictionary::SetBinaryValue(const std::string& key, | |
98 const std::string& binary) | |
99 { | |
100 SetValue(key, new BinaryStringValue(binary)); | |
101 } | |
102 | |
103 | |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
104 void Dictionary::SetFileValue(const std::string& key, |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
105 const std::string& file) |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
106 { |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
107 SetValue(key, new FileValue(file)); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
108 } |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
109 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
110 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
111 void Dictionary::SetFileValue(const std::string& key, |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
112 const void* content, |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
113 size_t size) |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
114 { |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
115 SetValue(key, new FileValue(content, size)); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
116 } |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
117 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
118 |
0 | 119 void Dictionary::SetIntegerValue(const std::string& key, |
120 int64_t value) | |
121 { | |
122 SetValue(key, new Integer64Value(value)); | |
123 } | |
124 | |
125 | |
126 void Dictionary::SetNullValue(const std::string& key) | |
127 { | |
128 SetValue(key, new NullValue); | |
129 } | |
130 | |
131 | |
132 const IValue& Dictionary::GetValue(const std::string& key) const | |
133 { | |
134 Values::const_iterator found = values_.find(key); | |
135 | |
136 if (found == values_.end()) | |
137 { | |
138 LOG(ERROR) << "Inexistent value in a dictionary: " << key; | |
139 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem); | |
140 } | |
141 else | |
142 { | |
143 assert(found->second != NULL); | |
144 return *found->second; | |
145 } | |
146 } | |
147 } |