diff --git a/src/binary_stream.cpp b/src/binary_stream.cpp index 047f858..906a2ef 100644 --- a/src/binary_stream.cpp +++ b/src/binary_stream.cpp @@ -270,7 +270,7 @@ void BinaryStreamWriter::wwstring(std::wstring s) { wu32(s.size()); std::wstring_convert> converter; - wstring(converter.to_bytes(s)); + wstring_raw(converter.to_bytes(s)); } void BinaryStreamWriter::wwstring_raw(std::wstring s) diff --git a/src/serializer.h b/src/serializer.h index 85d2819..b787560 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -4,6 +4,18 @@ #include "util.h" #include "log.h" +template BinaryStreamWriter& operator<<(BinaryStreamWriter& w, const T& obj) +{ + obj.write(w); + return w; +} + +template BinaryStreamReader& operator>>(BinaryStreamReader& r, const T& obj) +{ + obj.read(r); + return r; +} + class Serializer { public: @@ -65,6 +77,8 @@ public: { using native_type = double; double value; + Double() = default; + Double(double value) : value(value) { } virtual std::string type_key() const override { return "doub"; } virtual std::string str(int indent, const std::string& prefix) const override { @@ -83,8 +97,16 @@ public: struct UnitFloat : public Type { 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; 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 str(int indent, const std::string& prefix) const override { @@ -106,6 +128,9 @@ public: { using native_type = std::wstring; 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 str(int indent, const std::string& prefix) const override { @@ -125,6 +150,8 @@ public: { std::string type; 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 str(int indent, const std::string& prefix) const override { @@ -146,6 +173,8 @@ public: { using native_type = int32_t; int32_t value; + Integer() = default; + Integer(int32_t value) : value(value) { } virtual std::string type_key() const override { return "long"; } virtual std::string str(int indent, const std::string& prefix) const override { @@ -165,6 +194,8 @@ public: { using native_type = bool; bool value; + Boolean() = default; + Boolean(bool value) : value(value) { } virtual std::string type_key() const override { return "bool"; } virtual std::string str(int indent, const std::string& prefix) const override { @@ -183,6 +214,9 @@ public: struct RawData : public Type { std::vector data; + RawData() = default; + //RawData(const RawData&) = delete; + RawData(std::vector data) : data(data) { } virtual std::string type_key() const override { return "tdta"; } virtual std::string str(int indent, const std::string& prefix) const override { @@ -203,6 +237,7 @@ public: std::wstring name; std::string class_id; Type::Map props; + Descriptor() = default; virtual std::string type_key() const override { return "Objc"; } virtual std::string str(int indent, const std::string& prefix) const override { @@ -275,6 +310,9 @@ public: uint32_t left; uint32_t bottom; 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 width() const { return right - left; } uint32_t height() const { return bottom - top; } @@ -302,6 +340,8 @@ public: { uint32_t x; 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 { return std::string(indent, '-') + prefix + fmt::format("point: [{}, {}]", x, y); @@ -325,8 +365,8 @@ public: uint8_t compression; std::vector data; Channel() = default; - //Channel(uint32_t depth, Rectangle rect, uint8_t compression, std::vector data) : - // depth(depth), rect(rect), compression(compression), data(std::move(data)) { } + Channel(uint32_t depth, Rectangle rect, uint8_t compression, std::vector data) : + depth(depth), rect(rect), compression(compression), data(std::move(data)) { } virtual bool read(BinaryStreamReader& r) override { auto length = r.ru32(); // skip if 0, length is from the next field to the end @@ -380,7 +420,7 @@ public: Rectangle rect; std::vector channels; 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(bool grayscale, bool invert) const { int nc = channels.size(); @@ -465,6 +505,12 @@ public: { } }; + template static T parse(BinaryStreamReader& r) + { + T ret{}; + ret.read(r); + return ret; + } protected: static Type::Ref instanciate(const std::string& key); static std::map> m_ctor_table;