libopenraw
metavalue.cpp
1 /*
2  * libopenraw - metavalue.cpp
3  *
4  * Copyright (C) 2007 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 "trace.h"
24 #include "exception.h"
25 #include "metavalue.h"
26 
27 using namespace Debug;
28 
29 namespace OpenRaw {
30 
31 MetaValue::MetaValue(const MetaValue & r)
32  : m_value(r.m_value)
33 {
34 }
35 
36 MetaValue::MetaValue(const value_t &v)
37  : m_value(v)
38 {
39 }
40 
41 namespace {
42 
43 template <class T>
44 MetaValue::value_t convert(const Internals::IFDEntry::Ref & e)
45 {
46  T v;
47  v = Internals::IFDTypeTrait<T>::get(*e, 0, false);
48  return MetaValue::value_t(v);
49 }
50 
51 }
52 
53 MetaValue::MetaValue(const Internals::IFDEntry::Ref & e)
54 {
55  switch(e->type()) {
56  case Internals::IFD::EXIF_FORMAT_BYTE:
57  {
58  m_value = convert<uint8_t>(e);
59  break;
60  }
61  case Internals::IFD::EXIF_FORMAT_ASCII:
62  {
63  m_value = convert<std::string>(e);
64  break;
65  }
66  case Internals::IFD::EXIF_FORMAT_SHORT:
67  {
68  m_value = convert<uint16_t>(e);
69  break;
70  }
71  case Internals::IFD::EXIF_FORMAT_LONG:
72  {
73  m_value = convert<uint32_t>(e);
74  break;
75  }
76  default:
77  Trace(DEBUG1) << "unhandled type " << e->type() << "\n";
78  break;
79  }
80 }
81 
82 template<typename T>
83 inline T MetaValue::get() const
84  throw(Internals::BadTypeException)
85 {
86  T v;
87  assert(!m_value.empty());
88  try {
89  v = boost::get<T>(m_value);
90  }
91  catch(...) { //const boost::bad_any_cast &) {
92  throw Internals::BadTypeException();
93  }
94  return v;
95 }
96 
97 
98 uint32_t MetaValue::getInteger() const
99  throw(Internals::BadTypeException)
100 {
101  return get<uint32_t>();
102 }
103 
104 std::string MetaValue::getString() const
105  throw(Internals::BadTypeException)
106 {
107  return get<std::string>();
108 }
109 
110 }
111 /*
112  Local Variables:
113  mode:c++
114  c-file-style:"stroustrup"
115  c-file-offsets:((innamespace . 0))
116  indent-tabs-mode:nil
117  fill-column:80
118  End:
119 */
static T get(IFDEntry &e, uint32_t idx=0, bool ignore_type=false)
Definition: ifdentry.h:211
boost::shared_ptr< IFDEntry > Ref
Definition: ifdentry.h:122