1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     ut_Signal.h
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NW_UT_SIGNAL_H_
19 #define NW_UT_SIGNAL_H_
20 
21 #include <nw/os/os_Memory.h>
22 #include <nw/ut/ut_MoveArray.h>
23 
24 namespace nw
25 {
26 namespace ut
27 {
28 
29 //----------------------------------------
30 //! @name シグナル関連
31 //@{
32 
33 //---------------------------------------------------------------------------
34 //! @brief        複数の呼び出しを行い、最後の返り値を返す関数オブジェクトです。
35 //---------------------------------------------------------------------------
36 template<typename TResult>
37 struct LastValueResult
38 {
39     typedef TResult ResultType;
40     template<typename TInputIterator, typename TInvoker>
operatorLastValueResult41     ResultType operator()(TInputIterator first, TInputIterator last, const TInvoker& invoker)
42     {
43         ResultType result;
44         while (first != last)
45         {
46             result = invoker(*first);
47             ++first;
48         }
49         return result;
50     }
51 };
52 
53 //---------------------------------------------------------------------------
54 //! @brief        複数の呼び出しを行い、最後の返り値を返す関数オブジェクトです。
55 //---------------------------------------------------------------------------
56 template<>
57 struct LastValueResult<void>
58 {
59     template<typename TInputIterator, typename TInvoker>
60     void operator()(TInputIterator first, TInputIterator last, const TInvoker& invoker)
61     {
62         while (first != last)
63         {
64             invoker(*first);
65             ++first;
66         }
67     }
68 };
69 
70 //---------------------------------------------------------------------------
71 //! @brief        スロットを生成します。
72 //!
73 //! @tparam       TSlot スロットの型です。
74 //! @tparam       TFunction 関数ポインタまたは、関数オブジェクトの型です。
75 //!
76 //! @param[in]    allocator アロケータです。
77 //! @param[in]    function 関数または、関数オブジェクトです。
78 //!
79 //! @return       生成したスロットです。
80 //---------------------------------------------------------------------------
81 template<typename TSlot, typename TFunction>
82 NW_INLINE TSlot*
83 CreateSlot(
84     os::IAllocator* allocator,
85     TFunction function
86 )
87 {
88     NW_NULL_ASSERT(allocator);
89 
90     void* memory = allocator->Alloc(sizeof(TSlot));
91     NW_NULL_ASSERT(memory);
92 
93     return new(memory) TSlot(allocator, function);
94 }
95 
96 //---------------------------------------------------------------------------
97 //! @brief        スロットを破棄する関数オブジェクトです。
98 //!
99 //! @tparam       TSlot スロットの型です。
100 //---------------------------------------------------------------------------
101 template<typename TSlot>
102 struct SlotDestroyer : public std::unary_function<TSlot&, void>
103 {
104     void operator()(TSlot& slot) const
105     {
106         slot->Destroy();
107     }
108 };
109 
110 //---------------------------------------------------------------------------
111 //! @brief        コンテナ要素の全てのスロットを破棄するための関数です。
112 //!
113 //! @tparam       TArray 削除するスロットの配列の型です。
114 //!
115 //! @param[in]    array 削除するスロットの配列です。
116 //---------------------------------------------------------------------------
117 template<typename TArray>
118 NW_INLINE void
119 DestroyAllSlots(
120     TArray& array
121 )
122 {
123     std::for_each(array.begin(), array.end(), SlotDestroyer<typename TArray::value_type>());
124     array.clear();
125 }
126 
127 //@}
128 
129 // ----------------------------------------------------------------------------
130 // 自動生成したファイルをインクルードします。
131 // ----------------------------------------------------------------------------
132 #include <nw/ut/generated/ut_Signal.hi>
133 
134 } // namespace ut
135 } // namespace nw
136 
137 #endif // NW_UT_SIGNAL_H_
138