error.hpp
Go to the documentation of this file.
// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2021 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
#ifndef ERROR_HPP_
#define ERROR_HPP_
// *****************************************************************************
#include "exiv2lib_export.h"
// included header files
#include "types.hpp"
// + standard includes
#include <exception>
#include <string>
// *****************************************************************************
// namespace extensions
namespace Exiv2 {
// *****************************************************************************
// class definitions
class EXIV2API LogMsg {
LogMsg(const LogMsg&);
LogMsg& operator=(const LogMsg&);
public:
enum Level { debug = 0, info = 1, warn = 2, error = 3, mute = 4 };
typedef void (*Handler)(int, const char*);
explicit LogMsg(Level msgType);
~LogMsg();
std::ostringstream& os();
static void setLevel(Level level);
static void setHandler(Handler handler);
static Level level();
static Handler handler();
static void defaultHandler(int level, const char* s);
private:
// DATA
// The output level. Only messages with type >= level_ will be written
static Level level_;
// The log handler in use
static Handler handler_;
// The type of this log message
const Level msgType_;
// Holds the log message until it is passed to the message handler
std::ostringstream os_;
}; // class LogMsg
// Macros for simple access
#define EXV_DEBUG if (LogMsg::debug >= LogMsg::level() && LogMsg::handler()) LogMsg(LogMsg::debug).os()
#define EXV_INFO if (LogMsg::info >= LogMsg::level() && LogMsg::handler()) LogMsg(LogMsg::info).os()
#define EXV_WARNING if (LogMsg::warn >= LogMsg::level() && LogMsg::handler()) LogMsg(LogMsg::warn).os()
#define EXV_ERROR if (LogMsg::error >= LogMsg::level() && LogMsg::handler()) LogMsg(LogMsg::error).os()
#ifdef _MSC_VER
// Disable MSVC warnings "non - DLL-interface classkey 'identifier' used as base
// for DLL-interface classkey 'identifier'"
# pragma warning( disable : 4275 )
#endif
template<typename charT, typename T>
std::basic_string<charT> toBasicString(const T& arg)
{
std::basic_ostringstream<charT> os;
os << arg;
return os.str();
}
class EXIV2API AnyError : public std::exception {
public:
AnyError();
AnyError(const AnyError& o);
virtual ~AnyError() throw();
virtual int code() const throw() =0;
};
inline std::ostream& operator<<(std::ostream& os, const AnyError& error)
{
return os << error.what();
}
enum ErrorCode {
kerGeneralError = -1,
kerSuccess = 0,
kerErrorMessage,
kerCallFailed,
kerNotAnImage,
kerInvalidDataset,
kerInvalidRecord,
kerInvalidKey,
kerInvalidTag,
kerValueNotSet,
kerDataSourceOpenFailed,
kerFileOpenFailed,
kerFileContainsUnknownImageType,
kerMemoryContainsUnknownImageType,
kerUnsupportedImageType,
kerFailedToReadImageData,
kerNotAJpeg,
kerFailedToMapFileForReadWrite,
kerFileRenameFailed,
kerTransferFailed,
kerMemoryTransferFailed,
kerInputDataReadFailed,
kerImageWriteFailed,
kerNoImageInInputData,
kerInvalidIfdId,
kerValueTooLarge,
kerDataAreaValueTooLarge,
kerOffsetOutOfRange,
kerUnsupportedDataAreaOffsetType,
kerInvalidCharset,
kerUnsupportedDateFormat,
kerUnsupportedTimeFormat,
kerWritingImageFormatUnsupported,
kerInvalidSettingForImage,
kerNotACrwImage,
kerFunctionNotSupported,
kerNoNamespaceInfoForXmpPrefix,
kerNoPrefixForNamespace,
kerTooLargeJpegSegment,
kerUnhandledXmpdatum,
kerUnhandledXmpNode,
kerXMPToolkitError,
kerDecodeLangAltPropertyFailed,
kerDecodeLangAltQualifierFailed,
kerEncodeLangAltPropertyFailed,
kerPropertyNameIdentificationFailed,
kerSchemaNamespaceNotRegistered,
kerNoNamespaceForPrefix,
kerAliasesNotSupported,
kerInvalidXmpText,
kerTooManyTiffDirectoryEntries,
kerMultipleTiffArrayElementTagsInDirectory,
kerWrongTiffArrayElementTagType,
kerInvalidKeyXmpValue,
kerInvalidIccProfile,
kerInvalidXMP,
kerTiffDirectoryTooLarge,
kerInvalidTypeValue,
kerInvalidLangAltValue,
kerInvalidMalloc,
kerCorruptedMetadata,
kerArithmeticOverflow,
kerMallocFailed,
};
template<typename charT>
class EXIV2API BasicError : public AnyError {
public:
explicit inline BasicError(ErrorCode code);
template<typename A>
inline BasicError(ErrorCode code, const A& arg1);
template<typename A, typename B>
inline BasicError(ErrorCode code, const A& arg1, const B& arg2);
template<typename A, typename B, typename C>
inline BasicError(ErrorCode code, const A& arg1, const B& arg2, const C& arg3);
virtual inline ~BasicError() throw();
virtual inline int code() const throw();
virtual inline const char* what() const throw();
#ifdef EXV_UNICODE_PATH
virtual inline const wchar_t* wwhat() const throw();
#endif
private:
void setMsg();
// DATA
ErrorCode code_;
int count_;
std::basic_string<charT> arg1_;
std::basic_string<charT> arg2_;
std::basic_string<charT> arg3_;
std::string msg_;
#ifdef EXV_UNICODE_PATH
std::wstring wmsg_;
#endif
}; // class BasicError
typedef BasicError<char> Error;
#ifdef EXV_UNICODE_PATH
typedef BasicError<wchar_t> WError;
#endif
// *****************************************************************************
// free functions, template and inline definitions
const char* errMsg(int code);
template<typename charT>
BasicError<charT>::BasicError(ErrorCode code)
: code_(code), count_(0)
{
setMsg();
}
template<typename charT> template<typename A>
BasicError<charT>::BasicError(ErrorCode code, const A& arg1)
: code_(code), count_(1), arg1_(toBasicString<charT>(arg1))
{
setMsg();
}
template<typename charT> template<typename A, typename B>
BasicError<charT>::BasicError(ErrorCode code, const A& arg1, const B& arg2)
: code_(code), count_(2),
arg1_(toBasicString<charT>(arg1)),
arg2_(toBasicString<charT>(arg2))
{
setMsg();
}
template<typename charT> template<typename A, typename B, typename C>
BasicError<charT>::BasicError(ErrorCode code, const A& arg1, const B& arg2, const C& arg3)
: code_(code), count_(3),
arg1_(toBasicString<charT>(arg1)),
arg2_(toBasicString<charT>(arg2)),
arg3_(toBasicString<charT>(arg3))
{
setMsg();
}
template<typename charT>
BasicError<charT>::~BasicError() throw()
{
}
template<typename charT>
int BasicError<charT>::code() const throw()
{
return code_;
}
template<typename charT>
const char* BasicError<charT>::what() const throw()
{
return msg_.c_str();
}
#ifdef EXV_UNICODE_PATH
template<typename charT>
const wchar_t* BasicError<charT>::wwhat() const throw()
{
return wmsg_.c_str();
}
#endif
#ifdef _MSC_VER
# pragma warning( default : 4275 )
#endif
} // namespace Exiv2
#endif // #ifndef ERROR_HPP_
Exiv2::AnyError
Error class interface. Allows the definition and use of a hierarchy of error classes which can all be...
Definition: error.hpp:174
Exiv2::AnyError::code
virtual int code() const =0
Return the error code.
Exiv2::BasicError
Simple error class used for exceptions. An output operator is provided to print errors to a stream.
Definition: error.hpp:264
Exiv2::BasicError::BasicError
BasicError(ErrorCode code)
Constructor taking only an error code.
Definition: error.hpp:337
Exiv2::BasicError::~BasicError
virtual ~BasicError()
Virtual destructor. (Needed because of throw())
Definition: error.hpp:370
Exiv2::BasicError::code
virtual int code() const
Return the error code.
Definition: error.hpp:375
Exiv2::BasicError::what
virtual const char * what() const
Return the error message as a C-string. The pointer returned by what() is valid only as long as the B...
Definition: error.hpp:381
Exiv2::LogMsg
Class for a log message, used by the library. Applications can set the log level and provide a custom...
Definition: error.hpp:75
Exiv2::LogMsg::Level
Level
Defined log levels. To suppress all log messages, either set the log level to mute or set the log mes...
Definition: error.hpp:85
Exiv2
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
Exiv2::ErrorCode
ErrorCode
Complete list of all Exiv2 error codes.
Definition: error.hpp:191
Exiv2::kerValueTooLarge
@ kerValueTooLarge
Entry::setValue: Value too large.
Definition: error.hpp:218
Exiv2::kerDataAreaValueTooLarge
@ kerDataAreaValueTooLarge
Entry::setDataArea: Value too large.
Definition: error.hpp:220
Exiv2::toBasicString
std::basic_string< charT > toBasicString(const T &arg)
Generalised toString function.
Definition: error.hpp:160
Exiv2::errMsg
const char * errMsg(int code)
Return the error message for the error with code code.
Definition: error.cpp:298
Exiv2::Error
BasicError< char > Error
Error class used for exceptions (std::string based)
Definition: error.hpp:324