|
| 1 | +#include "strarray_wrapper.hpp" |
| 2 | + |
| 3 | +strarray_owned_wrapper::strarray_owned_wrapper() |
| 4 | + : m_array{nullptr, 0} |
| 5 | +{ |
| 6 | +} |
| 7 | + |
| 8 | +strarray_owned_wrapper::strarray_owned_wrapper(git_strarray&& arr) |
| 9 | + : m_array(std::move(arr)) |
| 10 | +{ |
| 11 | +} |
| 12 | + |
| 13 | +strarray_owned_wrapper::strarray_owned_wrapper(strarray_owned_wrapper&& rhs) |
| 14 | + : m_array(std::move(rhs.m_array)) |
| 15 | +{ |
| 16 | + rhs.m_array = git_strarray{nullptr, 0}; |
| 17 | +} |
| 18 | + |
| 19 | +strarray_owned_wrapper& strarray_owned_wrapper::operator=(strarray_owned_wrapper&& rhs) |
| 20 | +{ |
| 21 | + std::swap(m_array.strings, rhs.m_array.strings); |
| 22 | + std::swap(m_array.count, rhs.m_array.count); |
| 23 | + return *this; |
| 24 | +} |
| 25 | + |
| 26 | +strarray_owned_wrapper::~strarray_owned_wrapper() |
| 27 | +{ |
| 28 | + git_strarray_dispose(&m_array); |
| 29 | +} |
| 30 | + |
| 31 | +strarray_owned_wrapper::operator git_strarray*() |
| 32 | +{ |
| 33 | + return &m_array; |
| 34 | +} |
| 35 | + |
| 36 | +size_t strarray_owned_wrapper::size() const |
| 37 | +{ |
| 38 | + return m_array.count; |
| 39 | +} |
| 40 | + |
| 41 | +std::string_view strarray_owned_wrapper::operator[](size_t i) const |
| 42 | +{ |
| 43 | + return { m_array.strings[i] }; |
| 44 | +} |
| 45 | + |
| 46 | +strarray_view_wrapper::strarray_view_wrapper(std::vector<std::string> patterns) |
| 47 | + : m_patterns(std::move(patterns)) |
| 48 | +{ |
| 49 | + init_str_array(); |
| 50 | +} |
| 51 | + |
| 52 | +strarray_view_wrapper::strarray_view_wrapper(strarray_view_wrapper&& rhs) |
| 53 | + : m_patterns(std::move(rhs.m_patterns)) |
| 54 | +{ |
| 55 | + init_str_array(); |
| 56 | + rhs.reset_str_array(); |
| 57 | +} |
| 58 | + |
| 59 | +strarray_view_wrapper& strarray_view_wrapper::operator=(strarray_view_wrapper&& rhs) |
| 60 | +{ |
| 61 | + using std::swap; |
| 62 | + swap(m_patterns, rhs.m_patterns); |
| 63 | + swap(m_array.strings, rhs.m_array.strings); |
| 64 | + swap(m_array.count, rhs.m_array.count); |
| 65 | + return *this; |
| 66 | +} |
| 67 | + |
| 68 | +strarray_view_wrapper::~strarray_view_wrapper() |
| 69 | +{ |
| 70 | + reset_str_array(); |
| 71 | +} |
| 72 | + |
| 73 | +strarray_view_wrapper::operator git_strarray*() |
| 74 | +{ |
| 75 | + return &m_array; |
| 76 | +} |
| 77 | + |
| 78 | +void strarray_view_wrapper::reset_str_array() |
| 79 | +{ |
| 80 | + delete[] m_array.strings; |
| 81 | + m_array = {nullptr, 0}; |
| 82 | +} |
| 83 | + |
| 84 | +void strarray_view_wrapper::init_str_array() |
| 85 | +{ |
| 86 | + m_array.strings = new char*[m_patterns.size()]; |
| 87 | + m_array.count = m_patterns.size(); |
| 88 | + for (size_t i = 0; i < m_patterns.size(); ++i) |
| 89 | + { |
| 90 | + m_array.strings[i] = const_cast<char*>(m_patterns[i].c_str()); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +size_t strarray_view_wrapper::size() const |
| 95 | +{ |
| 96 | + return m_patterns.size(); |
| 97 | +} |
| 98 | + |
| 99 | + |
0 commit comments