Skip to content

Commit 45617f2

Browse files
authored
Add git_strarray wrapper (#17)
* add git_strarray wrapper * move implementation to cpp file * small fix * address review comments
1 parent 7ba5a9c commit 45617f2

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/utils/common.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,51 @@ std::string get_current_git_path()
2323
// sub->add_option("directory", directory, "info about directory arg")
2424
// ->check(CLI::ExistingDirectory | CLI::NonexistentPath)
2525
// ->default_val(std::filesystem::current_path());
26+
27+
git_strarray_wrapper::git_strarray_wrapper(std::vector<std::string> patterns)
28+
: m_patterns(std::move(patterns))
29+
{
30+
init_str_array();
31+
}
32+
33+
git_strarray_wrapper::git_strarray_wrapper(git_strarray_wrapper&& rhs)
34+
: m_patterns(std::move(rhs.m_patterns))
35+
{
36+
init_str_array();
37+
rhs.reset_str_array();
38+
}
39+
40+
git_strarray_wrapper& git_strarray_wrapper::operator=(git_strarray_wrapper&& rhs)
41+
{
42+
using std::swap;
43+
swap(m_patterns, rhs.m_patterns);
44+
swap(m_array.strings, rhs.m_array.strings);
45+
swap(m_array.count, rhs.m_array.count);
46+
return *this;
47+
}
48+
49+
git_strarray_wrapper::~git_strarray_wrapper()
50+
{
51+
reset_str_array();
52+
}
53+
54+
git_strarray_wrapper::operator git_strarray*()
55+
{
56+
return &m_array;
57+
}
58+
59+
void git_strarray_wrapper::reset_str_array()
60+
{
61+
delete[] m_array.strings;
62+
m_array={nullptr, 0};
63+
}
64+
65+
void git_strarray_wrapper::init_str_array()
66+
{
67+
m_array.strings = new char*[m_patterns.size()];
68+
m_array.count = m_patterns.size();
69+
for (size_t i=0; i<m_patterns.size(); ++i)
70+
{
71+
m_array.strings[i] = const_cast<char*>(m_patterns[i].c_str());
72+
}
73+
}

src/utils/common.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#include <string>
44
#include <utility>
5+
#include <vector>
6+
7+
#include <git2.h>
58

69
class noncopyable_nonmovable
710
{
@@ -57,3 +60,30 @@ class libgit2_object : private noncopyable_nonmovable
5760
};
5861

5962
std::string get_current_git_path();
63+
64+
class git_strarray_wrapper
65+
{
66+
public:
67+
git_strarray_wrapper()
68+
: m_patterns{}
69+
, m_array{nullptr, 0}
70+
{}
71+
git_strarray_wrapper(std::vector<std::string> patterns);
72+
73+
git_strarray_wrapper(const git_strarray_wrapper&) = delete;
74+
git_strarray_wrapper& operator=(const git_strarray_wrapper&) = delete;
75+
76+
git_strarray_wrapper(git_strarray_wrapper&& rhs);
77+
git_strarray_wrapper& operator=(git_strarray_wrapper&&);
78+
79+
~git_strarray_wrapper();
80+
81+
operator git_strarray*();
82+
83+
private:
84+
std::vector<std::string> m_patterns;
85+
git_strarray m_array;
86+
87+
void reset_str_array();
88+
void init_str_array();
89+
};

0 commit comments

Comments
 (0)