#include "godot_variant.h" #include #include bool GodotVariant::operator<(const GodotVariant& rhs) const { if (value.index() != rhs.value.index()) { return value.index() < rhs.value.index(); } if (const int32_t* pval = std::get_if(&value)) { return *pval < std::get(rhs.value); } else if (const std::string* pval = std::get_if(&value)) { return *pval < std::get(rhs.value); } else if (const std::map* pval = std::get_if>(&value)) { return *pval < std::get>(rhs.value); } else if (const std::vector* pval = std::get_if>(&value)) { return *pval < std::get>(rhs.value); } throw std::invalid_argument("Incomparable variants."); } void GodotVariant::Serialize(std::basic_ostream& output) const { if (const int32_t* pval = std::get_if(&value)) { uint32_t typebits = 2; output.write(reinterpret_cast(&typebits), 4); output.write(reinterpret_cast(pval), 4); } else if (const std::string* pval = std::get_if(&value)) { uint32_t typebits = 4; output.write(reinterpret_cast(&typebits), 4); uint32_t lengthbits = pval->length(); output.write(reinterpret_cast(&lengthbits), 4); output.write(pval->data(), pval->length()); if (pval->length() % 4 != 0) { int padding = 4 - (pval->length() % 4); for (int i = 0; i < padding; i++) { output.put(0); } } } else if (const std::map* pval = std::get_if>(&value)) { uint32_t typebits = 18; output.write(reinterpret_cast(&typebits), 4); uint32_t lengthbits = pval->size() & 0x7fffffff; output.write(reinterpret_cast(&lengthbits), 4); for (const auto& mapping : *pval) { mapping.first.Serialize(output); mapping.second.Serialize(output); } } else if (const std::vector* pval = std::get_if>(&value)) { uint32_t typebits = 19; output.write(reinterpret_cast(&typebits), 4); uint32_t lengthbits = pval->size() & 0x7fffffff; output.write(reinterpret_cast(&lengthbits), 4); for (const auto& entry : *pval) { entry.Serialize(output); } } }