-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblSmartPointerFunctions.hpp
108 lines (90 loc) · 3.66 KB
/
blSmartPointerFunctions.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef BL_SMARTPOINTERFUNCTIONS_HPP
#define BL_SMARTPOINTERFUNCTIONS_HPP
//-------------------------------------------------------------------
// FILE: blSmartPointerFunctions.hpp
// CLASS: None
// BASE CLASS: None
//
// PURPOSE: A collection of simple functions used to
// get smart pointers of resources.
//
// AUTHOR: Vincenzo Barbato
// http://www.barbatolabs.com
// navyenzo@gmail.com
//
// LISENSE: MIT-LICENCE
// http://www.opensource.org/licenses/mit-license.php
//
// DEPENDENCIES: - std::shared_ptr
//
// NOTES:
//
// DATE CREATED: Oct/04/2013
//
// DATE UPDATED:
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes and libs needed for this file
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Enums used for this file and sub-files
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// FUNCTION: null_deleter
//
// PURPOSE: Used when creating std::shared_ptr
// smart pointers so the shared pointer
// doesn't try to release the resource
// being pointed to.
//-------------------------------------------------------------------
template<typename blResourceType>
class null_deleter
{
public:
void operator()(blResourceType const*)const
{
}
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to get shared pointers of resources
//
// NOTE: The resources can be passed as references
// or pointers, and if passed by pointer, the
// returned shared pointer can be set in charge
// of deleting the pointer or not.
//-------------------------------------------------------------------
template<typename blResourceType>
inline std::shared_ptr<blResourceType> get_shared_ptr(blResourceType& theResource)
{
return std::shared_ptr<blResourceType>(&theResource,null_deleter<blResourceType>());
}
template<typename blResourceType>
inline std::shared_ptr<blResourceType const> get_const_shared_ptr(const blResourceType& theResource)
{
return std::shared_ptr<blResourceType const>(&theResource,null_deleter<const blResourceType>());
}
template<typename blResourceType>
inline std::shared_ptr<blResourceType> get_shared_ptr(blResourceType* theResource)
{
return std::shared_ptr<blResourceType>(theResource,null_deleter<blResourceType>());
}
template<typename blResourceType>
inline std::shared_ptr<blResourceType const> get_const_shared_ptr(blResourceType const* theResource)
{
return std::shared_ptr<blResourceType const>(theResource,null_deleter<const blResourceType>());
}
template<typename blResourceType>
inline std::shared_ptr<blResourceType> get_shared_ptr_and_delete_when_done(blResourceType* theResource)
{
return std::shared_ptr<blResourceType>(theResource);
}
template<typename blResourceType>
inline std::shared_ptr<blResourceType const> get_const_shared_ptr_and_delete_when_done(blResourceType const* theResource)
{
return std::shared_ptr<blResourceType const>(theResource);
}
//-------------------------------------------------------------------
#endif // BL_SMARTPOINTERFUNCTIONS_HPP