summaryrefslogtreecommitdiff
path: root/src/lib/eina_cxx/eina_accessor.hh
blob: b389b31bf7fa5535210881275a76825f139ebaaa (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#ifndef EINA_ACCESSOR_HH_
#define EINA_ACCESSOR_HH_

#include <Eina.h>
#include <eina_error.hh>

#include <memory>
#include <iterator>
#include <cstdlib>
#include <cassert>

namespace efl { namespace eina {

template <typename T>
struct accessor
{
  typedef unsigned int key_type;
  typedef T mapped_type;
  typedef T value_type;
  typedef std::size_t size_type;

  accessor() : _impl(0) {}
  explicit accessor(Eina_Accessor* impl)
    : _impl(impl)
  {
    assert(_impl != 0);
  }
  accessor(accessor<T> const& other)
    : _impl(eina_accessor_clone(other._impl))
  {}
  accessor<T>& operator=(accessor<T> const& other)
  {
    eina_accessor_free(_impl);
    _impl = eina_accessor_clone(other._impl);
    if(!_impl)
      throw eina::system_error(efl::eina::get_error_code(), "Error cloning accessor");
    return *this;
  }
  ~accessor()
  {
    eina_accessor_free(_impl);
  }
  
  mapped_type& operator[](size_type i) const
  {
    assert(_impl != 0);
    void* p;
    if(!eina_accessor_data_get(_impl, i, &p))
      {
        eina::error_code ec = efl::eina::get_error_code();
        throw eina::system_error(ec, "EFL Eina Error");
      }
    return *static_cast<mapped_type*>(p);
  }

  Eina_Accessor* native_handle() const;

  void swap(accessor<T>& other)
  {
    std::swap(_impl, other._impl);
  }
private:
  typedef Eina_Accessor*(accessor<T>::*unspecified_bool_type)() const;
public:
  operator unspecified_bool_type() const
  {
    return native_handle() ? &accessor<T>::native_handle : 0 ;
  }
private:
  Eina_Accessor* _impl;
};

template <typename U>
void swap(accessor<U>& lhs, accessor<U>& rhs)
{
  lhs.swap(rhs);
}

template <typename T>
struct accessor_iterator
{
  typedef T value_type;
  typedef value_type* pointer;
  typedef value_type& reference;
  typedef std::ptrdiff_t difference_type;
  typedef std::random_access_iterator_tag iterator_category;

  accessor_iterator(accessor<T> const& a, unsigned int pos = 0u)
    : _accessor(a), _index(pos)
  {}

  accessor_iterator<T>& operator+=(difference_type i)
  {
    _index += i;
    return *this;
  }
  accessor_iterator<T>& operator-=(difference_type i)
  {
    _index -= i;
    return *this;
  }
  value_type& operator[](difference_type i)
  {
    return _accessor[_index + i];
  }
  accessor_iterator<T>& operator++()
  {
    ++_index;
    return *this;
  }
  accessor_iterator<T>& operator--()
  {
    --_index;
    return *this;
  }
  accessor_iterator<T>& operator++(int)
  {
    accessor_iterator<T> tmp(*this);
    ++*this;
    return tmp;
  }
  accessor_iterator<T>& operator--(int)
  {
    accessor_iterator<T> tmp(*this);
    --*this;
    return tmp;
  }
  value_type& operator*() const
  {
    return _accessor[_index];
  }
  pointer operator->() const
  {
    return &**this;
  }
  void swap(accessor_iterator<T>& other)
  {
    std::swap(_index, other._index);
    std::swap(_accessor, other._accessor);
  }
private:
  accessor<T> _accessor;
  unsigned int _index;

  template <typename U>
  friend bool operator==(accessor_iterator<U> const& lhs, accessor_iterator<U> const& rhs)
  {
    return lhs._index == rhs._index;
  }

  template <typename U>
  friend typename accessor_iterator<U>::difference_type
  operator-(accessor_iterator<U> const& lhs, accessor_iterator<U> const& rhs)
  {
    return lhs._index - rhs._index;
  }

  template <typename U>
  friend
  accessor_iterator<U> operator+(accessor_iterator<U> lhs
                                 , typename accessor_iterator<U>::difference_type rhs)
  {
    lhs._index += rhs;
    return lhs;
  }

  template <typename U>
  friend
  accessor_iterator<U> operator+(typename accessor_iterator<U>::difference_type lhs
                               , accessor_iterator<U> rhs)
  {
    return rhs + lhs;
  }

  template <typename U>
  friend bool operator<(accessor_iterator<U> const& lhs, accessor_iterator<U> const& rhs)
  {
    return lhs._index < rhs._index;
  }

  template <typename U>
  friend bool operator<=(accessor_iterator<U> const& lhs, accessor_iterator<U> const& rhs)
  {
    return lhs._index <= rhs._index;
  }
};

template <typename U>
bool operator>=(accessor_iterator<U> const& lhs, accessor_iterator<U> const& rhs)
{
  return !(lhs < rhs);
}

template <typename U>
bool operator>(accessor_iterator<U> const& lhs, accessor_iterator<U> const& rhs)
{
  return !(lhs <= rhs);
}

template <typename U>
bool operator!=(accessor_iterator<U> const& lhs, accessor_iterator<U> const& rhs)
{
  return !(lhs == rhs);
}

template <typename U>
void swap(accessor_iterator<U>& lhs, accessor_iterator<U>& rhs)
{
  lhs.swap(rhs);
}

} }

#endif