tools

various tools
git clone git://deadbeef.fr/tools.git
Log | Files | Refs | README | LICENSE

memory.hpp (2913B)


      1 // Copyright (c) 2020 Morel BĂ©renger
      2 // 
      3 // This software is provided 'as-is', without any express or implied
      4 // warranty. In no event will the authors be held liable for any damages
      5 // arising from the use of this software.
      6 // 
      7 // Permission is granted to anyone to use this software for any purpose,
      8 // including commercial applications, and to alter it and redistribute it
      9 // freely, subject to the following restrictions:
     10 // 
     11 // 1. The origin of this software must not be misrepresented; you must not
     12 //    claim that you wrote the original software. If you use this software
     13 //    in a product, an acknowledgment in the product documentation would be
     14 //    appreciated but is not required.
     15 // 2. Altered source versions must be plainly marked as such, and must not be
     16 //    misrepresented as being the original software.
     17 // 3. This notice may not be removed or altered from any source distribution.
     18 
     19 // This file defines unique_res and weak_res class utilities.
     20 // goal: easy RAII for any resource, without any assumption
     21 //   about what is an invalid value, and without consuming
     22 //   more data memory than a single, raw pointer.
     23 
     24 #ifndef MEMORY_HPP
     25 #define MEMORY_HPP
     26 
     27 #include <utility>
     28 
     29 template <typename T>
     30 void generic_free( T* p )
     31 {
     32 	free( p );
     33 }
     34 
     35 template
     36 <
     37 	typename T,
     38 	T INVALID_VAL,
     39 	void(*DTOR)(T)
     40 >
     41 class weak_res
     42 {
     43 protected:
     44 	typedef weak_res<T,INVALID_VAL,DTOR> COMPLETE_TYPE;
     45 	T m_data;
     46 public:
     47 	bool valid( void ) const
     48 	{
     49 		return m_data != INVALID_VAL;
     50 	}
     51 
     52 	bool operator==( COMPLETE_TYPE const& other ) const
     53 	{
     54 		return m_data == other.m_data;
     55 	}
     56 
     57 	bool operator==( T const& other ) const
     58 	{
     59 		return m_data == other;
     60 	}
     61 
     62 	bool operator!=( COMPLETE_TYPE const& other ) const
     63 	{
     64 		return m_data != other.m_data;
     65 	}
     66 
     67 	bool operator!=( T const& other ) const
     68 	{
     69 		return m_data != other;
     70 	}
     71 
     72 	const T get( void ) const
     73 	{
     74 		return m_data;
     75 	}
     76 };
     77 
     78 template
     79 <
     80 	typename T,
     81 	T INVALID_VAL,
     82 	void(*DTOR)(T)
     83 >
     84 class unique_res : public weak_res<T,INVALID_VAL,DTOR>
     85 //class unique_res
     86 {
     87 	typedef unique_res<T,INVALID_VAL,DTOR> UNIQUE_TYPE;
     88 public:
     89 	unique_res( UNIQUE_TYPE const& ) = delete;
     90 	UNIQUE_TYPE& operator=( UNIQUE_TYPE const& other ) = delete;
     91 
     92 	unique_res( void )
     93 	{
     94 		this->m_data = INVALID_VAL;
     95 	}
     96 
     97 	unique_res( UNIQUE_TYPE && other )
     98 	{
     99 		this->m_data = other.m_data;
    100 		other.m_data = INVALID_VAL;
    101 	}
    102 
    103 	unique_res( T && src )
    104 	{
    105 		this->m_data = src;
    106 		src = INVALID_VAL;
    107 	}
    108 
    109 	UNIQUE_TYPE& operator=( UNIQUE_TYPE && other )
    110 	{
    111 		clear();
    112 		std::swap( this->m_data, other.m_data );
    113 		return *this;
    114 	}
    115 
    116 	UNIQUE_TYPE& operator=( T && other )
    117 	{
    118 		clear();
    119 		std::swap( this->m_data, other );
    120 		return *this;
    121 	}
    122 
    123 	T release( void )
    124 	{
    125 		T ret = this->m_data;
    126 		this->m_data = INVALID_VAL;
    127 		return ret;
    128 	}
    129 
    130 	void clear( void )
    131 	{
    132 		if( INVALID_VAL != this->m_data )
    133 		{
    134 			DTOR( this->m_data );
    135 			this->m_data = INVALID_VAL;
    136 		}
    137 	}
    138 
    139 	~unique_res( void )
    140 	{
    141 		clear();
    142 	}
    143 };
    144 
    145 #endif