diff -r 6988973515d2 -r ca23936b455b src/main.h --- a/src/main.h Wed May 25 20:36:34 2022 +0300 +++ b/src/main.h Mon Jun 06 22:01:22 2022 +0300 @@ -34,78 +34,6 @@ constexpr char mainwindow[] = "mainwindow"; } -namespace ldraw -{ - class Object; - - // Uniquely identifies a model body object - template - struct Id - { - std::int32_t value; - template - static constexpr bool is_base_or_base_of = std::disjunction_v, std::is_base_of>; - template>> - constexpr bool operator<(ldraw::Id other) const - { - return this->value < other.value; - } - friend constexpr unsigned int qHash(ldraw::Id id) - { - return qHash(id.value); - } - // Allow comparing ids as long as they are related - template>> - friend bool operator==(ldraw::Id one, ldraw::Id other) - { - return one.value == other.value; - } - // Allow upcasting - template>> - constexpr operator Id() const - { - return Id{this->value}; - } - }; - - using id_t = Id; - using triangleid_t = Id; - using quadrilateralid_t = Id; - using edgeid_t = Id; - using conditionaledgeid_t = Id; - using subfileid_t = Id; - using commentid_t = Id; - using metacommandid_t = Id; - - constexpr struct NullId - { - template - constexpr operator Id() const - { - return Id{0}; - } - static constexpr decltype(ldraw::id_t::value) value = 0; - } NULL_ID = {}; - - template - inline bool operator==(Id one, decltype(NULL_ID)) - { - return one.value == 0; - } - - template - inline bool operator!=(Id one, decltype(NULL_ID)) - { - return one.value != 0; - } - - template - inline bool operator<(Id one, decltype(NULL_ID)) - { - return one.value < 0; - } -} - constexpr std::size_t operator""_z(const unsigned long long int x) { return static_cast(x); @@ -262,8 +190,6 @@ return qHash(value.value); } -using ModelId = TypeValue; - /** * Iterates a @c glm::mat */ @@ -279,8 +205,15 @@ } } -QDataStream& operator<<(QDataStream&, const glm::vec3&); -QDataStream& operator>>(QDataStream&, glm::vec3&); +inline QDataStream& operator<<(QDataStream& stream, const glm::vec3& vec) +{ + return stream << vec.x << vec.y << vec.z; +} + +inline QDataStream& operator>>(QDataStream& stream, glm::vec3& vec) +{ + return stream >> vec.x >> vec.y >> vec.z; +} template QDataStream& operator<<(QDataStream& stream, const glm::mat& mat) @@ -312,3 +245,27 @@ } return result; } + +template +std::optional pointerToOptional(const T* p) +{ + std::optional result; + if (p != nullptr) { + result = *p; + } + return result; +} + +template +void removeFromMap(std::map& map, T&& key) +{ + const auto it = map.find(key); + if (it != map.end()) { + map.erase(it); + } +} + +// some magic code from https://en.cppreference.com/w/cpp/utility/variant/visit +// for use with std::visit +template struct overloaded : Ts... { using Ts::operator()...; }; +template overloaded(Ts...) -> overloaded;