libopenraw  0.3.7
metavalue.cpp
1 /*
2  * libopenraw - metavalue.cpp
3  *
4  * Copyright (C) 2007-2020 Hubert Figuiere
5  * Copyright (C) 2008 Novell, Inc.
6  *
7  * This library is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser 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 library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #include <assert.h>
24 
25 #include "metavalue.hpp"
26 #include "exception.hpp"
27 
28 namespace OpenRaw {
29 
30 using Internals::IFD::ORRational;
31 using Internals::IFD::ORSRational;
32 
33 MetaValue::MetaValue(const MetaValue & r)
34  : m_values(r.m_values)
35 {
36 }
37 
38 MetaValue::MetaValue(const value_t &v)
39 {
40  m_values.push_back(v);
41 }
42 
43 MetaValue::MetaValue(const std::vector<value_t> &v)
44  : m_values(v)
45 {
46 
47 }
48 
49 template<typename T>
50 inline T MetaValue::get(int idx) const noexcept(false)
51 {
52  assert(!m_values.empty());
53  try {
54  return boost::get<T>(m_values[idx]);
55  }
56  catch(...) { //const boost::bad_any_cast &) {
57  throw Internals::BadTypeException();
58  }
59 }
60 
61 template<typename T>
62 inline const T & MetaValue::getRef(int idx) const noexcept(false)
63 {
64  static const T v;
65  assert(!m_values.empty());
66  try {
67  return boost::get<T>(m_values[idx]);
68  }
69  catch(...) { //const boost::bad_any_cast &) {
70  throw Internals::BadTypeException();
71  }
72  return v;
73 }
74 
75 uint32_t MetaValue::getUInteger(int idx) const
76 {
77  return get<uint32_t>(idx);
78 }
79 
80 int32_t MetaValue::getSInteger(int idx) const
81 {
82  return get<int32_t>(idx);
83 }
84 
85 const std::string & MetaValue::getString(int idx) const
86 {
87  return getRef<std::string>(idx);
88 }
89 
90 double MetaValue::getDouble(int idx) const
91 {
92  int type = m_values[idx].which();
93  switch (type) {
94  case 5:
95  return to_double(get<OpenRaw::Internals::IFD::ORRational>(idx));
96  case 6:
97  return to_double(get<OpenRaw::Internals::IFD::ORSRational>(idx));
98  default:
99  break;
100  }
101  return get<double>(idx);
102 }
103 
106  : public boost::static_visitor<std::string>
107 {
108 public:
109  std::string operator()(const std::string& s) {
110  return s;
111  }
112  std::string operator()(const uint8_t v) {
113  return std::to_string(v);
114  }
115  std::string operator()(uint32_t v) {
116  return std::to_string(v);
117  }
118  std::string operator()(int32_t v) {
119  return std::to_string(v);
120  }
121  std::string operator()(double v) {
122  return std::to_string(v);
123  }
124  std::string operator()(const ORRational& r) {
125  return std::to_string(r.num) + "/" + std::to_string(r.denom);
126  }
127  std::string operator()(const ORSRational& r) {
128  return std::to_string(r.num) + "/" + std::to_string(r.denom);
129  }
130 };
131 
132 const std::string & MetaValue::getAsString(bool full) const
133 {
134  uint32_t count = 0;
135  if (m_as_str.empty()) {
136  auto visitor = to_string_visitor();
137  bool multiple = (getCount() > 1);
138  if (multiple) {
139  m_as_str += "[ ";
140  }
141  for (auto value : m_values) {
142  m_as_str += value.apply_visitor(visitor);
143  if (multiple) {
144  m_as_str += ", ";
145  }
146  count++;
147  if (!full && count > 20) {
148  m_as_str += "...";
149  break;
150  }
151  }
152  if (multiple) {
153  m_as_str += "]";
154  }
155  }
156  return m_as_str;
157 }
158 
159 }
160 /*
161  Local Variables:
162  mode:c++
163  c-file-style:"stroustrup"
164  c-file-offsets:((innamespace . 0))
165  indent-tabs-mode:nil
166  fill-column:80
167  End:
168 */
Formatter for getAsString()
Definition: metavalue.cpp:107
double to_double(const ORRational &r)
Convert an ORRational to a double.
Definition: ifd.hpp:69
Global namespace for libopenraw.
Definition: arwfile.cpp:29