Added Value::toFloat, Value::toRational

v0.27.3
Andreas Huggel 22 years ago
parent 640430787a
commit 013f56e77d

@ -21,7 +21,7 @@
/*! /*!
@file value.hpp @file value.hpp
@brief Value interface and concrete subclasses @brief Value interface and concrete subclasses
@version $Name: $ $Revision: 1.3 $ @version $Name: $ $Revision: 1.4 $
@author Andreas Huggel (ahu) @author Andreas Huggel (ahu)
<a href="mailto:ahuggel@gmx.net">ahuggel@gmx.net</a> <a href="mailto:ahuggel@gmx.net">ahuggel@gmx.net</a>
@date 09-Jan-04, ahu: created @date 09-Jan-04, ahu: created
@ -113,6 +113,22 @@ namespace Exif {
@return The converted value. @return The converted value.
*/ */
virtual long toLong(long n =0) const =0; virtual long toLong(long n =0) const =0;
/*!
@brief Convert the n-th component of the value to a float. The
behaviour of this method may be undefined if there is no
n-th component.
@return The converted value.
*/
virtual float toFloat(long n =0) const =0;
/*!
@brief Convert the n-th component of the value to a Rational. The
behaviour of this method may be undefined if there is no
n-th component.
@return The converted value.
*/
virtual Rational toRational(long n =0) const =0;
/*! /*!
@brief Return the value as a string. Implemented in terms of @brief Return the value as a string. Implemented in terms of
write(std::ostream& os) const of the concrete class. write(std::ostream& os) const of the concrete class.
@ -171,6 +187,8 @@ namespace Exif {
public: public:
//! Default constructor. //! Default constructor.
DataValue(TypeId typeId =undefined) : Value(typeId) {} DataValue(TypeId typeId =undefined) : Value(typeId) {}
//! Virtual destructor.
virtual ~DataValue() {}
//! Assignment operator. //! Assignment operator.
DataValue& operator=(const DataValue& rhs); DataValue& operator=(const DataValue& rhs);
/*! /*!
@ -201,6 +219,9 @@ namespace Exif {
virtual Value* clone() const; virtual Value* clone() const;
virtual std::ostream& write(std::ostream& os) const; virtual std::ostream& write(std::ostream& os) const;
virtual long toLong(long n =0) const { return value_[n]; } virtual long toLong(long n =0) const { return value_[n]; }
virtual float toFloat(long n =0) const { return value_[n]; }
virtual Rational toRational(long n =0) const
{ return Rational(value_[n], 1); }
private: private:
std::string value_; std::string value_;
@ -212,6 +233,8 @@ namespace Exif {
public: public:
//! Default constructor. //! Default constructor.
AsciiValue() : Value(asciiString) {} AsciiValue() : Value(asciiString) {}
//! Virtual destructor.
virtual ~AsciiValue() {}
//! Assignment operator. //! Assignment operator.
AsciiValue& operator=(const AsciiValue& rhs); AsciiValue& operator=(const AsciiValue& rhs);
/*! /*!
@ -251,6 +274,9 @@ namespace Exif {
*/ */
virtual std::ostream& write(std::ostream& os) const; virtual std::ostream& write(std::ostream& os) const;
virtual long toLong(long n =0) const { return value_[n]; } virtual long toLong(long n =0) const { return value_[n]; }
virtual float toFloat(long n =0) const { return value_[n]; }
virtual Rational toRational(long n =0) const
{ return Rational(value_[n], 1); }
private: private:
std::string value_; std::string value_;
@ -285,6 +311,8 @@ namespace Exif {
public: public:
//! Default constructor. //! Default constructor.
ValueType() : Value(getType<T>()) {} ValueType() : Value(getType<T>()) {}
//! Virtual destructor.
virtual ~ValueType() {}
//! Assignment operator. //! Assignment operator.
ValueType<T>& operator=(const ValueType<T>& rhs); ValueType<T>& operator=(const ValueType<T>& rhs);
virtual void read(const char* buf, long len, ByteOrder byteOrder); virtual void read(const char* buf, long len, ByteOrder byteOrder);
@ -301,6 +329,8 @@ namespace Exif {
virtual Value* clone() const; virtual Value* clone() const;
virtual std::ostream& write(std::ostream& os) const; virtual std::ostream& write(std::ostream& os) const;
virtual long toLong(long n =0) const; virtual long toLong(long n =0) const;
virtual float toFloat(long n =0) const;
virtual Rational toRational(long n =0) const;
//! Container for values //! Container for values
typedef std::vector<T> ValueList; typedef std::vector<T> ValueList;
@ -533,6 +563,42 @@ namespace Exif {
{ {
return value_[n].first / value_[n].second; return value_[n].first / value_[n].second;
} }
// Default implementation
template<typename T>
inline float ValueType<T>::toFloat(long n) const
{
return value_[n];
}
// Specialization for rational
template<>
inline float ValueType<Rational>::toFloat(long n) const
{
return (float)value_[n].first / value_[n].second;
}
// Specialization for unsigned rational
template<>
inline float ValueType<URational>::toFloat(long n) const
{
return (float)value_[n].first / value_[n].second;
}
// Default implementation
template<typename T>
inline Rational ValueType<T>::toRational(long n) const
{
return Rational(value_[n], 1);
}
// Specialization for rational
template<>
inline Rational ValueType<Rational>::toRational(long n) const
{
return Rational(value_[n].first, value_[n].second);
}
// Specialization for unsigned rational
template<>
inline Rational ValueType<URational>::toRational(long n) const
{
return Rational(value_[n].first, value_[n].second);
}
} // namespace Exif } // namespace Exif

Loading…
Cancel
Save