SlHelpers
Loading...
Searching...
No Matches
Buf.h
1// SPDX-License-Identifier: GPL-2.0-only
2
3#pragma once
4
5#include <string>
6#include <string_view>
7
8#include <git2.h>
9
10namespace SlGit {
11
15class Buf {
16public:
17 Buf() : m_buf(GIT_BUF_INIT) {}
18 ~Buf() { git_buf_dispose(&m_buf); }
19
20 Buf(const Buf &other) = delete;
21 Buf operator=(const Buf &other) = delete;
22
24 Buf(Buf &&other) noexcept : m_buf(other.m_buf) { other.m_buf = GIT_BUF_INIT; }
26 Buf &operator=(Buf &&other) noexcept {
27 if (this != &other) {
28 git_buf_dispose(&m_buf);
29 m_buf = other.m_buf;
30 other.m_buf = GIT_BUF_INIT;
31 }
32 return *this;
33 }
34
36 std::string_view sv() const noexcept { return std::string_view(m_buf.ptr, m_buf.size); }
38 std::string str() const noexcept { return std::string(m_buf.ptr, m_buf.size); }
39
41 git_buf &buf() { return m_buf; }
43 const git_buf &buf() const { return m_buf; }
45 operator const git_buf *() const { return &m_buf; }
46private:
47 git_buf m_buf;
48};
49
50}
Buf(Buf &&other) noexcept
Move constructor.
Definition Buf.h:24
git_buf & buf()
Get the stored libgit2's git_buf.
Definition Buf.h:41
std::string str() const noexcept
Get this Buf as a string.
Definition Buf.h:38
Buf & operator=(Buf &&other) noexcept
Move assignment.
Definition Buf.h:26
std::string_view sv() const noexcept
Get this Buf as a string_view.
Definition Buf.h:36
const git_buf & buf() const
Get the stored libgit2's git_buf.
Definition Buf.h:43