summaryrefslogtreecommitdiff
path: root/src/range/slice.hpp
blob: da52e5ff04404d648c8186c277a05e463c78af2b (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
#pragma once
//    slice.hpp - a borrowed array
//
//    Copyright © 2011-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 <cstddef>

#include <type_traits>
#include <vector>


namespace tmwa
{
template<class T>
class Slice
{
    T *_begin;
    T *_end;
public:
    class iterator;

    Slice(std::nullptr_t);
    Slice(T *b, T *e);
    Slice(T *b, size_t l);
    template<class U, typename=typename std::enable_if<sizeof(T) == sizeof(U) && std::is_base_of<T, U>::value>::type>
    Slice(Slice<U> o);
    template<size_t n, typename=typename std::enable_if<sizeof(T) != 1>::type>
    Slice(T (&arr)[n]);
    // TODO: come up with something else once using ranges (wrap all containers?)
    Slice(std::vector<T>& vec);

    iterator begin() const;
    iterator end() const;
    T *data() const;
    size_t size() const;
    operator bool() const;
    bool operator not() const;
    T& front() const;
    T& back() const;
    T& pop_front();
    T& pop_back();
    __attribute__((deprecated("use iterators instead")))
    T& operator[](size_t o);

    Slice slice_t(size_t o) const;
    Slice slice_h(size_t o) const;
    Slice rslice_t(size_t no) const;
    Slice rslice_h(size_t no) const;
    Slice islice_t(iterator it) const;
    Slice islice_h(iterator it) const;
    Slice lslice(size_t o, size_t l) const;
    Slice pslice(size_t b, size_t e) const;
    Slice islice(iterator b, iterator e) const;
};
} // namespace tmwa

#include "slice.tcc"