X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fstringcodec%2Fcodec.h;h=c70e052dd1b35e819a591327777f8eec4dbdc9bd;hp=00f03b0cc4b4c4f2f4fcd133f2d3fa67e9fac5fb;hb=d3fc0bf0f20f100f2831188c1ce21461d21c2c7a;hpb=94ee3a1040f67d9de2e92fc34049642b08d65b3e diff --git a/source/stringcodec/codec.h b/source/stringcodec/codec.h index 00f03b0..c70e052 100644 --- a/source/stringcodec/codec.h +++ b/source/stringcodec/codec.h @@ -10,6 +10,7 @@ namespace StringCodec { enum ErrorMode { + DEFAULT, THROW_ON_ERROR, IGNORE_ERRORS, TRANSLITERATE @@ -138,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 @@ -153,6 +154,28 @@ public: typedef Codec::Encoder Encoder; typedef Codec::Decoder Decoder; + +/** +A helper class to provide some common functionality. +*/ +template +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 ustring decode(const std::string &s)