]> git.tdb.fi Git - libs/core.git/blobdiff - source/stringcodec/codec.h
Exception changes
[libs/core.git] / source / stringcodec / codec.h
index 5acbe7b0d1dc34b1d7285c6b96a63665b1ffde2f..00f03b0cc4b4c4f2f4fcd133f2d3fa67e9fac5fb 100644 (file)
@@ -2,7 +2,7 @@
 #define MSP_STRINGCODEC_CODEC_H_
 
 #include <string>
-#include <msp/core/except.h>
+#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<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. */
@@ -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<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: