X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fstringcodec%2Fcodec.h;fp=source%2Fstringcodec%2Fcodec.h;h=00f03b0cc4b4c4f2f4fcd133f2d3fa67e9fac5fb;hp=5acbe7b0d1dc34b1d7285c6b96a63665b1ffde2f;hb=94ee3a1040f67d9de2e92fc34049642b08d65b3e;hpb=93bb92bad766d269a6bac87e00020a6158531739 diff --git a/source/stringcodec/codec.h b/source/stringcodec/codec.h index 5acbe7b..00f03b0 100644 --- a/source/stringcodec/codec.h +++ b/source/stringcodec/codec.h @@ -2,7 +2,7 @@ #define MSP_STRINGCODEC_CODEC_H_ #include -#include +#include "except.h" #include "ustring.h" namespace Msp { @@ -15,15 +15,6 @@ enum ErrorMode 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 @@ -73,10 +64,17 @@ public: 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 + 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. */ @@ -116,10 +114,19 @@ public: /** 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 + unichar error(const E &err) + { + if(err_mode==TRANSLITERATE) + return 0xFFFD; + else if(err_mode==IGNORE_ERRORS) + return -1; + else + throw err; + } }; protected: