Created
August 7, 2018 02:56
-
-
Save prince-chrismc/9bab1f399e65e064a956cf9346921752 to your computer and use it in GitHub Desktop.
Object Owner Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <memory> | |
#include <thread> | |
template< typename __OBJECT__> | |
class CObjectOwner | |
{ | |
public: | |
template <typename ...Args> | |
CObjectOwner() : m_poObject( std::make_unique( std::forward<Args>(args)... ) {} | |
void GetObjectOwnership( std::unique_ptr<__OBJECT__>& io_poObject ) { m_muObjectProtector.lock(); io_poObject = std::move( m_poObject ); } | |
void ReturnObject( std::unique_ptr<__OBJECT__>&& io_poObject ) { m_poObject = std::move( io_poObject ); m_muObjectProtector.unlock(); } | |
private: | |
std::unique_ptr<__OBJECT__> m_poObject; | |
std::mutex m_muObjectProtector; | |
}; | |
/* | |
std::unique_ptr<TMapOfResources> mappoResources; | |
GetObjectOwnership( mappoResources ); | |
--> DoStuff(); | |
ReturnObject( std::move( mappoResources ) ); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment