[ox/std] Add conversion functions for geo types, cleanup

This commit is contained in:
Gary Talent 2024-05-25 21:29:54 -05:00
parent 407e54246f
commit dc20c66797
8 changed files with 108 additions and 16 deletions

View File

@ -96,6 +96,7 @@ install(
buildinfo.hpp buildinfo.hpp
byteswap.hpp byteswap.hpp
concepts.hpp concepts.hpp
conv.hpp
def.hpp def.hpp
defer.hpp defer.hpp
defines.hpp defines.hpp

View File

@ -32,7 +32,7 @@ constexpr To bit_cast(const From &src) noexcept requires(sizeof(To) == sizeof(Fr
namespace ox { namespace ox {
template<typename To, typename From> template<typename To, typename From>
constexpr typename enable_if<sizeof(To) == sizeof(From), To>::type cbit_cast(From src) noexcept { constexpr To cbit_cast(From src) noexcept requires(sizeof(To) == sizeof(From)) {
To dst = {}; To dst = {};
ox::memcpy(&dst, &src, sizeof(src)); ox::memcpy(&dst, &src, sizeof(src));
return dst; return dst;

45
deps/ox/src/ox/std/conv.hpp vendored Normal file
View File

@ -0,0 +1,45 @@
/*
* Copyright 2015 - 2024 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "point.hpp"
#include "size.hpp"
#include "vec.hpp"
namespace ox {
constexpr Vec2::operator Point() const noexcept {
return {
static_cast<int32_t>(x),
static_cast<int32_t>(y),
};
}
constexpr Vec2::operator Size() const noexcept {
return {
static_cast<int32_t>(x),
static_cast<int32_t>(y),
};
}
constexpr Point::operator Vec2() const noexcept {
return {
static_cast<float>(x),
static_cast<float>(y),
};
}
constexpr Size::operator Vec2() const noexcept {
return {
static_cast<float>(width),
static_cast<float>(height),
};
}
}

View File

@ -64,6 +64,7 @@ class Point {
constexpr bool operator!=(const Point&) const noexcept; constexpr bool operator!=(const Point&) const noexcept;
explicit constexpr operator class Vec2() const noexcept;
}; };
constexpr Point::Point(int x, int y) noexcept { constexpr Point::Point(int x, int y) noexcept {

View File

@ -64,6 +64,7 @@ class Size {
constexpr bool operator!=(const Size&) const noexcept; constexpr bool operator!=(const Size&) const noexcept;
explicit constexpr operator class Vec2() const noexcept;
}; };
constexpr Size::Size(int width, int height) noexcept { constexpr Size::Size(int width, int height) noexcept {

View File

@ -16,6 +16,7 @@
#include "istring.hpp" #include "istring.hpp"
#include "byteswap.hpp" #include "byteswap.hpp"
#include "concepts.hpp" #include "concepts.hpp"
#include "conv.hpp"
#include "cstringview.hpp" #include "cstringview.hpp"
#include "cstrops.hpp" #include "cstrops.hpp"
#include "def.hpp" #include "def.hpp"

View File

@ -185,6 +185,8 @@ struct enable_if<true, T> {
using type = T; using type = T;
}; };
template<bool B, typename T>
using enable_if_t = typename enable_if<B, T>::type;
template<typename T> template<typename T>
struct is_pointer { struct is_pointer {

View File

@ -23,17 +23,16 @@
namespace ox { namespace ox {
template<typename T> class Vec2 {
struct Vec {
public: public:
using value_type = T; using value_type = float;
using size_type = std::size_t; using size_type = std::size_t;
static constexpr auto TypeName = "net.drinkingtea.ox.Point"; static constexpr auto TypeName = "net.drinkingtea.ox.Point";
static constexpr auto TypeVersion = 1; static constexpr auto TypeVersion = 1;
T x = 0; float x = 0;
T y = 0; float y = 0;
template<typename RefType = value_type&, typename PtrType = value_type*, bool reverse = false> template<typename RefType = value_type&, typename PtrType = value_type*, bool reverse = false>
struct iterator: public ox::Iterator<std::bidirectional_iterator_tag, value_type> { struct iterator: public ox::Iterator<std::bidirectional_iterator_tag, value_type> {
@ -141,14 +140,14 @@ struct Vec {
}; };
constexpr Vec() noexcept = default; constexpr Vec2() noexcept = default;
template<typename ...Args> template<typename ...Args>
constexpr Vec(T pX, T pY) noexcept: x(pX), y(pY) { constexpr Vec2(float pX, float pY) noexcept: x(pX), y(pY) {
} }
#if __has_include(<imgui.h>) #if __has_include(<imgui.h>)
explicit constexpr Vec(const ImVec2 &v) noexcept: Vec(v.x, v.y) { explicit constexpr Vec2(const ImVec2 &v) noexcept: Vec2(v.x, v.y) {
} }
explicit inline operator ImVec2() const noexcept { explicit inline operator ImVec2() const noexcept {
@ -228,7 +227,7 @@ struct Vec {
} }
} }
constexpr auto operator==(const Vec &v) const noexcept { constexpr auto operator==(const Vec2 &v) const noexcept {
for (auto i = 0u; i < v.size(); ++i) { for (auto i = 0u; i < v.size(); ++i) {
if ((*this)[i] != v[i]) { if ((*this)[i] != v[i]) {
return false; return false;
@ -237,29 +236,71 @@ struct Vec {
return true; return true;
} }
constexpr auto operator!=(const Vec &v) const noexcept { constexpr auto operator!=(const Vec2 &v) const noexcept {
return !operator==(v); return !operator==(v);
} }
explicit constexpr operator class Point() const noexcept;
explicit constexpr operator class Size() const noexcept;
[[nodiscard]] [[nodiscard]]
constexpr std::size_t size() const noexcept { constexpr std::size_t size() const noexcept {
return 2; return 2;
} }
constexpr Vec2 operator+(float i) const noexcept {
return {x + i, y + i};
}
constexpr Vec2 operator+=(float i) noexcept {
x += i;
y += i;
return *this;
}
constexpr Vec2 operator-(float i) const noexcept {
return {x - i, y - i};
}
constexpr Vec2 operator-=(float i) noexcept {
x -= i;
y -= i;
return *this;
}
constexpr Vec2 operator*(float i) const noexcept {
return {x * i, y * i};
}
constexpr Vec2 operator*=(float i) noexcept {
x *= i;
y *= i;
return *this;
}
constexpr Vec2 operator/(float i) const noexcept {
return {x / i, y / i};
}
constexpr Vec2 operator/=(float i) noexcept {
x /= i;
y /= i;
return *this;
}
protected: protected:
[[nodiscard]] [[nodiscard]]
constexpr T *start() noexcept { constexpr float *start() noexcept {
return &x; return&x;
} }
[[nodiscard]] [[nodiscard]]
constexpr const T *start() const noexcept { constexpr const float *start() const noexcept {
return &x; return &x;
} }
}; };
using Vec2 = Vec<float>;
template<typename T> template<typename T>
constexpr Error model(T *io, ox::CommonPtrWith<Vec2> auto *obj) noexcept { constexpr Error model(T *io, ox::CommonPtrWith<Vec2> auto *obj) noexcept {
oxReturnError(io->template setTypeInfo<Vec2>()); oxReturnError(io->template setTypeInfo<Vec2>());