Skip to content

Commit bd07ea7

Browse files
authored
Implemented template metod peel (#47)
* Implemented template metod peel * Typo negative
1 parent fe44bbd commit bd07ea7

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/wrapper/commit_wrapper.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ class commit_wrapper : public wrapper_base<git_commit>
2626
commit_wrapper(git_commit* commit);
2727

2828
friend class repository_wrapper;
29+
friend class reference_wrapper;
2930
};

src/wrapper/object_wrapper.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ class object_wrapper : public wrapper_base<git_object>
2424
object_wrapper(git_object* obj);
2525

2626
friend class repository_wrapper;
27+
friend class reference_wrapper;
2728
};

src/wrapper/refs_wrapper.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <concepts>
34
#include <string>
45

56
#include <git2.h>
@@ -20,9 +21,38 @@ class reference_wrapper : public wrapper_base<git_reference>
2021
std::string short_name() const;
2122
bool is_remote() const;
2223

24+
template <class W>
25+
W peel() const;
26+
2327
private:
2428

2529
reference_wrapper(git_reference* ref);
2630

2731
friend class repository_wrapper;
2832
};
33+
34+
class commit_wrapper;
35+
class object_wrapper;
36+
37+
// TODO: add constraints on W
38+
// For now it accepts commit_wrapper and object_wrapper only
39+
template <class W>
40+
W reference_wrapper::peel() const
41+
{
42+
constexpr git_object_t obj_type = []
43+
{
44+
if constexpr (std::same_as<W, commit_wrapper>)
45+
{
46+
return GIT_OBJECT_COMMIT;
47+
}
48+
else // Default case
49+
{
50+
return GIT_OBJECT_ANY;
51+
}
52+
}();
53+
54+
using resource_type = typename W::resource_type;
55+
git_object* resource = nullptr;
56+
throw_if_error(git_reference_peel(&resource, this->p_resource, obj_type));
57+
return W(reinterpret_cast<resource_type*>(resource));
58+
}

0 commit comments

Comments
 (0)