Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class resource_descriptor>
- class resource_t{
- using self_t = resource_t<resource_descriptor>;
- using value_t = typename resource_descriptor::type;
- public:
- resource_t()
- : value()
- , deleter() {
- }
- template<class U>
- resource_t(U&& value)
- : value(std::forward<U>(value))
- , deleter() {
- }
- template<class U, class D>
- resource_t(U&& value, D&& deleter)
- : value(std::forward<U>(value))
- , deleter(std::forward<D>(deleter)){
- }
- resource_t(self_t&& other)
- : value(other.deleter.move(other.value))
- , deleter(std::move(other.deleter)){
- }
- ~resource_t(){
- reset();
- }
- value_t release(){
- return deleter.move(value);
- }
- self_t& operator=(self_t&& other){
- reset();
- value = other.deleter.move(other.value);
- deleter = std::move(other.deleter);
- return *this;
- }
- value_t& get(){
- return value;
- }
- const value_t& get() const{
- return value;
- }
- void reset(){
- deleter(value);
- }
- private:
- value_t value;
- resource_descriptor deleter;
- };
- ---------------------------------
- struct WindowClassResourceDescriptor{
- using type = ATOM;
- WindowClassResourceDescriptor()
- : instance(0){
- }
- WindowClassResourceDescriptor(HINSTANCE instance)
- : instance(instance){
- }
- ATOM move(ATOM& value){
- ATOM ret = value;
- value = 0;
- return ret;
- }
- void operator()(ATOM& value){
- if ( value ){
- UnregisterClassW(MAKEINTATOM(value), instance);
- value = 0;
- }
- }
- private:
- HINSTANCE instance;
- };
- using WindowClassResource = resource_t<WindowClassResourceDescriptor>;
- struct HWNDResourceDescriptor {
- using type = HWND;
- HWNDResourceDescriptor(){
- }
- HWND move(HWND& value) {
- HWND ret = value;
- value = NULL;
- return ret;
- }
- void operator()(HWND& value) {
- if (value) {
- DestroyWindow(value);
- value = NULL;
- }
- }
- };
- using HWNDResource = resource_t<HWNDResourceDescriptor>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement