raw
This commit is contained in:
64
Thirdparty/Pangolin/include/pangolin/utils/registration.h
vendored
Normal file
64
Thirdparty/Pangolin/include/pangolin/utils/registration.h
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace pangolin {
|
||||
|
||||
template<typename TokenType=void>
|
||||
class Registration
|
||||
{
|
||||
public:
|
||||
using UnregisterFunc = std::function<void(const TokenType&)>;
|
||||
|
||||
Registration()
|
||||
: token()
|
||||
{
|
||||
}
|
||||
|
||||
Registration(TokenType token, UnregisterFunc unregister)
|
||||
: token(token), unregister(unregister)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// No copy constructor
|
||||
Registration(const Registration&) = delete;
|
||||
|
||||
// Default move constructor
|
||||
Registration(Registration&& o)
|
||||
{
|
||||
*this = std::move(o);
|
||||
}
|
||||
|
||||
Registration<TokenType>& operator =(Registration<TokenType>&& o)
|
||||
{
|
||||
token = o.token;
|
||||
unregister = std::move(o.unregister);
|
||||
o.unregister = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Registration()
|
||||
{
|
||||
Release();
|
||||
}
|
||||
|
||||
void Release()
|
||||
{
|
||||
if(unregister) {
|
||||
unregister(token);
|
||||
token = TokenType();
|
||||
}
|
||||
}
|
||||
|
||||
const TokenType& Token()
|
||||
{
|
||||
return token;
|
||||
}
|
||||
|
||||
private:
|
||||
TokenType token;
|
||||
UnregisterFunc unregister;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user