|
| 1 | +#include "composed_string_view.hxx" |
| 2 | + |
| 3 | +#include <experimental/string_view> |
| 4 | +#include <string> |
| 5 | +#include <memory> |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | + |
| 9 | +using namespace std; |
| 10 | +using namespace std::experimental; |
| 11 | + |
| 12 | +namespace csv |
| 13 | +{ |
| 14 | +template <typename CHAR_T, typename LEFT, typename RIGHT> |
| 15 | +struct composed_string_view |
| 16 | +{ |
| 17 | + LEFT m_left; |
| 18 | + RIGHT m_right; |
| 19 | + |
| 20 | + using this_t = composed_string_view<CHAR_T, LEFT, RIGHT>; |
| 21 | + |
| 22 | + template <typename CONT> struct const_iterator_base |
| 23 | + { |
| 24 | + const CONT* m_view; |
| 25 | + enum ACTIVE |
| 26 | + { |
| 27 | + LEFT_ACTIVE, |
| 28 | + RIGHT_ACTIVE |
| 29 | + }; |
| 30 | + |
| 31 | + ACTIVE m_active; |
| 32 | + |
| 33 | + union |
| 34 | + { |
| 35 | + typename LEFT::const_iterator m_subItLeft; |
| 36 | + typename RIGHT::const_iterator m_subItRight; |
| 37 | + }; |
| 38 | + |
| 39 | + const_iterator_base(const CONT* view, ACTIVE active, |
| 40 | + typename LEFT::const_iterator subItLeft, |
| 41 | + typename RIGHT::const_iterator subItRight) |
| 42 | + : m_view(view), m_active(active) |
| 43 | + { |
| 44 | + if (m_active == LEFT_ACTIVE) |
| 45 | + m_subItLeft = subItLeft; |
| 46 | + else |
| 47 | + m_subItRight = subItRight; |
| 48 | + } |
| 49 | + |
| 50 | + const_iterator_base(const const_iterator_base& other) |
| 51 | + : m_view(other.m_view), m_active(other.m_active) |
| 52 | + { |
| 53 | + if (m_active == LEFT_ACTIVE) |
| 54 | + m_subItLeft = other.m_subItLeft; |
| 55 | + else |
| 56 | + m_subItRight = other.m_subItRight; |
| 57 | + } |
| 58 | + |
| 59 | + const_iterator_base& operator=(const const_iterator_base& other) |
| 60 | + { |
| 61 | + m_view = other.m_view; |
| 62 | + m_active = other.m_active; |
| 63 | + |
| 64 | + if (m_active == LEFT_ACTIVE) |
| 65 | + m_subItLeft = other.m_subItLeft; |
| 66 | + else |
| 67 | + m_subItRight = other.m_subItRight; |
| 68 | + |
| 69 | + return *this; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + const_iterator_base& operator++() |
| 74 | + { |
| 75 | + if (m_active == LEFT_ACTIVE) |
| 76 | + { |
| 77 | + if (m_subItLeft == m_view->m_left.end()) |
| 78 | + { |
| 79 | + m_active = RIGHT_ACTIVE; |
| 80 | + m_subItRight = m_view->m_right.begin(); |
| 81 | + } |
| 82 | + else |
| 83 | + ++m_subItLeft; |
| 84 | + } |
| 85 | + else |
| 86 | + ++m_subItRight; |
| 87 | + |
| 88 | + return *this; |
| 89 | + } |
| 90 | + |
| 91 | + const_iterator_base operator++(int) |
| 92 | + { |
| 93 | + auto tmp = *this; |
| 94 | + operator++(); |
| 95 | + return tmp; |
| 96 | + } |
| 97 | + |
| 98 | + bool equalSubIt(const const_iterator_base& other) const |
| 99 | + { |
| 100 | + bool result = m_active == other.m_active && |
| 101 | + (m_active == LEFT_ACTIVE |
| 102 | + ? (m_subItLeft == other.m_subItLeft) |
| 103 | + : (m_subItRight == other.m_subItRight)); |
| 104 | + return result; |
| 105 | + } |
| 106 | + |
| 107 | + bool operator==(const const_iterator_base& other) const |
| 108 | + { |
| 109 | + bool result = m_view == other.m_view; |
| 110 | + result = result && equalSubIt(other); |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + bool operator!=(const const_iterator_base& other) const |
| 115 | + { |
| 116 | + return !(*this == other); |
| 117 | + } |
| 118 | + |
| 119 | + const CHAR_T& operator*() const |
| 120 | + { |
| 121 | + return m_active == LEFT_ACTIVE ? *m_subItLeft : *m_subItRight; |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + using const_iterator = const_iterator_base<this_t>; |
| 126 | + |
| 127 | + const_iterator begin() const |
| 128 | + { |
| 129 | + return const_iterator(this, const_iterator::LEFT_ACTIVE, m_left.begin(), |
| 130 | + m_right.end()); |
| 131 | + } |
| 132 | + |
| 133 | + const_iterator end() const |
| 134 | + { |
| 135 | + return const_iterator(this, const_iterator::RIGHT_ACTIVE, |
| 136 | + m_left.begin(), m_right.end()); |
| 137 | + } |
| 138 | + |
| 139 | + size_t size() const { return m_left.size() + m_right.size(); } |
| 140 | + |
| 141 | + operator std::string() const |
| 142 | + { |
| 143 | + std::string result(" ", size() + 1); |
| 144 | + auto it = std::copy(m_left.begin(), m_left.end(), result.begin()); |
| 145 | + std::copy(m_right.begin(), m_right.end(), it); |
| 146 | + return result; |
| 147 | + } |
| 148 | +}; |
| 149 | + |
| 150 | +} // namespace csv |
| 151 | + |
| 152 | +using namespace csv; |
| 153 | + |
| 154 | +int main(int argc, const char** argv) |
| 155 | +{ |
| 156 | + string_view one("Hallo "); |
| 157 | + string_view two("Welt"); |
| 158 | + string_view three(", wie"); |
| 159 | + |
| 160 | + using type_one = composed_string_view<char, string_view, string_view>; |
| 161 | + type_one view_one{one, two}; |
| 162 | + |
| 163 | + cout << "size: " << view_one.size() << endl; |
| 164 | + |
| 165 | + using type_two = composed_string_view<char, type_one, string_view>; |
| 166 | + type_two viewTwo{view_one, three}; |
| 167 | + |
| 168 | + cout << "size two: " << viewTwo.size() << endl; |
| 169 | + string test = viewTwo; |
| 170 | + cout << "Test: " << test << endl; |
| 171 | + |
| 172 | + cout << "Size *void: " << sizeof(void*) << endl; |
| 173 | + cout << "Size strView one: " << sizeof(one) << endl; |
| 174 | + cout << "Size one: " << sizeof(view_one) << endl; |
| 175 | + cout << "Size: " << sizeof(viewTwo) << endl; |
| 176 | + cout << "Size it_view: " << sizeof(string_view::const_iterator) << endl; |
| 177 | + cout << "Size it_one: " << sizeof(type_one::const_iterator) << endl; |
| 178 | + cout << "Size it_two: " << sizeof(type_two::const_iterator) << endl; |
| 179 | + |
| 180 | + return 0; |
| 181 | +} |
0 commit comments