]> git.tdb.fi Git - libs/core.git/blobdiff - source/stringcodec/codec.h
Let the base classes decide the default error mode
[libs/core.git] / source / stringcodec / codec.h
index 5acbe7b0d1dc34b1d7285c6b96a63665b1ffde2f..d0871ff119ab563ea2ff000ece006c55577ddf48 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 {
@@ -10,20 +10,12 @@ namespace StringCodec {
 
 enum ErrorMode
 {
+       DEFAULT,
        THROW_ON_ERROR,
        IGNORE_ERRORS,
        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
@@ -49,7 +41,7 @@ public:
        protected:
                ErrorMode err_mode;
 
-               Encoder(ErrorMode em): err_mode(em) { }
+               Encoder(ErrorMode em): err_mode(em==DEFAULT ? THROW_ON_ERROR : em) { }
        public:
                virtual ~Encoder() { }
 
@@ -73,10 +65,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. */
@@ -93,7 +92,7 @@ public:
        protected:
                ErrorMode err_mode;
 
-               Decoder(ErrorMode em): err_mode(em) { }
+               Decoder(ErrorMode em): err_mode(em==DEFAULT ? THROW_ON_ERROR : em) { }
        public:
                virtual ~Decoder() { }
 
@@ -116,10 +115,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:
@@ -131,10 +139,10 @@ public:
        virtual const char *get_name() const = 0;
 
        /** Creates an encoder for this codec. */
-       virtual Encoder *create_encoder(ErrorMode err_mode = THROW_ON_ERROR) const = 0;
+       virtual Encoder *create_encoder(ErrorMode err_mode = DEFAULT) const = 0;
 
        /** Creates a decoder for this codec. */
-       virtual Decoder *create_decoder(ErrorMode err_mode = THROW_ON_ERROR) const = 0;
+       virtual Decoder *create_decoder(ErrorMode err_mode = DEFAULT) const = 0;
 
        /** Determines whether the given string can be successfully decoded with
        this codec.  Note that this function returning true does not guarantee that
@@ -146,8 +154,30 @@ public:
 typedef Codec::Encoder Encoder;
 typedef Codec::Decoder Decoder;
 
+
+/**
+A helper class to provide some common functionality.
+*/
+template<typename C>
+class StandardCodec: public Codec
+{
+private:
+       ErrorMode err_mode;
+
+protected:
+       StandardCodec(ErrorMode em): err_mode(em==DEFAULT ? THROW_ON_ERROR : em) { }
+
+public:
+       virtual Encoder *create_encoder(ErrorMode em = DEFAULT) const
+       { return new typename C::Encoder(em==DEFAULT ? err_mode : em); }
+
+       virtual Decoder *create_decoder(ErrorMode em = DEFAULT) const
+       { return new typename C::Decoder(em==DEFAULT ? err_mode : em); }
+};
+
+
 /** Convenience function that decodes a string. */
-template<class C>
+template<typename C>
 ustring decode(const std::string &s)
 {
        typename C::Decoder dec;
@@ -155,7 +185,7 @@ ustring decode(const std::string &s)
 }
 
 /** Convenience function that encodes a string. */
-template<class C>
+template<typename C>
 std::string encode(const ustring &s)
 {
        typename C::Encoder enc;
@@ -163,7 +193,7 @@ std::string encode(const ustring &s)
 }
 
 /** Convenience function that transcodes a string from one codec to another. */
-template<class F, class T>
+template<typename F, typename T>
 std::string transcode(const std::string &s)
 {
        return encode<T>(decode<F>(s));