Skip to content
Snippets Groups Projects
Commit 142bc84d authored by Philip Trettner's avatar Philip Trettner
Browse files

introduced pos+pos = sum_of_pos

parent 9d41effb
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <typed-geometry/detail/macros.hh> #include <typed-geometry/detail/macros.hh>
#include <typed-geometry/detail/scalar_traits.hh> #include <typed-geometry/detail/scalar_traits.hh>
#include <typed-geometry/detail/sum_of_pos.hh>
#include <typed-geometry/types/dir.hh> #include <typed-geometry/types/dir.hh>
#include <typed-geometry/types/pos.hh> #include <typed-geometry/types/pos.hh>
#include <typed-geometry/types/size.hh> #include <typed-geometry/types/size.hh>
...@@ -35,11 +36,13 @@ TG_IMPL_DEFINE_BINARY_OP_SCALAR(pos, +); ...@@ -35,11 +36,13 @@ TG_IMPL_DEFINE_BINARY_OP_SCALAR(pos, +);
TG_IMPL_DEFINE_BINARY_OP_SCALAR(pos, *); TG_IMPL_DEFINE_BINARY_OP_SCALAR(pos, *);
TG_IMPL_DEFINE_BINARY_OP_SCALAR_DIV(pos); TG_IMPL_DEFINE_BINARY_OP_SCALAR_DIV(pos);
// deprecated / not supported operations // special pos+pos handling
template <int D, class ScalarT> template <int D, class ScalarT>
[[deprecated("pos + pos is not a proper operation. did you mean pos + vec?")]] pos<D, ScalarT> operator+(pos<D, ScalarT> const& a, pos<D, ScalarT> const& b) TG_NODISCARD constexpr sum_of_pos<D, ScalarT> operator+(pos<D, ScalarT> const& a, pos<D, ScalarT> const& b)
{ {
return a + vec(b); return sum_of_pos(a) + b;
} }
// deprecated / not supported operations
} // namespace tg } // namespace tg
#pragma once
#include <typed-geometry/types/pos.hh>
#include <typed-geometry/types/vec.hh>
namespace tg
{
/*
* sum_of_pos is a helper struct for guaranteeing syntactic soundness when dealing with sums of positions
* it is the result of operator+(pos, pos)
* it can only be added onto other sum_of_pos
* to get back to a pos one needs to use operator/(sum_of_pos, ScalarT)
*/
template <int D, class ScalarT>
struct sum_of_pos
{
using scalar_t = ScalarT;
using div_t = decltype(ScalarT() / 0.0f);
using pos_t = pos<D, ScalarT>;
constexpr sum_of_pos() = default;
/* implicit */ constexpr sum_of_pos(pos_t const& p) : accum(p) {}
constexpr pos<D, div_t> operator/(div_t const& rhs) const { return accum / rhs; }
constexpr sum_of_pos operator+(sum_of_pos const& rhs) const { return accum + vec(rhs); }
constexpr sum_of_pos operator+(pos_t const& rhs) const { return accum + vec(rhs); }
private:
pos_t accum;
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment