SlHelpers
Loading...
Searching...
No Matches
Object.h
1// SPDX-License-Identifier: GPL-2.0-only
2
3#pragma once
4
5#include <string>
6
7#include <git2.h>
8
9#include "../helpers/Unique.h"
10
11#include "Helpers.h"
12
13namespace SlGit {
14
15class Repo;
16
20class Object {
21public:
22 virtual ~Object() = default;
23
25 const git_oid *id() const noexcept { return git_object_id(object()); }
27 std::string idStr() const noexcept { return Helpers::oidToStr(*id()); }
28
30 git_object_t type() const noexcept { return git_object_type(object()); }
32 std::string typeStr() const noexcept { return git_object_type2string(type()); }
33
35 virtual git_object *object() const noexcept { return nullptr; }
36
38 const Repo &repo() const { return *m_repo; }
39
41 bool operator==(const Object &other) const noexcept {
42 if (object() == other.object())
43 return true;
44 if (!object() || !other.object())
45 return false;
46 return git_oid_equal(id(), other.id());
47 }
48
50 bool operator!=(const Object &other) const noexcept { return !(*this == other); }
51protected:
52 Object() = delete;
54 explicit Object(const Repo &repo) : m_repo(&repo) {}
55private:
56 const Repo *m_repo;
57};
58
64template<typename T>
65class TypedObject : public Object {
66 using Holder = SlHelpers::UniqueHolder<T>;
67public:
68 TypedObject() = delete;
70 git_object *object() const noexcept override {
71 return reinterpret_cast<git_object *>(typed());
72 }
73
75 operator T *() const noexcept { return typed(); }
76
77protected:
79 explicit TypedObject(const Repo &repo, T* typed) noexcept :
80 Object(repo), m_typed(typed) { }
81
83 T *typed() const noexcept { return m_typed.get(); }
84private:
85 Holder m_typed;
86};
87
88}
static std::string oidToStr(const git_oid &id)
Convert OID id to string.
Definition Helpers.h:20
Git Object class – base for Blob, Commit, Tag, Tree.
Definition Object.h:20
const Repo & repo() const
Get the Repo this Object lives in.
Definition Object.h:38
virtual git_object * object() const noexcept
Get the libgit2's git_object pointer of this Object.
Definition Object.h:35
std::string idStr() const noexcept
Get OID (SHA) of this Object – as a string.
Definition Object.h:27
bool operator!=(const Object &other) const noexcept
Compare two Objects (their SHAs).
Definition Object.h:50
Object(const Repo &repo)
Constuct a new Object.
Definition Object.h:54
git_object_t type() const noexcept
Get Type of this Object.
Definition Object.h:30
bool operator==(const Object &other) const noexcept
Compare two Objects (their SHAs).
Definition Object.h:41
std::string typeStr() const noexcept
Get Type of this Object – as a string.
Definition Object.h:32
const git_oid * id() const noexcept
Get OID (SHA) of this Object.
Definition Object.h:25
The most important Git class.
Definition Repo.h:45
git_object * object() const noexcept override
Get a pointer to the generic git_object.
Definition Object.h:70
TypedObject(const Repo &repo, T *typed) noexcept
Construct a new TypedObject.
Definition Object.h:79
T * typed() const noexcept
Get the stored pointer typed to one of libgit2's types.
Definition Object.h:83
Wrapper around std::unique_ptr for easier re-definition of free.
Definition Unique.h:41