Ownership is best expressed with std::unique_ptr
.
class Logger {
private:
std::unique_ptr<ILogTarget> _Target;
};
int hash(const char* name) {
std::ifstream input(name, std::ios::binary);
const auto size = 4096;
std::unique_ptr<char[]> buffer(new char[size]);
int h = 42;
while (input) {
input.read(buffer.get(), size);
h = h + buffer[42] % 42;
}
return h;
}
FILE*
It is the same as every other resource:
fopen
fclose
struct FCloserDeleter
{
void operator()(FILE* file) const
{
if (file)
{
fclose(file);
}
}
};
typedef std::unique_ptr<FILE, FCloserDeleter> FilePtr;
template <typename T>
using UniqueReleasePtr<T> = std::unique_ptr<T, ReleaseDeleter>;
template <typename T>
using UniqueDestroyPtr<T> = std::unique_ptr<T, DestroyDeleter>;
namespace std
{
template <>
struct default_delete<ID3DDevice>
{
void operator()(ID3DDevice* ptr) const
{
ptr->Release();
}
}
}
owner
Presented in A brief introduction to C++’s model for type and resource safety
Implemented in:
template <class T>
using owner = T;
(https://github.com/Microsoft/GSL/blob/master/include/gsl/gsl#L55)
Static checkers:
intrusive_ptr
or shared_ptr