commit 2b7aee32b91cb8eb8f1df6133882220883f5855a
parent 6a9b2bea81d59a2208d0e1cc672839fa7e9a6486
Author: Morel BĂ©renger <berengermorel76@gmail.com>
Date: Tue, 16 Nov 2021 19:40:00 +0100
btl: unique_res improvements
Diffstat:
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/btl/src/memory.hpp b/btl/src/memory.hpp
@@ -26,22 +26,34 @@
#include <utility>
+// ease the life for memory stuff, just use:
+// typedef unique_res<T,nullptr,generic_free<T>> T_owner;
+// typedef weak_res<T,nullptr> T_owned;
template <typename T>
void generic_free( T* p )
{
- free( p );
+ delete p;
}
+// those classes are purely for semantics, and do not
+// provide any real guarantee, as anyone is free to call
+// `close()`, `free()` or whatever function able to release
+// resource in `T`, but at least it should make it easier to
+// understand that it is a bad idea.
+// Arrays are explicitly not handled, because that's what
+// vector is for. If you can't affort the small overhead of
+// one pointer per collection, you likely can afford to
+// write your own stuff.
+
template
<
typename T,
- T INVALID_VAL,
- void(*DTOR)(T)
+ T INVALID_VAL
>
class weak_res
{
protected:
- typedef weak_res<T,INVALID_VAL,DTOR> COMPLETE_TYPE;
+ typedef weak_res<T,INVALID_VAL> COMPLETE_TYPE;
T m_data;
public:
bool valid( void ) const
@@ -81,8 +93,7 @@ template
T INVALID_VAL,
void(*DTOR)(T)
>
-class unique_res : public weak_res<T,INVALID_VAL,DTOR>
-//class unique_res
+class unique_res : public weak_res<T,INVALID_VAL>
{
typedef unique_res<T,INVALID_VAL,DTOR> UNIQUE_TYPE;
public: