Add a SettingChar type

This commit is contained in:
Ivan Zhukov 2018-04-22 02:38:01 +03:00
parent a138ab2820
commit e84263eb3b

View File

@ -706,4 +706,57 @@ struct SettingString
}
};
struct SettingChar
{
private:
void checkStringIsACharacter(const String & x) const {
if (x.size() != 1)
throw Exception(std::string("A setting's value string has to be an exactly one character long"));
}
public:
char value;
bool changed = false;
SettingChar(char x = '\0') : value(x) {}
operator char() const { return value; }
SettingChar & operator= (char x) { set(x); return *this; }
String toString() const
{
return String(1, value);
}
void set(char x) {
value = x;
changed = true;
}
void set(const String & x)
{
checkStringIsACharacter(x);
value = x[0];
changed = true;
}
void set(const Field & x)
{
String s = safeGet<const String &>(x);
checkStringIsACharacter(s);
set(s);
}
void set(ReadBuffer & buf)
{
String x;
readBinary(x, buf);
checkStringIsACharacter(x);
set(x);
}
void write(WriteBuffer & buf) const
{
writeBinary(toString(), buf);
}
};
}