SlHelpers
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 
13 namespace SlGit {
14 
15 class Repo;
16 
20 class Object {
21 public:
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); }
51 protected:
52  Object() = delete;
54  explicit Object(const Repo &repo) : m_repo(&repo) {}
55 private:
56  const Repo *m_repo;
57 };
58 
64 template<typename T>
65 class TypedObject : public Object {
67 public:
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 
77 protected:
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(); }
84 private:
85  Holder m_typed;
86 };
87 
88 }
Definition: Blob.h:11
Wrapper around std::unique_ptr for easier re-definition of free.
Definition: Unique.h:41
git_object_t type() const noexcept
Get Type of this Object.
Definition: Object.h:30
Git Object class – base for Blob, Commit, Tag, Tree.
Definition: Object.h:65
git_object * object() const noexcept override
Get a pointer to the generic git_object.
Definition: Object.h:70
bool operator==(const Object &other) const noexcept
Compare two Objects (their SHAs)
Definition: Object.h:41
const git_oid * id() const noexcept
Get OID (SHA) of this Object.
Definition: Object.h:25
static std::string oidToStr(const git_oid &id)
Convert OID id to string.
Definition: Helpers.h:20
std::string idStr() const noexcept
Get OID (SHA) of this Object – as a string.
Definition: Object.h:27
The most important Git class.
Definition: Repo.h:45
virtual git_object * object() const noexcept
Get the libgit2&#39;s git_object pointer of this Object.
Definition: Object.h:35
Git Object class – base for Blob, Commit, Tag, Tree.
Definition: Object.h:20
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&#39;s types.
Definition: Object.h:83
std::string typeStr() const noexcept
Get Type of this Object – as a string.
Definition: Object.h:32
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
const Repo & repo() const
Get the Repo this Object lives in.
Definition: Object.h:38