libopenraw
metavalue.cpp
00001 /*
00002  * libopenraw - metavalue.cpp
00003  *
00004  * Copyright (C) 2007 Hubert Figuiere
00005  * Copyright (C) 2008 Novell, Inc.
00006  *
00007  * This library is free software: you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation, either version 3 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library.  If not, see
00019  * <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 
00023 #include "trace.h"
00024 #include "exception.h"
00025 #include "metavalue.h"
00026 
00027 using namespace Debug;
00028 
00029 namespace OpenRaw {
00030 
00031 MetaValue::MetaValue(const MetaValue & r)
00032     : m_value(r.m_value)
00033 {
00034 }
00035 
00036 MetaValue::MetaValue(const value_t &v)
00037     : m_value(v)
00038 {
00039 }
00040 
00041 namespace {
00042 
00043 template <class T>
00044 MetaValue::value_t convert(const Internals::IFDEntry::Ref & e)
00045 {
00046     T v;
00047     v = Internals::IFDTypeTrait<T>::get(*e, 0, false);
00048     return MetaValue::value_t(v);
00049 }
00050 
00051 }
00052 
00053 MetaValue::MetaValue(const Internals::IFDEntry::Ref & e)
00054 {
00055     switch(e->type()) {
00056     case Internals::IFD::EXIF_FORMAT_BYTE:
00057     {
00058         m_value = convert<uint8_t>(e);
00059         break;
00060     }
00061     case Internals::IFD::EXIF_FORMAT_ASCII:
00062     {
00063         m_value = convert<std::string>(e);
00064         break;
00065     }
00066     case Internals::IFD::EXIF_FORMAT_SHORT:
00067     {
00068         m_value = convert<uint16_t>(e);
00069         break;
00070     }
00071     case Internals::IFD::EXIF_FORMAT_LONG:
00072     {
00073         m_value = convert<uint32_t>(e);
00074         break;
00075     }
00076     default:
00077         Trace(DEBUG1) << "unhandled type " << e->type() << "\n";
00078         break;
00079     }
00080 }
00081 
00082 template<typename T>
00083 inline  T MetaValue::get() const
00084     throw(Internals::BadTypeException)
00085 {
00086     T v;
00087     assert(!m_value.empty());
00088     try {
00089         v = boost::get<T>(m_value);
00090     }
00091     catch(...) { //const boost::bad_any_cast &) {
00092         throw Internals::BadTypeException();
00093     }
00094     return v;
00095 }
00096 
00097 
00098 uint32_t MetaValue::getInteger() const
00099     throw(Internals::BadTypeException)
00100 {
00101     return get<uint32_t>();
00102 }
00103 
00104 std::string MetaValue::getString() const
00105     throw(Internals::BadTypeException)
00106 {
00107     return get<std::string>();
00108 }
00109 
00110 }
00111 /*
00112   Local Variables:
00113   mode:c++
00114   c-file-style:"stroustrup"
00115   c-file-offsets:((innamespace . 0))
00116   indent-tabs-mode:nil
00117   fill-column:80
00118   End:
00119 */