#define MSP_STRINGCODEC_CODEC_H_
#include <string>
-#include <msp/core/except.h>
+#include "except.h"
#include "ustring.h"
namespace Msp {
TRANSLITERATE
};
-/**
-An exception thrown for all kinds of problems encountered while encoding or
-decoding strings.
-*/
-class CodecError: public Exception
-{
-public:
- CodecError(const std::string &w_): Exception(w_) { }
-};
/**
Base class for string codecs. Use one of the derived classes or the function
protected:
/** Handles an error depending on the error mode.
- THROW_ON_ERROR: throws CodecError(msg)
+ THROW_ON_ERROR: throws err
IGNORE_ERRORS: does nothing
TRANSLITERATE: calls transliterate(ch, buf) */
- void error(unichar ch, std::string &buf, const std::string &msg);
+ template<typename E>
+ void error(unichar ch, std::string &buf, const E &err)
+ {
+ if(err_mode==TRANSLITERATE)
+ transliterate(ch, buf);
+ else if(err_mode!=IGNORE_ERRORS)
+ throw err;
+ }
/** Attempts to produce an alternative encoding for a unicode character.
Typically this includes dropping accent marks or romanizing letters. */
/** Handles an error depending on the error mode. The return value is
suitable for returning from decode_char.
- THROW_ON_ERROR: throws CodecError(msg)
+ THROW_ON_ERROR: throws err
IGNORE_ERRORS: returns -1
TRANSLITERATE: return 0xFFFD */
- unichar error(const std::string &msg);
+ template<typename E>
+ unichar error(const E &err)
+ {
+ if(err_mode==TRANSLITERATE)
+ return 0xFFFD;
+ else if(err_mode==IGNORE_ERRORS)
+ return -1;
+ else
+ throw err;
+ }
};
protected: