1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once


#include <concepts><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utility>


#include "internal/dummy.hpp"

#include "algebraic/opposite.hpp"
#include "algebraic/internal/concepts.hpp"


namespace uni {

namespace algebraic {


template<internal::magma M>
struct opposite
  :
    std::conditional_t<internal::associative<M>, associative, uni::internal::dummy>,
    std::conditional_t<internal::commutative<M>, commutative, uni::internal::dummy>
{
    using value_type = M::value_type;

  private:
    M _value;

  public:
    template<std::convertible_to<M> T>
    opposite(const T& v) : _value(v) {};


    template<class... Args>
        requires std::constructible_from<value_type, Args...>
    opposite(Args&&... args) noexcept(NO_EXCEPT) : _value(std::forward<Args>(args)...) {}


    inline explicit operator value_type() const noexcept(NO_EXCEPT) { return this->_value.val(); }
    inline auto val() const noexcept(NO_EXCEPT) { return this->_value.val(); };

    inline const value_type* operator->() const noexcept(NO_EXCEPT) { return this->_value.operator->(); };
    inline value_type* operator->() noexcept(NO_EXCEPT) { return this->_value.operator->(); };


    friend inline auto operator<=>(const opposite& lhs, const opposite& rhs) noexcept(NO_EXCEPT) {
        return lhs._value <=> rhs._value;
    };

    friend inline bool operator==(const opposite& lhs, const opposite& rhs) noexcept(NO_EXCEPT) {
        return lhs._value == rhs._value;
    }


    friend inline opposite operator+(const opposite& lhs, const opposite& rhs) noexcept(NO_EXCEPT) {
        return rhs._value + lhs._value;
    }


    friend inline opposite operator-(const opposite& val) noexcept(NO_EXCEPT)
        requires internal::invertible<M>
    {
        return -val._value;
    }
};



template<internal::magma M>
struct make_opposite;


template<internal::magma M>
struct make_opposite<opposite<M>> {
    using type = M;
};

template<internal::magma M>
    requires internal::commutative<M>
struct make_opposite<M> {
    using type = M;
};


template<internal::magma M>
    requires (!internal::commutative<M>)
struct make_opposite<M> {
    using type = opposite<M>;
};


template<internal::magma M>
using make_opposite_t = make_opposite<M>::type;


} // namespace algebraic

} // namespace uni