1 //
2 // CDDL HEADER START
3 //
4 // The contents of this file are subject to the terms of the Common Development
5 // and Distribution License Version 1.0 (the "License").
6 //
7 // You can obtain a copy of the license at
8 // http://www.opensource.org/licenses/CDDL-1.0.  See the License for the
9 // specific language governing permissions and limitations under the License.
10 //
11 // When distributing Covered Code, include this CDDL HEADER in each file and
12 // include the License file in a prominent location with the name LICENSE.CDDL.
13 // If applicable, add the following below this CDDL HEADER, with the fields
14 // enclosed by brackets "[]" replaced with your own identifying information:
15 //
16 // Portions Copyright (c) [yyyy] [name of copyright owner]. All rights reserved.
17 //
18 // CDDL HEADER END
19 //
20 
21 //
22 // Copyright (c) 2016--2020, Regents of the University of Minnesota.
23 // All rights reserved.
24 //
25 // Contributors:
26 //    Ryan S. Elliott
27 //
28 
29 //
30 // Release: This file is part of the kim-api-2.2.1 package.
31 //
32 
33 #include <map>
34 
35 #ifndef KIM_TEMPERATURE_UNIT_HPP_
36 #include "KIM_TemperatureUnit.hpp"
37 #endif
38 
39 namespace KIM
40 {
41 // Order doesn't matter as long as all values are unique
42 namespace TEMPERATURE_UNIT
43 {
44 #include "KIM_TemperatureUnit.inc"
45 TemperatureUnit const unused(ID_unused);
46 TemperatureUnit const K(ID_K);
47 
48 namespace
49 {
50 typedef std::
51     map<TemperatureUnit const, std::string, TEMPERATURE_UNIT::Comparator>
52         StringMap;
53 
GetStringMap()54 StringMap const GetStringMap()
55 {
56   StringMap m;
57   m[unused] = "unused";
58   m[K] = "K";
59   return m;
60 }
61 
62 StringMap const temperatureUnitToString = GetStringMap();
63 std::string const temperatureUnitUnknown("unknown");
64 }  // namespace
65 
66 
GetNumberOfTemperatureUnits(int * const numberOfTemperatureUnits)67 void GetNumberOfTemperatureUnits(int * const numberOfTemperatureUnits)
68 {
69   *numberOfTemperatureUnits = temperatureUnitToString.size();
70 }
71 
GetTemperatureUnit(int const index,TemperatureUnit * const temperatureUnit)72 int GetTemperatureUnit(int const index, TemperatureUnit * const temperatureUnit)
73 {
74   int numberOfTemperatureUnits;
75   GetNumberOfTemperatureUnits(&numberOfTemperatureUnits);
76   if ((index < 0) || (index >= numberOfTemperatureUnits)) return true;
77 
78   StringMap::const_iterator iter = temperatureUnitToString.begin();
79   for (int i = 0; i < index; ++i) ++iter;
80   *temperatureUnit = iter->first;
81   return false;  // no error
82 }
83 }  // namespace TEMPERATURE_UNIT
84 
85 // implementation of TemperatureUnit
TemperatureUnit()86 TemperatureUnit::TemperatureUnit() {}
TemperatureUnit(int const id)87 TemperatureUnit::TemperatureUnit(int const id) : temperatureUnitID(id) {}
TemperatureUnit(std::string const & str)88 TemperatureUnit::TemperatureUnit(std::string const & str)
89 {
90   temperatureUnitID = -1;
91   for (TEMPERATURE_UNIT::StringMap::const_iterator iter
92        = TEMPERATURE_UNIT::temperatureUnitToString.begin();
93        iter != TEMPERATURE_UNIT::temperatureUnitToString.end();
94        ++iter)
95   {
96     if (iter->second == str)
97     {
98       temperatureUnitID = (iter->first).temperatureUnitID;
99       break;
100     }
101   }
102 }
103 
Known() const104 bool TemperatureUnit::Known() const
105 {
106   int numberOfTemperatureUnits;
107   TEMPERATURE_UNIT::GetNumberOfTemperatureUnits(&numberOfTemperatureUnits);
108 
109   for (int i = 0; i < numberOfTemperatureUnits; ++i)
110   {
111     TemperatureUnit tempUnit;
112     TEMPERATURE_UNIT::GetTemperatureUnit(i, &tempUnit);
113 
114     if (*this == tempUnit) { return true; }
115   }
116 
117   return false;
118 }
119 
operator ==(TemperatureUnit const & rhs) const120 bool TemperatureUnit::operator==(TemperatureUnit const & rhs) const
121 {
122   return temperatureUnitID == rhs.temperatureUnitID;
123 }
operator !=(TemperatureUnit const & rhs) const124 bool TemperatureUnit::operator!=(TemperatureUnit const & rhs) const
125 {
126   return temperatureUnitID != rhs.temperatureUnitID;
127 }
128 
ToString() const129 std::string const & TemperatureUnit::ToString() const
130 {
131   TEMPERATURE_UNIT::StringMap::const_iterator iter
132       = TEMPERATURE_UNIT::temperatureUnitToString.find(*this);
133   if (iter == TEMPERATURE_UNIT::temperatureUnitToString.end())
134     return TEMPERATURE_UNIT::temperatureUnitUnknown;
135   else
136     return iter->second;
137 }
138 }  // namespace KIM
139