/*---------------------------------------------------------------------------* Project: Horizon File: os_ContinuationIterator.h Copyright (C)2009 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 20477 $ *--------------------------------------------------------------------------- */ #ifndef NN_OS_OS_CONTINUATIONITERATOR_H_ #define NN_OS_OS_CONTINUATIONITERATOR_H_ #ifdef __cplusplus #include #include #include #include namespace nn { namespace os { class ContinuationIteratorBase { private: struct Context; static bit32 ChangeContext(bit32 retval, Context* from, Context* to); static bit32 ChangeContext(bit32 retval, Context* to); Context* m_MainContext; Context* m_IteratorContext; Result m_Result; void RunImpl(); static void RunIterator(ContinuationIteratorBase* _this); protected: // Overrides the process that executes using this iterator virtual Result Run() = 0; ~ContinuationIteratorBase() {} public: // Specify the stack using stackBottom and initialize it void Initialize(void* stackBottom); // Return the process to MoveNext. // Can only call from inside the Run function. Result Yield(); // Runs until the next Yield or until the termination of Run. // Returns 'true' if Yield was encountered; Returns 'false' if Run function terminated. // If a value other than "ResultSuccess" is specified for "result", send notification to return forcibly using Run function. // bool MoveNext(Result result = nn::ResultSuccess()); // Get the execution result of Run function after MoveNext returns "false". // If this value is referenced before MoveNext returns "false", the behavior is undefined. Result GetResult() const { return m_Result; } }; template class ContinuationIterator : public ContinuationIteratorBase { private: T m_Value; public: // Sets the iterator value. // Can only call from inside the Run function. void SetCurrent(const T& value) { m_Value = value; } // Returns the iterator value. // Can only call from inside the Run function. Result YieldReturn(const T& value) { SetCurrent(value); return Yield(); } // Gets the iterator value. const T& GetCurrent() const { return m_Value; } }; }} #endif #endif