Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
usServicePropertiesImpl.cpp
Go to the documentation of this file.
1 /*=============================================================================
2 
3  Library: CppMicroServices
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 =============================================================================*/
21 
22 #include "usServicePropertiesImpl_p.h"
23 
24 #include <limits>
25 #include <stdexcept>
26 #ifdef US_PLATFORM_WINDOWS
27 #include <string.h>
28 #define ci_compare strnicmp
29 #else
30 #include <strings.h>
31 #define ci_compare strncasecmp
32 #endif
33 
34 US_BEGIN_NAMESPACE
35 
36 Any ServicePropertiesImpl::emptyAny;
37 
38 ServicePropertiesImpl::ServicePropertiesImpl(const ServiceProperties& p)
39 {
40  if (p.size() > static_cast<std::size_t>(std::numeric_limits<int>::max()))
41  {
42  throw std::runtime_error("ServiceProperties object contains too many keys");
43  }
44 
45  keys.reserve(p.size());
46  values.reserve(p.size());
47 
48  for (ServiceProperties::const_iterator iter = p.begin();
49  iter != p.end(); ++iter)
50  {
51  if (Find(iter->first) > -1)
52  {
53  std::string msg = "ServiceProperties object contains case variants of the key: ";
54  msg += iter->first;
55  throw std::runtime_error(msg.c_str());
56  }
57  keys.push_back(iter->first);
58  values.push_back(iter->second);
59  }
60 }
61 
62 const Any& ServicePropertiesImpl::Value(const std::string& key) const
63 {
64  int i = Find(key);
65  if (i < 0) return emptyAny;
66  return values[i];
67 }
68 
69 const Any& ServicePropertiesImpl::Value(int index) const
70 {
71  if (index < 0 || static_cast<std::size_t>(index) >= values.size())
72  return emptyAny;
73  return values[static_cast<std::size_t>(index)];
74 }
75 
76 int ServicePropertiesImpl::Find(const std::string& key) const
77 {
78  for (std::size_t i = 0; i < keys.size(); ++i)
79  {
80  if (ci_compare(key.c_str(), keys[i].c_str(), key.size()) == 0)
81  {
82  return static_cast<int>(i);
83  }
84  }
85  return -1;
86 }
87 
88 int ServicePropertiesImpl::FindCaseSensitive(const std::string& key) const
89 {
90  for (std::size_t i = 0; i < keys.size(); ++i)
91  {
92  if (key == keys[i])
93  {
94  return static_cast<int>(i);
95  }
96  }
97  return -1;
98 }
99 
100 const std::vector<std::string>& ServicePropertiesImpl::Keys() const
101 {
102  return keys;
103 }
104 
105 US_END_NAMESPACE
#define ci_compare
static T max(T x, T y)
Definition: svm.cpp:70
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties