enum ErrorMode
{
+ DEFAULT,
THROW_ON_ERROR,
IGNORE_ERRORS,
TRANSLITERATE
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
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;
}
/** Convenience function that encodes a string. */
-template<class C>
+template<typename C>
std::string encode(const ustring &s)
{
typename C::Encoder enc;
}
/** 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));