]> git.tdb.fi Git - libs/core.git/blobdiff - source/stringcodec/codec.h
Mark overridden virtual functions as such
[libs/core.git] / source / stringcodec / codec.h
index 00f03b0cc4b4c4f2f4fcd133f2d3fa67e9fac5fb..5e67d46049078f3555994eeca393ce9691bf75f8 100644 (file)
@@ -10,6 +10,7 @@ namespace StringCodec {
 
 enum ErrorMode
 {
+       DEFAULT,
        THROW_ON_ERROR,
        IGNORE_ERRORS,
        TRANSLITERATE
@@ -38,11 +39,11 @@ public:
        class Encoder
        {
        protected:
-               ErrorMode err_mode;
+               ErrorMode err_mode = THROW_ON_ERROR;
 
-               Encoder(ErrorMode em): err_mode(em) { }
+               Encoder(ErrorMode em): err_mode(em==DEFAULT ? THROW_ON_ERROR : em) { }
        public:
-               virtual ~Encoder() { }
+               virtual ~Encoder() = default;
 
                /** Encodes a single unicode character.  If the character can't be
                represented in this encoding, error() should be called. */
@@ -89,11 +90,11 @@ public:
        class Decoder
        {
        protected:
-               ErrorMode err_mode;
+               ErrorMode err_mode = THROW_ON_ERROR;
 
-               Decoder(ErrorMode em): err_mode(em) { }
+               Decoder(ErrorMode em): err_mode(em==DEFAULT ? THROW_ON_ERROR : em) { }
        public:
-               virtual ~Decoder() { }
+               virtual ~Decoder() = default;
 
                /** Decodes a single character from a string.  The iterator is advanced
                to the next character.  For stateful codecs, -1 may be returned if a
@@ -130,18 +131,18 @@ public:
        };
 
 protected:
-       Codec() { }
+       Codec() = default;
 public:
-       virtual ~Codec() { }
+       virtual ~Codec() = default;
 
        /** Returns the name of the encoding handled by this codec. */
        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
@@ -153,8 +154,33 @@ 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 = THROW_ON_ERROR;
+
+protected:
+       StandardCodec(ErrorMode em): err_mode(em==DEFAULT ? THROW_ON_ERROR : em) { }
+
+       ErrorMode get_error_mode(ErrorMode em = DEFAULT) const
+       { return (em==DEFAULT ? err_mode : em); }
+
+public:
+       Encoder *create_encoder(ErrorMode em = DEFAULT) const override
+       { return new typename C::Encoder(get_error_mode(em)); }
+
+       Decoder *create_decoder(ErrorMode em = DEFAULT) const override
+       { return new typename C::Decoder(get_error_mode(em)); }
+};
+
+
 /** Convenience function that decodes a string. */
-template<class C>
+template<typename C>
 ustring decode(const std::string &s)
 {
        typename C::Decoder dec;
@@ -162,7 +188,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;
@@ -170,7 +196,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));