summaryrefslogtreecommitdiff
path: root/src/common/const_array.hpp
blob: 93ae337d6441902e11986638c0ff9a6138fbe48e (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef CONST_ARRAY_HPP
#define CONST_ARRAY_HPP
//    const_array.hpp - just a pointer-to-const and a length
//
//    Copyright © 2011-2012 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 "sanity.hpp"

#include <cstring>

#include <iterator>
#include <ostream>
#include <string>
#include <vector>

#ifdef WORKAROUND_GCC46_COMPILER
// constexpr is buggy with templates in this version
# define constexpr /* nothing */
#endif

// TODO see if I ever actually use this, and not the subclass
template<class T>
class const_array
{
    const T *d;
    size_t n;
public:
    typedef const T *iterator;
    typedef std::reverse_iterator<iterator> reverse_iterator;

    constexpr
    const_array(std::nullptr_t)
    : d(nullptr), n(0)
    {}

    constexpr
    const_array(const T *p, size_t z)
    : d(p), n(z)
    {}

    constexpr
    const_array(const T *b, const T *e)
    : d(b), n(e - b)
    {}

    const_array(std::initializer_list<T> list)
    : d(list.begin()), n(list.size())
    {}

    // Implicit conversion from std::vector
    const_array(const std::vector<T>& v)
    : d(v.data()), n(v.size())
    {}

    // but disallow conversion from a temporary
    const_array(std::vector<T>&&) = delete;

    // All methods are non-const to "encourage" you
    // to always pass a const_array by value.
    // After all, "const const_array" looks funny.
    constexpr
    const T *data() { return d; }
    constexpr
    size_t size() { return n; }
    constexpr
    bool empty() { return not n; }
    constexpr explicit
    operator bool() { return n; }

    constexpr
    std::pair<const_array, const_array> cut(size_t o)
    {
        return {const_array(d, o), const_array(d + o, n - o)};
    }

    constexpr
    const_array first(size_t o)
    {
        return cut(o).first;
    }

    constexpr
    const_array last(size_t l)
    {
        return cut(size() - l).second;
    }

    constexpr
    const_array after(size_t o)
    {
        return cut(o).second;
    }

    constexpr
    iterator begin() { return d; }
    constexpr
    iterator end() { return d + n; }
    constexpr
    reverse_iterator rbegin() { return reverse_iterator(end()); }
    constexpr
    reverse_iterator rend() { return reverse_iterator(begin()); }

    constexpr
    const T& front() { return *begin(); }
    constexpr
    const T& back() { return *rbegin(); }
};

// subclass just provides a simpler name and some conversions
class const_string : public const_array<char>
{
public:
    // Implicit conversion from C string.
    constexpr
    const_string(const char *z)
    : const_array<char>(z, z ? strlen(z) : 0)
    {}

    // Same as parent constructor.
    constexpr
    const_string(const char *s, size_t l)
    : const_array<char>(s, l)
    {}

    // Same as parent constructor.
    constexpr
    const_string(const char *b, const char *e)
    : const_array<char>(b, e)
    {}

    // Same as parent constructor.
    const_string(const std::vector<char> s)
    : const_array<char>(s)
    {}

    // Implicit conversion from C++ string.
    const_string(const std::string& s)
    : const_array<char>(s.data(), s.size())
    {}

    // but disallow converion from a temporary.
    const_string(std::string&&) = delete;

    // allow being sloppy
    constexpr
    const_string(const_array<char> a)
    : const_array<char>(a)
    {}
};
#ifdef WORKAROUND_GCC46_COMPILER
# undef constexpr
#endif

inline
std::ostream& operator << (std::ostream& o, const_string s)
{
    return o.write(s.data(), s.size());
}

#endif // CONST_ARRAY_HPP