comparison Framework/Common/Dictionary.cpp @ 0:7cea966b6829

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Jul 2018 08:16:29 +0200
parents
children 9774802fd05f
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 "Dictionary.h"
23
24 #include "BinaryStringValue.h"
25 #include "Integer64Value.h"
26 #include "NullValue.h"
27 #include "Utf8StringValue.h"
28
29 #include <Core/Logging.h>
30 #include <Core/OrthancException.h>
31
32 #include <cassert>
33
34 namespace OrthancDatabases
35 {
36 Dictionary::~Dictionary()
37 {
38 for (Values::iterator it = values_.begin();
39 it != values_.end(); ++it)
40 {
41 assert(it->second != NULL);
42 delete it->second;
43 }
44 }
45
46
47 bool Dictionary::HasKey(const std::string& key) const
48 {
49 return values_.find(key) != values_.end();
50 }
51
52
53 void Dictionary::Remove(const std::string& key)
54 {
55 Values::iterator found = values_.find(key);
56
57 if (found != values_.end())
58 {
59 assert(found->second != NULL);
60 delete found->second;
61 values_.erase(found);
62 }
63 }
64
65
66 void Dictionary::SetValue(const std::string& key,
67 IValue* value) // Takes ownership
68 {
69 if (value == NULL)
70 {
71 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
72 }
73
74 Values::iterator found = values_.find(key);
75
76 if (found == values_.end())
77 {
78 values_[key] = value;
79 }
80 else
81 {
82 assert(found->second != NULL);
83 delete found->second;
84 found->second = value;
85 }
86 }
87
88
89 void Dictionary::SetUtf8Value(const std::string& key,
90 const std::string& utf8)
91 {
92 SetValue(key, new Utf8StringValue(utf8));
93 }
94
95
96 void Dictionary::SetBinaryValue(const std::string& key,
97 const std::string& binary)
98 {
99 SetValue(key, new BinaryStringValue(binary));
100 }
101
102
103 void Dictionary::SetIntegerValue(const std::string& key,
104 int64_t value)
105 {
106 SetValue(key, new Integer64Value(value));
107 }
108
109
110 void Dictionary::SetNullValue(const std::string& key)
111 {
112 SetValue(key, new NullValue);
113 }
114
115
116 const IValue& Dictionary::GetValue(const std::string& key) const
117 {
118 Values::const_iterator found = values_.find(key);
119
120 if (found == values_.end())
121 {
122 LOG(ERROR) << "Inexistent value in a dictionary: " << key;
123 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem);
124 }
125 else
126 {
127 assert(found->second != NULL);
128 return *found->second;
129 }
130 }
131 }