Fenix @develop
 
Loading...
Searching...
No Matches
data_ref.hpp
1#ifndef FENIX_DATA_UTIL_DATA_REF_HPP
2#define FENIX_DATA_UTIL_DATA_REF_HPP
3
4#include "fenix/data/util/buffer.hpp"
5
6#include <limits>
7
8namespace fenix::data::util {
9
10namespace detail {
11
12template <bool IsConst>
14 public:
15 using DataT = std::conditional_t<IsConst, const char, char>;
16 using DataBufT = std::conditional_t<IsConst, const DataBuffer, DataBuffer>;
17
19 DataRefHolder(DataT* b, size_t len) : raw_buf(b), raw_buf_size(len) {}
20 DataRefHolder(DataBufT& db) : data_buf(&db) {}
21
22 template <bool OtherConst>
24 *this = o;
25 }
26 template <bool OtherConst>
28 static_assert(IsConst || !OtherConst);
29 raw_buf = o.raw_buf;
30 raw_buf_size = o.raw_buf_size;
31 data_buf = o.data_buf;
32 return *this;
33 }
34
35 DataT* data() const { return data_buf ? data_buf->data() : raw_buf; }
36
37 size_t size() const { return data_buf ? data_buf->size() : raw_buf_size; }
38
39 protected:
41
42 DataBufT* data_buf = nullptr;
43
44 DataT* raw_buf = nullptr;
45 size_t raw_buf_size = 0;
46};
47
48} //namespace detail
49
50class DataRef {
51 public:
52 DataRef(char* b, size_t len) : ref(b, len) {}
53 DataRef(char* b) : ref(b, std::numeric_limits<size_t>::max()) {}
54 DataRef() : DataRef(nullptr) {}
55
56 DataRef(DataBuffer& db) : ref(db) {}
57
58 template <typename T, typename... U>
59 DataRef(std::vector<T, U...>& v)
60 : DataRef((char*)v.data(), v.size() * sizeof(T)) {}
61
62 DataRef(const DataRef& dr) : ref(dr.ref) {}
63 DataRef& operator=(const DataRef& dr) {
64 ref = dr.ref;
65 return *this;
66 }
67
68 char* data() const { return ref.data(); }
69 size_t size() const { return ref.size(); }
70
71 DataRef bounded(size_t max_size) {
72 if (max_size < size()) return DataRef(data(), max_size);
73 else return *this;
74 }
75
76 bool is_bounded() const {
77 return size() != std::numeric_limits<size_t>::max();
78 }
79
80 protected:
81 friend class ConstDataRef;
82
84};
85
87 public:
88 ConstDataRef(const char* b, size_t len) : ref(b, len) {}
89 ConstDataRef(const char* b) : ref(b, std::numeric_limits<size_t>::max()) {}
90 ConstDataRef() : ConstDataRef(nullptr) {}
91
92 ConstDataRef(const DataBuffer& db) : ref(db) {}
93
94 template <typename T, typename... U>
95 ConstDataRef(const std::vector<T, U...>& v)
96 : ref(v.data(), v.size() * sizeof(T)) {}
97
98 ConstDataRef(const DataRef& dr) : ref(dr.ref) {}
99 ConstDataRef& operator=(const DataRef& dr) {
100 ref = dr.ref;
101 return *this;
102 }
103
104 ConstDataRef(const ConstDataRef& dr) : ref(dr.ref) {};
105 ConstDataRef& operator=(const ConstDataRef& dr) {
106 ref = dr.ref;
107 return *this;
108 }
109
110 const char* data() const { return ref.data(); }
111 size_t size() const { return ref.size(); }
112
113 ConstDataRef bounded(size_t max_size) const {
114 if (max_size < size()) return ConstDataRef(data(), max_size);
115 else return *this;
116 }
117
118 bool is_bounded() const {
119 return size() != std::numeric_limits<size_t>::max();
120 }
121
122 private:
124};
125
126} //namespace fenix::data::util
127
128#endif
Definition data_ref.hpp:86
Definition buffer.hpp:68
Definition data_ref.hpp:50