-
Notifications
You must be signed in to change notification settings - Fork 2
/
view.h
63 lines (53 loc) · 1.43 KB
/
view.h
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
#ifndef _XH_PREDICATE_H_
#define _XH_PREDICATE_H_
#include <functional>
#include "function_traits.h"
using namespace std;
namespace xh {
#define ITERATOR_TRAITS_DEF(iter, ptr_iter) \
template<class Container>\
struct iter##_traits{\
using type = typename Container::iter;\
};\
template<class T>\
struct iter##_traits<T*>{\
using type = ptr_iter;\
};\
template<class T, size_t N>\
struct iter##_traits<T[N]>:iter##_traits<T*>{};\
template<class T>\
using iter##_traits_t = typename iter##_traits<T>::type;
ITERATOR_TRAITS_DEF(iterator, T *)
ITERATOR_TRAITS_DEF(const_iterator, const T*)
#undef ITERATOR_TRIATS_DEF
template<class Container>
class view {
public:
using iterator = typename const_iterator_traits<Container>::type;
using container = Container;
protected:
iterator _begin;
iterator _end;
public:
view(Container &ctn) : _begin(std::begin(ctn)), _end(std::end(ctn)) {}
view(const iterator &begin_, const iterator &end_)
: _begin(begin_), _end(end_) {}
view(const view &v) {
memcpy(this, &v, sizeof(view<Container>));
}
view(view &&v) {
memcpy(this, &v, sizeof(view<Container>));
}
view &operator=(const view &v) {
memcpy(this, &v, sizeof(view<Container>));
return *this;
}
view &operator=(view &&v) {
memcpy(this, &v, sizeof(view<Container>));
return *this;
}
iterator begin() const { return _begin; }
iterator end() const { return _end; }
};
};
#endif //_XH_PREDICATE_H_