1 /*---------------------------------------------------------------------------* 2 Project: Horizon 3 File: os_ContinuationIterator.h 4 5 Copyright (C)2009 Nintendo Co., Ltd. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Rev: 20477 $ 14 *---------------------------------------------------------------------------*/ 15 16 17 #ifndef NN_OS_OS_CONTINUATIONITERATOR_H_ 18 #define NN_OS_OS_CONTINUATIONITERATOR_H_ 19 20 #ifdef __cplusplus 21 22 #include <nn.h> 23 #include <nn/Result.h> 24 #include <nn/WithInitialize.h> 25 #include <csetjmp> 26 27 namespace nn { namespace os { 28 29 class ContinuationIteratorBase 30 { 31 private: 32 33 struct Context; 34 35 static bit32 ChangeContext(bit32 retval, Context* from, Context* to); 36 static bit32 ChangeContext(bit32 retval, Context* to); 37 38 Context* m_MainContext; 39 Context* m_IteratorContext; 40 Result m_Result; 41 42 void RunImpl(); 43 static void RunIterator(ContinuationIteratorBase* _this); 44 45 protected: 46 47 // このイテレータで実行する処理をオーバライドする。 48 virtual Result Run() = 0; 49 ~ContinuationIteratorBase()50 ~ContinuationIteratorBase() {} 51 52 public: 53 54 // stackBottom でスタックを指定し、初期化する 55 void Initialize(void* stackBottom); 56 57 // 処理を MoveNext に返す。 58 // Run() の中でのみ呼ぶことができる。 59 Result Yield(); 60 61 // 次の Yield または Run() の終了までを実行する。 62 // Yield で戻った場合は true を、Run() が終了した場合には false を返す。 63 // result に ResultSuccess 以外を指定した場合には、 64 // 強制的に Run() で戻ることを通知する。 65 bool MoveNext(Result result = nn::ResultSuccess()); 66 67 // MoveNext が false を返した後、Run() の実行結果を取得する。 68 // MoveNext が false を返す以前にこの値を参照した場合の動作は未定義。 GetResult()69 Result GetResult() const { return m_Result; } 70 71 }; 72 73 template <class T> 74 class ContinuationIterator : public ContinuationIteratorBase 75 { 76 private: 77 78 T m_Value; 79 80 public: 81 82 // イテレータの値を設定する。 83 // Run() の中でのみ呼ぶことができる。 SetCurrent(const T & value)84 void SetCurrent(const T& value) { m_Value = value; } 85 86 // イテレータの値を返す。 87 // Run() の中でのみ呼ぶことができる。 YieldReturn(const T & value)88 Result YieldReturn(const T& value) { SetCurrent(value); return Yield(); } 89 90 // イテレータの値を取得する。 GetCurrent()91 const T& GetCurrent() const { return m_Value; } 92 93 }; 94 95 }} 96 97 #endif 98 99 #endif 100