add constructors and shift operator overload

This commit is contained in:
2019-03-07 21:44:00 +01:00
parent 3e16341cf2
commit bf82505de4
2 changed files with 50 additions and 4 deletions

View File

@@ -270,7 +270,7 @@ void BinaryStreamWriter::wwstring(std::wstring s)
{ {
wu32(s.size()); wu32(s.size());
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff>> converter; std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff>> converter;
wstring(converter.to_bytes(s)); wstring_raw(converter.to_bytes(s));
} }
void BinaryStreamWriter::wwstring_raw(std::wstring s) void BinaryStreamWriter::wwstring_raw(std::wstring s)

View File

@@ -4,6 +4,18 @@
#include "util.h" #include "util.h"
#include "log.h" #include "log.h"
template<typename T> BinaryStreamWriter& operator<<(BinaryStreamWriter& w, const T& obj)
{
obj.write(w);
return w;
}
template<typename T> BinaryStreamReader& operator>>(BinaryStreamReader& r, const T& obj)
{
obj.read(r);
return r;
}
class Serializer class Serializer
{ {
public: public:
@@ -65,6 +77,8 @@ public:
{ {
using native_type = double; using native_type = double;
double value; double value;
Double() = default;
Double(double value) : value(value) { }
virtual std::string type_key() const override { return "doub"; } virtual std::string type_key() const override { return "doub"; }
virtual std::string str(int indent, const std::string& prefix) const override virtual std::string str(int indent, const std::string& prefix) const override
{ {
@@ -83,8 +97,16 @@ public:
struct UnitFloat : public Type struct UnitFloat : public Type
{ {
using native_type = double; using native_type = double;
static constexpr const char* UnitAngle = "#Ang"; // angle: base degrees
static constexpr const char* UnitDensity = "#Rsl"; // density: base per inch
static constexpr const char* UnitDist = "#Rlt"; // distance: base 72ppi
static constexpr const char* UnitNone = "#Nne"; // none: coerced.
static constexpr const char* UnitPercent = "#Prc"; // percent: unit value
static constexpr const char* UnitPixel = "#Pxl"; // pixels: tagged unit value
std::string unit; std::string unit;
double value; double value;
UnitFloat() = default;
UnitFloat(const std::string& unit, double value) : unit(unit), value(value) { }
virtual std::string type_key() const override { return "UntF"; } virtual std::string type_key() const override { return "UntF"; }
virtual std::string str(int indent, const std::string& prefix) const override virtual std::string str(int indent, const std::string& prefix) const override
{ {
@@ -106,6 +128,9 @@ public:
{ {
using native_type = std::wstring; using native_type = std::wstring;
std::wstring value; std::wstring value;
String() = default;
String(const std::wstring& s) : value(s) { }
String(const std::string& s) : value(str2wstr(s)) { }
virtual std::string type_key() const override { return "TEXT"; } virtual std::string type_key() const override { return "TEXT"; }
virtual std::string str(int indent, const std::string& prefix) const override virtual std::string str(int indent, const std::string& prefix) const override
{ {
@@ -125,6 +150,8 @@ public:
{ {
std::string type; std::string type;
std::string value; std::string value;
Enum() = default;
Enum(const std::string& type, const std::string& value) : type(type), value(value) { }
virtual std::string type_key() const override { return "enum"; } virtual std::string type_key() const override { return "enum"; }
virtual std::string str(int indent, const std::string& prefix) const override virtual std::string str(int indent, const std::string& prefix) const override
{ {
@@ -146,6 +173,8 @@ public:
{ {
using native_type = int32_t; using native_type = int32_t;
int32_t value; int32_t value;
Integer() = default;
Integer(int32_t value) : value(value) { }
virtual std::string type_key() const override { return "long"; } virtual std::string type_key() const override { return "long"; }
virtual std::string str(int indent, const std::string& prefix) const override virtual std::string str(int indent, const std::string& prefix) const override
{ {
@@ -165,6 +194,8 @@ public:
{ {
using native_type = bool; using native_type = bool;
bool value; bool value;
Boolean() = default;
Boolean(bool value) : value(value) { }
virtual std::string type_key() const override { return "bool"; } virtual std::string type_key() const override { return "bool"; }
virtual std::string str(int indent, const std::string& prefix) const override virtual std::string str(int indent, const std::string& prefix) const override
{ {
@@ -183,6 +214,9 @@ public:
struct RawData : public Type struct RawData : public Type
{ {
std::vector<uint8_t> data; std::vector<uint8_t> data;
RawData() = default;
//RawData(const RawData&) = delete;
RawData(std::vector<uint8_t> data) : data(data) { }
virtual std::string type_key() const override { return "tdta"; } virtual std::string type_key() const override { return "tdta"; }
virtual std::string str(int indent, const std::string& prefix) const override virtual std::string str(int indent, const std::string& prefix) const override
{ {
@@ -203,6 +237,7 @@ public:
std::wstring name; std::wstring name;
std::string class_id; std::string class_id;
Type::Map props; Type::Map props;
Descriptor() = default;
virtual std::string type_key() const override { return "Objc"; } virtual std::string type_key() const override { return "Objc"; }
virtual std::string str(int indent, const std::string& prefix) const override virtual std::string str(int indent, const std::string& prefix) const override
{ {
@@ -275,6 +310,9 @@ public:
uint32_t left; uint32_t left;
uint32_t bottom; uint32_t bottom;
uint32_t right; uint32_t right;
Rectangle() = default;
Rectangle(uint32_t top, uint32_t left, uint32_t bottom, uint32_t right) :
top(top), left(left), bottom(bottom), right(right) { }
uint32_t area() const { return (right - left) * (bottom - top); } uint32_t area() const { return (right - left) * (bottom - top); }
uint32_t width() const { return right - left; } uint32_t width() const { return right - left; }
uint32_t height() const { return bottom - top; } uint32_t height() const { return bottom - top; }
@@ -302,6 +340,8 @@ public:
{ {
uint32_t x; uint32_t x;
uint32_t y; uint32_t y;
Point() = default;
Point(uint32_t x, uint32_t y) : x(x), y(y) { }
virtual std::string str(int indent, const std::string& prefix) const override virtual std::string str(int indent, const std::string& prefix) const override
{ {
return std::string(indent, '-') + prefix + fmt::format("point: [{}, {}]", x, y); return std::string(indent, '-') + prefix + fmt::format("point: [{}, {}]", x, y);
@@ -325,8 +365,8 @@ public:
uint8_t compression; uint8_t compression;
std::vector<uint8_t> data; std::vector<uint8_t> data;
Channel() = default; Channel() = default;
//Channel(uint32_t depth, Rectangle rect, uint8_t compression, std::vector<uint8_t> data) : Channel(uint32_t depth, Rectangle rect, uint8_t compression, std::vector<uint8_t> data) :
// depth(depth), rect(rect), compression(compression), data(std::move(data)) { } depth(depth), rect(rect), compression(compression), data(std::move(data)) { }
virtual bool read(BinaryStreamReader& r) override virtual bool read(BinaryStreamReader& r) override
{ {
auto length = r.ru32(); // skip if 0, length is from the next field to the end auto length = r.ru32(); // skip if 0, length is from the next field to the end
@@ -380,7 +420,7 @@ public:
Rectangle rect; Rectangle rect;
std::vector<Channel> channels; std::vector<Channel> channels;
VMArray() = default; VMArray() = default;
//VMArray(uint32_t version, const Rectangle& rect) : version(version), rect(rect) { } VMArray(uint32_t version, const Rectangle& rect) : version(version), rect(rect) { }
std::shared_ptr<Image> image(bool grayscale, bool invert) const std::shared_ptr<Image> image(bool grayscale, bool invert) const
{ {
int nc = channels.size(); int nc = channels.size();
@@ -465,6 +505,12 @@ public:
{ {
} }
}; };
template<typename T> static T parse(BinaryStreamReader& r)
{
T ret{};
ret.read(r);
return ret;
}
protected: protected:
static Type::Ref instanciate(const std::string& key); static Type::Ref instanciate(const std::string& key);
static std::map<std::string /*key*/, std::function<Type::Ref()>> m_ctor_table; static std::map<std::string /*key*/, std::function<Type::Ref()>> m_ctor_table;