// RuntimeForStatic base library | C+- (SysRoutine) | Author: Sergei Y.Kitáev
#pragma once
#include "include_restrictions.h"
namespace RuntimeForStatic {
//==================================================================================================

template<typename TResult>$with_finally_r<TResult>::$with_finally_r($A,
   const std::function<TResult()>& body,
   const $ActionFunc& finally )
{
   if ( !body )
      $FatalError::RaiseNullTargetOfArgument($$CI, $NAME_OF( body ) );
   if ( !finally )
      $FatalError::RaiseNullTargetOfArgument($$CI, $NAME_OF( finally ) );
   _BodyFuncPtr_ = &body;
   _FinallyFuncPtr_ = &finally;
}

template<typename TResult>$with_finally_r<TResult>::~$with_finally_r()
{
   (*_FinallyFuncPtr_)();
}

//--------------------------------------------------------------------------------------------------

template<typename TResult>TResult $with_finally_r<TResult>::Do($O) const
{
   if ( !_BodyFuncPtr_ )
      $INVALID_OPERATION();
   return (*_BodyFuncPtr_)();
}

//==================================================================================================
} // RuntimeForStatic
