iutest  1.17.1.0
iutest_mfc.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_MFC_HPP_43CC925C_BED2_4D9D_8502_78204B69B675_
16 #define INCG_IRIS_IUTEST_MFC_HPP_43CC925C_BED2_4D9D_8502_78204B69B675_
17 
18 //======================================================================
19 // include
20 #include <iterator>
21 
22 namespace iutest {
23 namespace mfc
24 {
25 
29 template<typename T, typename U>
30 class mfc_iterator : public ::std::iterator < ::std::input_iterator_tag, U >
31 {
32  typedef typename iutest_type_traits::add_const_to_pointer<U>::type Item;
33 public:
34  mfc_iterator(const T& container, POSITION pos) : m_container(container), m_pos(pos) {}
35  mfc_iterator(const mfc_iterator& rhs) : m_container(rhs.m_container), m_pos(rhs.m_pos) {}
36 
37  mfc_iterator& operator = (const mfc_iterator& rhs) { m_container = rhs.m_container; m_pos = m_pos; return *this; }
38 
39  mfc_iterator& operator ++ () { advance(); return *this; }
40  mfc_iterator& operator ++ (int) { mfc_iterator r(*this); advance(); return r; }
41 
42  Item operator * () const { return m_container.GetAt(m_pos); }
43  Item operator -> () const { return m_container.GetAt(m_pos); }
44 
45  bool operator == (mfc_iterator& rhs) { return rhs.m_pos == m_pos; }
46  bool operator != (mfc_iterator& rhs) { return rhs.m_pos != m_pos; }
47 private:
48  void advance()
49  {
50  __if_exists(T::GetNext) {
51  m_container.GetNext(m_pos);
52  }
53  __if_not_exists(T::GetNext) {
54  m_pos++;
55  }
56  }
57 private:
58  const T& m_container;
59  POSITION m_pos;
60 };
61 
65 template<typename T, typename Key, typename Value>
66 class mfc_map_iterator : public ::std::iterator < ::std::input_iterator_tag, ::std::pair<Key, Value> >
67 {
68  typedef ::std::pair<Key, Value> Item;
69 public:
70  mfc_map_iterator(const T& container, POSITION pos) : m_container(container), m_pos(pos) {}
71  mfc_map_iterator(const mfc_map_iterator& rhs) : m_container(rhs.m_container), m_pos(rhs.m_pos) {}
72 
73  mfc_map_iterator& operator = (const mfc_map_iterator& rhs) { m_container = rhs.m_container; m_pos = m_pos; return *this; }
74 
75  mfc_map_iterator& operator ++ () { advance(); return *this; }
76  mfc_map_iterator& operator ++ (int) { mfc_iterator r(*this); advance(); return r; }
77 
78  Item operator * () const { return m_pair; }
79  Item operator -> () const { return m_pair; }
80 
81  bool operator == (mfc_map_iterator& rhs) { return rhs.m_pos == m_pos; }
82  bool operator != (mfc_map_iterator& rhs) { return rhs.m_pos != m_pos; }
83 private:
84  void advance()
85  {
86  __if_exists(T::GetNextAssoc) {
87  m_container.GetNextAssoc(m_pos, m_pair.first, m_pair.second);
88  }
89  }
90 
91 private:
92  const T& m_container;
93  POSITION m_pos;
94  ::std::pair<Key, Value> m_pair;
95 };
96 
97 template<typename T, typename U>
98 mfc_iterator<CList<T, U>, T> begin(CList<T, U>& list)
99 {
100  return mfc_iterator<CList<T, U>, T>(list, list.GetHeadPosition());
101 }
102 template<typename T, typename U>
103 mfc_iterator<CList<T, U>, T> end(CList<T, U>& list)
104 {
105  return mfc_iterator<CList<T, U>, T>(list, NULL);
106 }
107 
108 template<typename T, typename U>
109 T* begin(CArray<T, U>& ar)
110 {
111  return ar.GetData();
112 }
113 template<typename T, typename U>
114 T* end(CArray<T, U>& ar)
115 {
116  return ar.GetData() + ar.GetCount();
117 }
118 
119 template<typename T, typename TA, typename U, typename UA>
120 mfc_map_iterator< CMap<T, TA, U, UA>, T, U > begin(CMap<T, TA, U, UA>& map)
121 {
122  return mfc_map_iterator< CMap<T, TA, U, UA>, T, U >(map, map.GetStartPosition());
123 }
124 
125 template<typename T, typename TA, typename U, typename UA>
126 mfc_map_iterator< CMap<T, TA, U, UA>, T, U > end(CMap<T, TA, U, UA>& map)
127 {
128  return mfc_map_iterator< CMap<T, TA, U, UA>, T, U >(map, NULL);
129 }
130 
131 namespace peep
132 {
133 
134 template<typename T>
135 struct base_type : public T
136 {
137  __if_exists(T::BASE_TYPE)
138  {
139  using T::BASE_TYPE;
140  typedef typename T::BASE_TYPE BASE_KEY;
141  typedef typename T::BASE_TYPE BASE_VALUE;
142  }
143 
144  __if_not_exists(T::BASE_TYPE)
145  {
146  __if_exists(T::BASE_KEY)
147  {
148  __if_exists(T::BASE_VALUE)
149  {
150  using T::BASE_KEY;
151  using T::BASE_VALUE;
152  typedef ::std::pair<T::BASE_KEY, T::BASE_VALUE> BASE_TYPE;
153  }
154  }
155  }
156 };
157 template<typename T, typename U>
158 struct base_type< CArray<T, U> >
159 {
160  typedef T BASE_TYPE;
161  typedef T BASE_KEY;
162  typedef T BASE_VALUE;
163 };
164 template<typename BASE_CLASS, typename T>
165 struct base_type< CTypedPtrArray<BASE_CLASS, T> >
166 {
167  typedef T BASE_TYPE;
168  typedef T BASE_KEY;
169  typedef T BASE_VALUE;
170 };
171 template<typename T, typename U>
172 struct base_type< CList<T, U> >
173 {
174  typedef T BASE_TYPE;
175  typedef T BASE_KEY;
176  typedef T BASE_VALUE;
177 };
178 template<typename BASE_CLASS, typename T>
179 struct base_type< CTypedPtrList<BASE_CLASS, T> >
180 {
181  typedef T BASE_TYPE;
182  typedef T BASE_KEY;
183  typedef T BASE_VALUE;
184 };
185 template<typename T, typename TA, typename U, typename UA>
186 struct base_type< CMap<T, TA, U, UA> >
187 {
188  typedef ::std::pair<T, U> BASE_TYPE;
189  typedef T BASE_KEY;
190  typedef U BASE_VALUE;
191 };
192 template<typename BASE_CLASS, typename KEY, typename VALUE>
193 struct base_type< CTypedPtrMap<BASE_CLASS, KEY, VALUE> >
194 {
195  typedef ::std::pair<KEY, VALUE> BASE_TYPE;
196  typedef KEY BASE_KEY;
197  typedef VALUE BASE_VALUE;
198 };
199 
200 template<typename T>
201 struct mfc_iterator_traits
202 {
203  typedef typename base_type<T>::BASE_TYPE BASE_TYPE;
204  typedef typename base_type<T>::BASE_KEY BASE_KEY;
205  typedef typename base_type<T>::BASE_VALUE BASE_VALUE;
206  template<typename U, bool isArray, bool isList>
207  struct type_select
208  {
209  typedef mfc_map_iterator<T, BASE_KEY, BASE_VALUE> type;
210  };
211  template<typename U>
212  struct type_select<U, true, false>
213  {
214  typedef U* type;
215  };
216  template<typename U>
217  struct type_select<U, false, true>
218  {
219  typedef mfc_iterator<T, U> type;
220  };
221  typedef typename type_select<BASE_TYPE
222  , IUTEST_STATIC_EXISTS(T::GetData)
223  , IUTEST_STATIC_EXISTS(T::GetHeadPosition)
224  >::type type;
225 };
226 
227 } // end of namespace peep
228 
229 template<typename T>
230 typename peep::base_type<T>::BASE_TYPE* begin(T& ar
231  , typename detail::enable_if< IUTEST_STATIC_EXISTS(T::GetData), void>::type*& = detail::enabler::value)
232 {
233  return reinterpret_cast<typename peep::base_type<T>::BASE_TYPE*>(ar.GetData());
234 }
235 template<typename T>
236 typename peep::base_type<T>::BASE_TYPE* end(T& ar
237  , typename detail::enable_if< IUTEST_STATIC_EXISTS(T::GetData), void>::type*& = detail::enabler::value)
238 {
239  return reinterpret_cast<typename peep::base_type<T>::BASE_TYPE*>(ar.GetData() + ar.GetCount());
240 }
241 
242 template<typename T>
243 mfc_iterator<T, typename peep::base_type<T>::BASE_TYPE> begin(T& list
244  , typename detail::enable_if< IUTEST_STATIC_EXISTS(T::GetHeadPosition), void>::type*& = detail::enabler::value)
245 {
246  return mfc_iterator<T, peep::base_type<T>::BASE_TYPE>(list, list.GetHeadPosition());
247 }
248 template<typename T>
249 mfc_iterator<T, typename peep::base_type<T>::BASE_TYPE> end(T& list
250  , typename detail::enable_if< IUTEST_STATIC_EXISTS(T::GetHeadPosition), void>::type*& = detail::enabler::value)
251 {
252  return mfc_iterator<T, peep::base_type<T>::BASE_TYPE>(list, NULL);
253 }
254 
255 template<typename T>
256 typename peep::mfc_iterator_traits<T>::type begin(T& map
257  , typename detail::enable_if< IUTEST_STATIC_EXISTS(T::GetStartPosition), void>::type*& = detail::enabler::value)
258 {
259  return typename peep::mfc_iterator_traits<T>::type(map, map.GetStartPosition());
260 }
261 template<typename T>
262 typename peep::mfc_iterator_traits<T>::type end(T& map
263  , typename detail::enable_if< IUTEST_STATIC_EXISTS(T::GetStartPosition), void>::type*& = detail::enabler::value)
264 {
265  return typename peep::mfc_iterator_traits<T>::type(map, NULL);
266 }
267 
271 template<typename T>
272 class CContainer
273 {
274 public:
279  typedef typename peep::base_type<T>::BASE_TYPE BASE_TYPE;
280  typedef typename peep::base_type<T>::BASE_KEY BASE_KEY;
281  typedef typename peep::base_type<T>::BASE_VALUE BASE_VALUE;
286 public:
287  explicit CContainer(T& container) : m_container(container) {}
288 
289  iterator_type begin() const { return mfc::begin(m_container); }
290  iterator_type end() const { return mfc::end(m_container); }
291 
292 private:
293  T& m_container;
294 };
295 
299 template<typename T>
300 CContainer<T> make_container(T& obj) { return CContainer<T>(obj); }
301 
302 } // end of namespace mfc
303 } // end of namespace iutest
304 
305 #endif // INCG_IRIS_IUTEST_MFC_HPP_43CC925C_BED2_4D9D_8502_78204B69B675_
iutest_config.hpp
iris unit test config
iutest::mfc::make_container
CContainer< T > make_container(T &obj)
make mfc container
Definition: iutest_mfc.hpp:301
iutest
iutest root namespace
Definition: iutest_charcode.hpp:31
iutest::mfc::CContainer
provide begin/end in mfc container
Definition: iutest_mfc.hpp:273
iutest::mfc::mfc_map_iterator
mfc map container iterator
Definition: iutest_mfc.hpp:67
iutest::mfc::mfc_iterator
mfc container iterator
Definition: iutest_mfc.hpp:31