summaryrefslogtreecommitdiff
path: root/src/compat/borrow.hpp
blob: 0ea6a269922fc9f6d90ef7651bfc0ee02868795e (plain) (blame)
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
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once
//    borrow.hpp - a non-null, unowned, pointer
//
//    Copyright © 2014 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "fwd.hpp"

#include <cstdlib>

#include <iterator>

#include "option.hpp"

// unit tests currention in option_test.cpp

namespace tmwa
{
    // TODO see if const-by-default is a thing
    template<class T>
    class Borrowed
    {
        T *stupid;
    public:
        Borrowed() = delete;
        explicit
        Borrowed(T *p) : stupid(p)
        {
            if (!p) abort();
        }

        T& operator *() const
        {
            return *stupid;
        }

        T *operator ->() const
        {
            return stupid;
        }

        template<class U>
        Borrowed<U> downcast_to() const
        {
            static_assert(std::is_base_of<T, U>::value, "base check");
            static_assert(!std::is_same<T, U>::value, "same check");
            return Borrowed<U>(static_cast<U *>(stupid));
        }

        template<class U>
        Borrowed<U> upcast_to() const
        {
            static_assert(std::is_base_of<U, T>::value, "base check");
            static_assert(!std::is_same<T, U>::value, "same check");
            return Borrowed<U>(stupid);
        }

        friend bool operator == (Borrowed l, Borrowed r)
        {
            return l.stupid == r.stupid;
        }
        friend bool operator != (Borrowed l, Borrowed r)
        {
            return !(l == r);
        }
    };

    namespace option
    {
        template<class T>
        class OptionRepr<Borrowed<T>>
        {
            T *stupider;
        public:
            void set_none() { stupider = nullptr; }
            void post_set_some() {}
            bool is_some() const { return stupider != nullptr; }
            Borrowed<T> *ptr() { return reinterpret_cast<Borrowed<T> *>(&stupider); }
            const Borrowed<T> *ptr() const { return reinterpret_cast<const Borrowed<T> *>(&stupider); }
        };
    }

    template<class T>
    using P = Borrowed<T>;

    template<class T>
    Borrowed<T> borrow(T& ref)
    {
        return Borrowed<T>(&ref);
    }

    template<class T>
    T *as_raw_pointer(Option<Borrowed<T>> ptr)
    {
        return &*TRY_UNWRAP(ptr, return nullptr);
    }
} // namespace tmwa