bsnes-libretro/nall/unique-pointer.hpp
Tim Allen 344e63d928 Update to v097r02 release.
byuu says:

Note: balanced/performance profiles still broken, sorry.

Changelog:
- added nall/GNUmakefile unique() function; used on linking phase of
  higan
- added nall/unique_pointer
- target-tomoko and {System}::Video updated to use
  unique_pointer<ClassName> instead of ClassName* [1]
- locate() updated to search multiple paths [2]
- GB: pass gekkio's if_ie_registers and boot_hwio-G test ROMs
- FC, GB, GBA: merge video/ into the PPU cores
- ruby: fixed ~AudioXAudio2() typo

[1] I expected this to cause new crashes on exit due to changing the
order of destruction of objects (and deleting things that weren't
deleted before), but ... so far, so good. I guess we'll see what crops
up, especially on OS X (which is already crashing for unknown reasons on
exit.)

[2] right now, the search paths are: programpath(), {configpath(),
"higan/"}, {localpath(), "higan/"}; but we can add as many more as we
want, and we can also add platform-specific versions.
2016-01-25 22:27:18 +11:00

103 lines
2.4 KiB
C++

#pragma once
namespace nall {
template<typename T>
struct unique_pointer {
using type = T;
T* pointer = nullptr;
function<auto (T*) -> void> deleter;
unique_pointer(const unique_pointer&) = delete;
auto operator=(const unique_pointer&) -> unique_pointer& = delete;
unique_pointer(T* pointer = nullptr, const function<void (T*)>& deleter = {}) : pointer(pointer), deleter(deleter) {}
~unique_pointer() { reset(); }
auto operator=(T* source) -> unique_pointer& {
reset();
pointer = source;
return *this;
}
explicit operator bool() const { return pointer; }
auto operator->() -> T* { return pointer; }
auto operator->() const -> const T* { return pointer; }
auto operator*() -> T& { return *pointer; }
auto operator*() const -> const T& { return *pointer; }
auto operator()() -> T& { return *pointer; }
auto operator()() const -> const T& { return *pointer; }
auto data() -> T* { return pointer; }
auto data() const -> const T* { return pointer; }
auto release() -> T* {
auto result = pointer;
pointer = nullptr;
return result;
}
auto reset() -> void {
if(pointer) {
if(deleter) {
deleter(pointer);
} else {
delete pointer;
}
pointer = nullptr;
}
}
};
template<typename T>
struct unique_pointer<T[]> {
using type = T;
T* pointer = nullptr;
function<auto (T*) -> void> deleter;
unique_pointer(const unique_pointer&) = delete;
auto operator=(const unique_pointer&) -> unique_pointer& = delete;
unique_pointer(T* pointer = nullptr, const function<void (T*)>& deleter = {}) : pointer(pointer), deleter(deleter) {}
~unique_pointer() { reset(); }
auto operator=(T* source) -> unique_pointer& {
reset();
pointer = source;
return *this;
}
explicit operator bool() const { return pointer; }
auto operator()() -> T* { return pointer; }
auto operator()() const -> T* { return pointer; }
alwaysinline auto operator[](uint offset) -> T& { return pointer[offset]; }
alwaysinline auto operator[](uint offset) const -> const T& { return pointer[offset]; }
auto data() -> T* { return pointer; }
auto data() const -> const T* { return pointer; }
auto release() -> T* {
auto result = pointer;
pointer = nullptr;
return result;
}
auto reset() -> void {
if(pointer) {
if(deleter) {
deleter(pointer);
} else {
delete[] pointer;
}
pointer = nullptr;
}
}
};
}