Line data Source code
1 : /*! 2 : * \file esys/repo/libgit2/guard.h 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2020-2023 Michel Gillet 9 : * Distributed under the MIT License. 10 : * (See accompanying file LICENSE.txt or 11 : * copy at https://opensource.org/licenses/MIT) 12 : * 13 : * __legal_e__ 14 : * \endcond 15 : * 16 : */ 17 : 18 : #pragma once 19 : 20 : #include "esys/repo/esysrepo_defs.h" 21 : #include "esys/repo/libgit2/guardrelease.h" 22 : #include "esys/repo/libgit2/guardassign.h" 23 : #include "esys/repo/libgit2/guardequal.h" 24 : #include "esys/repo/libgit2/guardprint.h" 25 : 26 : namespace esys::repo::libgit2 27 : { 28 : 29 : template<typename T> 30 : class Guard 31 : { 32 : public: 33 : template<typename U> 34 : friend int guard_assign(Guard<U> *dest, const Guard<U> &src); 35 : 36 : template<typename U> 37 : friend bool guard_equal(const Guard<U> &left, const Guard<U> &right); 38 : 39 : template<typename U> 40 : friend void guard_print(std::ostream &os, const Guard<U> &object); 41 : 42 : Guard(); 43 : ~Guard(); 44 : 45 : T *get() const; 46 : T **get_p(); 47 : 48 : void reset(); 49 : 50 : void operator=(const Guard &other); 51 : 52 : //! Equal to comparison operator 53 : bool operator==(const Guard &other) const; 54 : 55 : //! Not equal to comparison operator 56 : bool operator!=(const Guard &other) const; 57 : 58 : private: 59 : T *m_data = nullptr; 60 : }; 61 : 62 : template<typename T> 63 1366 : Guard<T>::Guard() 64 : { 65 : } 66 : 67 : template<typename T> 68 1839 : Guard<T>::~Guard() 69 : { 70 1372 : guard_release<T>(m_data); 71 1169 : } 72 : 73 : template<typename T> 74 2322 : T *Guard<T>::get() const 75 : { 76 1811 : return m_data; 77 : } 78 : 79 : template<typename T> 80 1262 : T **Guard<T>::get_p() 81 : { 82 93 : return &m_data; 83 : } 84 : 85 : template<typename T> 86 30 : void Guard<T>::reset() 87 : { 88 30 : if (m_data == nullptr) return; 89 : 90 22 : guard_release<T>(m_data); 91 22 : m_data = nullptr; 92 : } 93 : 94 : template<typename T> 95 11 : void Guard<T>::operator=(const Guard &other) 96 : { 97 11 : int result = guard_assign<T>(this, other); 98 : // \TODO throw exception?? 99 11 : } 100 : 101 : template<typename T> 102 0 : bool Guard<T>::operator==(const Guard &other) const 103 : { 104 0 : if ((get() == nullptr) && (other.get() == nullptr)) return true; 105 0 : if ((get() == nullptr) || (other.get() == nullptr)) return false; 106 : 107 0 : return guard_equal(*this, other); 108 : } 109 : 110 : template<typename T> 111 : bool Guard<T>::operator!=(const Guard &other) const 112 : { 113 : return !operator==(other); 114 : } 115 : 116 : template<typename T> 117 0 : void libgit2_print(std::ostream &os, const Guard<T> &object) 118 : { 119 0 : guard_print(os, object); 120 : 121 : //os << "<" << typeid(T).name() << ">"; 122 : } 123 : 124 : } // namespace esys::repo::libgit2 125 : 126 : namespace std 127 : { 128 : 129 : template<typename T> 130 0 : ostream &operator<<(ostream &os, const esys::repo::libgit2::Guard<T> &guard) 131 : { 132 0 : if (guard.get() == nullptr) 133 : { 134 0 : os << "<nullptr>"; 135 0 : return os; 136 : } 137 : 138 0 : esys::repo::libgit2::libgit2_print(os, guard); 139 0 : return os; 140 : } 141 : 142 : } // namespace std