CPatch: New memory management
[TortoiseGit.git] / src / Utils / SmartLibgit2Ref.h
blobaa8d770108d064f5cabe04e279470d4b5b5d1081
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2014-2018 - TortoiseGit
4 // based on SmartHandle of TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #include "UnicodeUtils.h"
23 /**
24 * \ingroup Utils
25 * Helper classes for libgit2 references.
27 template <typename HandleType, void FreeFunction(HandleType*)>
28 class CSmartBuffer
30 public:
31 CSmartBuffer()
32 : m_Ref({0})
36 operator HandleType*()
38 return &m_Ref;
41 HandleType* operator->()
43 return &m_Ref;
46 ~CSmartBuffer()
48 FreeFunction(&m_Ref);
51 private:
52 CSmartBuffer(const CSmartBuffer&) = delete;
53 CSmartBuffer& operator=(const CSmartBuffer&) = delete;
55 HandleType m_Ref;
58 typedef CSmartBuffer<git_buf, git_buf_dispose> CAutoBuf;
59 typedef CSmartBuffer<git_strarray, git_strarray_free> CAutoStrArray;
61 template <typename ReferenceType, void FreeFunction(ReferenceType*)>
62 class CSmartLibgit2Ref
64 public:
65 CSmartLibgit2Ref()
66 : m_Ref(nullptr)
70 ~CSmartLibgit2Ref()
72 Free();
75 void Swap(CSmartLibgit2Ref& tmp)
77 ReferenceType* p;
79 p = m_Ref;
80 m_Ref = tmp.m_Ref;
81 tmp.m_Ref = p;
84 void Free()
86 FreeFunction(m_Ref);
87 m_Ref = nullptr;
90 ReferenceType* Detach()
92 ReferenceType* p = m_Ref;
93 m_Ref = nullptr;
95 return p;
98 operator ReferenceType*() const
100 return m_Ref;
104 * Free the wrapped object and return a pointer to the wrapped object ReferenceType*
106 ReferenceType** GetPointer()
108 Free();
109 return &m_Ref;
112 operator bool() const
114 return IsValid();
117 bool IsValid() const
119 return m_Ref != nullptr;
122 private:
123 CSmartLibgit2Ref(const CSmartLibgit2Ref&) = delete;
124 CSmartLibgit2Ref& operator=(const CSmartLibgit2Ref&) = delete;
126 protected:
127 ReferenceType* m_Ref;
130 typedef CSmartLibgit2Ref<git_object, git_object_free> CAutoObject;
131 typedef CSmartLibgit2Ref<git_submodule, git_submodule_free> CAutoSubmodule;
132 typedef CSmartLibgit2Ref<git_blob, git_blob_free> CAutoBlob;
133 typedef CSmartLibgit2Ref<git_reference, git_reference_free> CAutoReference;
134 typedef CSmartLibgit2Ref<git_tag, git_tag_free> CAutoTag;
135 typedef CSmartLibgit2Ref<git_tree_entry, git_tree_entry_free> CAutoTreeEntry;
136 typedef CSmartLibgit2Ref<git_diff, git_diff_free> CAutoDiff;
137 typedef CSmartLibgit2Ref<git_patch, git_patch_free> CAutoPatch;
138 typedef CSmartLibgit2Ref<git_diff_stats, git_diff_stats_free> CAutoDiffStats;
139 typedef CSmartLibgit2Ref<git_index, git_index_free> CAutoIndex;
140 typedef CSmartLibgit2Ref<git_remote, git_remote_free> CAutoRemote;
141 typedef CSmartLibgit2Ref<git_reflog, git_reflog_free> CAutoReflog;
142 typedef CSmartLibgit2Ref<git_revwalk, git_revwalk_free> CAutoRevwalk;
143 typedef CSmartLibgit2Ref<git_branch_iterator, git_branch_iterator_free> CAutoBranchIterator;
144 typedef CSmartLibgit2Ref<git_reference_iterator, git_reference_iterator_free> CAutoReferenceIterator;
145 typedef CSmartLibgit2Ref<git_describe_result, git_describe_result_free> CAutoDescribeResult;
146 typedef CSmartLibgit2Ref<git_status_list, git_status_list_free> CAutoStatusList;
147 typedef CSmartLibgit2Ref<git_note, git_note_free> CAutoNote;
148 typedef CSmartLibgit2Ref<git_signature, git_signature_free> CAutoSignature;
150 class CAutoRepository : public CSmartLibgit2Ref<git_repository, git_repository_free>
152 public:
153 CAutoRepository() {};
155 explicit CAutoRepository(git_repository*&& h)
157 m_Ref = h;
160 CAutoRepository(CAutoRepository&& that)
162 m_Ref = that.Detach();
165 #if defined(_MFC_VER) || defined(CSTRING_AVAILABLE)
166 CAutoRepository(const CString& gitDir)
168 Open(gitDir);
171 CAutoRepository(const CStringA& gitDirA)
173 Open(gitDirA);
176 int Open(const CString& gitDir)
178 return Open(CUnicodeUtils::GetUTF8(gitDir));
180 #endif
182 private:
183 CAutoRepository(const CAutoRepository&) = delete;
184 CAutoRepository& operator=(const CAutoRepository&) = delete;
186 protected:
187 int Open(const char* gitDirA)
189 return git_repository_open(GetPointer(), gitDirA);
193 class CAutoCommit : public CSmartLibgit2Ref<git_commit, git_commit_free>
195 public:
196 CAutoCommit() {}
198 explicit CAutoCommit(git_commit*&& h)
200 m_Ref = h;
203 private:
204 CAutoCommit(const CAutoCommit&) = delete;
205 CAutoCommit& operator=(const CAutoCommit&) = delete;
208 class CAutoTree : public CSmartLibgit2Ref<git_tree, git_tree_free>
210 public:
211 CAutoTree() {};
213 void ConvertFrom(CAutoObject&& h)
215 if (m_Ref != (git_tree*)(git_object*)h)
217 Free();
218 m_Ref = (git_tree*)h.Detach();
222 private:
223 CAutoTree(const CAutoTree&) = delete;
224 CAutoTree& operator=(const CAutoTree&) = delete;
227 class CAutoConfig : public CSmartLibgit2Ref<git_config, git_config_free>
229 public:
230 CAutoConfig() {}
232 CAutoConfig(CAutoRepository &repo)
234 git_repository_config(&m_Ref, repo);
237 CAutoConfig(bool init)
239 if (init)
240 New();
243 void New()
245 Free();
246 git_config_new(&m_Ref);
249 #if defined(_MFC_VER) || defined(CSTRING_AVAILABLE)
250 int GetString(const CString &key, CString &value) const
252 if (!IsValid())
253 return -1;
255 CAutoBuf buf;
256 int ret = 0;
257 if ((ret = git_config_get_string_buf(buf, m_Ref, CUnicodeUtils::GetUTF8(key))))
259 value.Empty();
260 return ret;
263 value = CUnicodeUtils::GetUnicode((CStringA)buf->ptr);
265 return ret;
268 int GetBOOL(const CString &key, BOOL &b) const
270 if (!IsValid())
271 return -1;
273 return git_config_get_bool(&b, m_Ref, CUnicodeUtils::GetUTF8(key));
276 int GetBool(const CString &key, bool &b) const
278 if (!IsValid())
279 return -1;
281 int value = FALSE;
282 int ret = 0;
283 if ((ret = GetBOOL(key, value)) < 0)
284 return ret;
286 b = (value == TRUE);
288 return ret;
290 #endif
292 private:
293 CAutoConfig(const CAutoConfig&) = delete;
294 CAutoConfig& operator=(const CAutoConfig&) = delete;