Skip to content

Instantly share code, notes, and snippets.

@prince-chrismc
Created August 7, 2018 02:56
Show Gist options
  • Save prince-chrismc/9bab1f399e65e064a956cf9346921752 to your computer and use it in GitHub Desktop.
Save prince-chrismc/9bab1f399e65e064a956cf9346921752 to your computer and use it in GitHub Desktop.
Object Owner Template
#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