1 /*---------------------------------------------------------------------------*
2   Project: NintendoWare
3   File   : ut_Signal.h
4 
5   Copyright (C) 2010 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   $Revision: 18106 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_UT_SIGNAL_H_
17 #define NW_UT_SIGNAL_H_
18 
19 #include <nw/os/os_Memory.h>
20 #include <nw/ut/ut_MoveArray.h>
21 
22 namespace nw
23 {
24 namespace ut
25 {
26 
27 //----------------------------------------
28 //! @name シグナル関連
29 //@{
30 
31 //---------------------------------------------------------------------------
32 //! @brief        複数の呼び出しを行い、最後の返り値を返す関数オブジェクトです。
33 //---------------------------------------------------------------------------
34 template<typename TResult>
35 struct LastValueResult
36 {
37     typedef TResult ResultType;
38     template<typename TInputIterator, typename TInvoker>
operatorLastValueResult39     ResultType operator()(TInputIterator first, TInputIterator last, const TInvoker& invoker)
40     {
41         ResultType result;
42         while (first != last)
43         {
44             result = invoker(*first);
45             ++first;
46         }
47         return result;
48     }
49 };
50 
51 //---------------------------------------------------------------------------
52 //! @brief        複数の呼び出しを行い、最後の返り値を返す関数オブジェクトです。
53 //---------------------------------------------------------------------------
54 template<>
55 struct LastValueResult<void>
56 {
57     template<typename TInputIterator, typename TInvoker>
58     void operator()(TInputIterator first, TInputIterator last, const TInvoker& invoker)
59     {
60         while (first != last)
61         {
62             invoker(*first);
63             ++first;
64         }
65     }
66 };
67 
68 //---------------------------------------------------------------------------
69 //! @brief        スロットを生成します。
70 //!
71 //! @tparam       TSlot スロットの型です。
72 //! @tparam       TFunction 関数ポインタまたは、関数オブジェクトの型です。
73 //!
74 //! @param[in]    allocator アロケータです。
75 //! @param[in]    function 関数または、関数オブジェクトです。
76 //!
77 //! @return       生成したスロットです。
78 //---------------------------------------------------------------------------
79 template<typename TSlot, typename TFunction>
80 NW_INLINE TSlot*
81 CreateSlot(
82     os::IAllocator* allocator,
83     TFunction function
84 )
85 {
86     NW_NULL_ASSERT(allocator);
87 
88     void* memory = allocator->Alloc(sizeof(TSlot));
89     NW_NULL_ASSERT(memory);
90 
91     return new(memory) TSlot(allocator, function);
92 }
93 
94 //---------------------------------------------------------------------------
95 //! @brief        スロットを破棄する関数オブジェクトです。
96 //!
97 //! @tparam       TSlot スロットの型です。
98 //---------------------------------------------------------------------------
99 template<typename TSlot>
100 struct SlotDestroyer : public std::unary_function<TSlot&, void>
101 {
102     void operator()(TSlot& slot) const
103     {
104         slot->Destroy();
105     }
106 };
107 
108 //---------------------------------------------------------------------------
109 //! @brief        コンテナ要素の全てのスロットを破棄するための関数です。
110 //!
111 //! @tparam       TArray 削除するスロットの配列の型です。
112 //!
113 //! @param[in]    array 削除するスロットの配列です。
114 //---------------------------------------------------------------------------
115 template<typename TArray>
116 NW_INLINE void
117 DestroyAllSlots(
118     TArray& array
119 )
120 {
121     std::for_each(array.begin(), array.end(), SlotDestroyer<typename TArray::value_type>());
122     array.clear();
123 }
124 
125 //@}
126 
127 // ----------------------------------------------------------------------------
128 // 自動生成したファイルをインクルードします。
129 // ----------------------------------------------------------------------------
130 #include <nw/ut/generated/ut_Signal.hi>
131 
132 } // namespace ut
133 } // namespace nw
134 
135 #endif // NW_UT_SIGNAL_H_
136