[C++]C++ Simple Lazy class
template<class T> class Lazy { #pragma region Var private: std::function<T (void)> _func; T _result; bool _bIsValueCreated; #pragma endregion
#pragma region Private Property private: __declspec(property(get=Get_func,put=Set_func)) std::function<T (void)> m_func; #pragma endregion
#pragma region Public Property public: __declspec(property(get=Get_result)) T m_result;
__declspec(property(get=Get_bIsValueCreated,put=Set_bIsValueCreated))
bool m_bIsValueCreated;
#pragma endregion
#pragma region Constructor & DeConstructor public: Lazy(std::function<T (void)> func) { Reset(); m_func = func; }
~Lazy(void)
{
Reset();
}
#pragma endregion
#pragma region Property Process Method private: inline std::function<T (void)> Get_func() { return _func; }
inline void Set_func(std::function<T (void)> value)
{
_func = value;
}
inline void Set_bIsValueCreated(bool value)
{
_bIsValueCreated = value;
}
public: inline T Get_result() { if(!m_bIsValueCreated) { Init(); } return _result; }
inline bool Get_bIsValueCreated()
{
return _result;
}
#pragma endregion
#pragma region Protected Method protected: void Reset() { _func = NULL; _result = NULL; _bIsValueCreated = false; } #pragma endregion
#pragma region Public Method public: void Init() { if(m_bIsValueCreated) return; _result = m_func(); m_bIsValueCreated=true; }
void ClearValue()
{
std::function<T (void)> func = m_func;
Reset();
m_func = func;
}
#pragma endregion };