X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fstringcodec%2Fcodec.h;h=f434ed82e597c706b7bf54de4e24081b4447f693;hb=99b9121e2158603372c7313400283b622e6754d8;hp=c70e052dd1b35e819a591327777f8eec4dbdc9bd;hpb=d3fc0bf0f20f100f2831188c1ce21461d21c2c7a;p=libs%2Fcore.git diff --git a/source/stringcodec/codec.h b/source/stringcodec/codec.h index c70e052..f434ed8 100644 --- a/source/stringcodec/codec.h +++ b/source/stringcodec/codec.h @@ -39,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. */ @@ -90,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 @@ -131,9 +131,9 @@ 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; @@ -162,22 +162,25 @@ template class StandardCodec: public Codec { private: - ErrorMode err_mode; + 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: virtual Encoder *create_encoder(ErrorMode em = DEFAULT) const - { return new typename C::Encoder(em==DEFAULT ? err_mode : em); } + { return new typename C::Encoder(get_error_mode(em)); } virtual Decoder *create_decoder(ErrorMode em = DEFAULT) const - { return new typename C::Decoder(em==DEFAULT ? err_mode : em); } + { return new typename C::Decoder(get_error_mode(em)); } }; /** Convenience function that decodes a string. */ -template +template ustring decode(const std::string &s) { typename C::Decoder dec; @@ -185,7 +188,7 @@ ustring decode(const std::string &s) } /** Convenience function that encodes a string. */ -template +template std::string encode(const ustring &s) { typename C::Encoder enc; @@ -193,7 +196,7 @@ std::string encode(const ustring &s) } /** Convenience function that transcodes a string from one codec to another. */ -template +template std::string transcode(const std::string &s) { return encode(decode(s));