atl.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. #ifndef BOOST_RANGE_ATL_HPP
  2. #define BOOST_RANGE_ATL_HPP
  3. // Boost.Range ATL Extension
  4. //
  5. // Copyright Shunsuke Sogame 2005-2006.
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // config
  10. //
  11. #include <atldef.h> // _ATL_VER
  12. #if !defined(BOOST_RANGE_ATL_NO_COLLECTIONS)
  13. #if (_ATL_VER < 0x0700)
  14. #define BOOST_RANGE_ATL_NO_COLLECTIONS
  15. #endif
  16. #endif
  17. #if !defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLE_XXX)
  18. #if (_ATL_VER < 0x0700) // dubious
  19. #define BOOST_RANGE_ATL_HAS_OLD_CSIMPLE_XXX
  20. #endif
  21. #endif
  22. // forward declarations
  23. //
  24. #include <basetyps.h> // IID
  25. namespace ATL {
  26. #if !defined(BOOST_RANGE_ATL_NO_COLLECTIONS)
  27. // arrays
  28. //
  29. template< class E, class ETraits >
  30. class CAtlArray;
  31. template< class E >
  32. class CAutoPtrArray;
  33. template< class I, const IID *piid >
  34. class CInterfaceArray;
  35. // lists
  36. //
  37. template< class E, class ETraits >
  38. class CAtlList;
  39. template< class E >
  40. class CAutoPtrList;
  41. template< class E, class Allocator >
  42. class CHeapPtrList;
  43. template< class I, const IID *piid >
  44. class CInterfaceList;
  45. // maps
  46. //
  47. template< class K, class V, class KTraits, class VTraits >
  48. class CAtlMap;
  49. template< class K, class V, class KTraits, class VTraits >
  50. class CRBTree;
  51. template< class K, class V, class KTraits, class VTraits >
  52. class CRBMap;
  53. template< class K, class V, class KTraits, class VTraits >
  54. class CRBMultiMap;
  55. // strings
  56. //
  57. #if !defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLESTRING)
  58. template< class BaseType, bool t_bMFCDLL >
  59. class CSimpleStringT;
  60. #else
  61. template< class BaseType >
  62. class CSimpleStringT;
  63. #endif
  64. template< class BaseType, class StringTraits >
  65. class CStringT;
  66. template< class StringType, int t_nChars >
  67. class CFixedStringT;
  68. template< class BaseType, const int t_nSize >
  69. class CStaticString;
  70. #endif // !defined(BOOST_RANGE_ATL_NO_COLLECTIONS)
  71. // simples
  72. //
  73. #if !defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLE_XXX)
  74. template< class T, class TEqual >
  75. class CSimpleArray;
  76. template< class TKey, class TVal, class TEqual >
  77. class CSimpleMap;
  78. #else
  79. template< class T >
  80. class CSimpleArray;
  81. template< class T >
  82. class CSimpleValArray;
  83. template< class TKey, class TVal >
  84. class CSimpleMap;
  85. #endif // !defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLE_XXX)
  86. // pointers
  87. //
  88. template< class E >
  89. class CAutoPtr;
  90. template< class T >
  91. class CComPtr;
  92. template< class T, const IID *piid >
  93. class CComQIPtr;
  94. template< class E, class Allocator >
  95. class CHeapPtr;
  96. template< class T >
  97. class CAdapt;
  98. } // namespace ATL
  99. // indirect_iterator customizations
  100. //
  101. #include <boost/mpl/identity.hpp>
  102. #include <boost/pointee.hpp>
  103. namespace boost {
  104. template< class E >
  105. struct pointee< ATL::CAutoPtr<E> > :
  106. mpl::identity<E>
  107. { };
  108. template< class T >
  109. struct pointee< ATL::CComPtr<T> > :
  110. mpl::identity<T>
  111. { };
  112. template< class T, const IID *piid >
  113. struct pointee< ATL::CComQIPtr<T, piid> > :
  114. mpl::identity<T>
  115. { };
  116. template< class E, class Allocator >
  117. struct pointee< ATL::CHeapPtr<E, Allocator> > :
  118. mpl::identity<E>
  119. { };
  120. template< class T >
  121. struct pointee< ATL::CAdapt<T> > :
  122. pointee<T>
  123. { };
  124. } // namespace boost
  125. // extended customizations
  126. //
  127. #include <boost/iterator/indirect_iterator.hpp>
  128. #include <boost/iterator/zip_iterator.hpp>
  129. #include <boost/range/detail/microsoft.hpp>
  130. #include <boost/tuple/tuple.hpp>
  131. #include <atlbase.h> // CComBSTR
  132. namespace boost { namespace range_detail_microsoft {
  133. #if !defined(BOOST_RANGE_ATL_NO_COLLECTIONS)
  134. // arrays
  135. //
  136. struct atl_array_functions :
  137. array_functions
  138. {
  139. template< class Iterator, class X >
  140. Iterator end(X& x) // redefine
  141. {
  142. return x.GetData() + x.GetCount(); // no 'GetSize()'
  143. }
  144. };
  145. template< class E, class ETraits >
  146. struct customization< ATL::CAtlArray<E, ETraits> > :
  147. atl_array_functions
  148. {
  149. template< class X >
  150. struct meta
  151. {
  152. typedef E val_t;
  153. typedef val_t *mutable_iterator;
  154. typedef val_t const *const_iterator;
  155. };
  156. };
  157. template< class E >
  158. struct customization< ATL::CAutoPtrArray<E> > :
  159. atl_array_functions
  160. {
  161. template< class X >
  162. struct meta
  163. {
  164. // ATL::CAutoPtr/CHeapPtr is no assignable.
  165. typedef ATL::CAutoPtr<E> val_t;
  166. typedef val_t *miter_t;
  167. typedef val_t const *citer_t;
  168. typedef indirect_iterator<miter_t> mutable_iterator;
  169. typedef indirect_iterator<citer_t> const_iterator;
  170. };
  171. };
  172. template< class I, const IID *piid >
  173. struct customization< ATL::CInterfaceArray<I, piid> > :
  174. atl_array_functions
  175. {
  176. template< class X >
  177. struct meta
  178. {
  179. typedef ATL::CComQIPtr<I, piid> val_t;
  180. typedef val_t *mutable_iterator;
  181. typedef val_t const *const_iterator;
  182. };
  183. };
  184. template< class E, class ETraits >
  185. struct customization< ATL::CAtlList<E, ETraits> > :
  186. list_functions
  187. {
  188. template< class X >
  189. struct meta
  190. {
  191. typedef E val_t;
  192. typedef list_iterator<X, val_t> mutable_iterator;
  193. typedef list_iterator<X const, val_t const> const_iterator;
  194. };
  195. };
  196. struct indirected_list_functions
  197. {
  198. template< class Iterator, class X >
  199. Iterator begin(X& x)
  200. {
  201. typedef typename Iterator::base_type base_t; // == list_iterator
  202. return Iterator(base_t(x, x.GetHeadPosition()));
  203. }
  204. template< class Iterator, class X >
  205. Iterator end(X& x)
  206. {
  207. typedef typename Iterator::base_type base_t;
  208. return Iterator(base_t(x, POSITION(0)));
  209. }
  210. };
  211. template< class E >
  212. struct customization< ATL::CAutoPtrList<E> > :
  213. indirected_list_functions
  214. {
  215. template< class X >
  216. struct meta
  217. {
  218. typedef ATL::CAutoPtr<E> val_t;
  219. typedef list_iterator<X, val_t> miter_t;
  220. typedef list_iterator<X const, val_t const> citer_t;
  221. typedef indirect_iterator<miter_t> mutable_iterator;
  222. typedef indirect_iterator<citer_t> const_iterator;
  223. };
  224. };
  225. template< class E, class Allocator >
  226. struct customization< ATL::CHeapPtrList<E, Allocator> > :
  227. indirected_list_functions
  228. {
  229. template< class X >
  230. struct meta
  231. {
  232. typedef ATL::CHeapPtr<E, Allocator> val_t;
  233. typedef list_iterator<X, val_t> miter_t;
  234. typedef list_iterator<X const, val_t const> citer_t;
  235. typedef indirect_iterator<miter_t> mutable_iterator;
  236. typedef indirect_iterator<citer_t> const_iterator;
  237. };
  238. };
  239. template< class I, const IID *piid >
  240. struct customization< ATL::CInterfaceList<I, piid> > :
  241. list_functions
  242. {
  243. template< class X >
  244. struct meta
  245. {
  246. typedef ATL::CComQIPtr<I, piid> val_t;
  247. typedef list_iterator<X, val_t> mutable_iterator;
  248. typedef list_iterator<X const, val_t const> const_iterator;
  249. };
  250. };
  251. // maps
  252. //
  253. struct atl_rb_tree_tag
  254. { };
  255. template< >
  256. struct customization< atl_rb_tree_tag > :
  257. indirected_list_functions
  258. {
  259. template< class X >
  260. struct meta
  261. {
  262. typedef typename X::CPair val_t;
  263. typedef list_iterator<X, val_t *, val_t *> miter_t;
  264. typedef list_iterator<X const, val_t const *, val_t const *> citer_t;
  265. typedef indirect_iterator<miter_t> mutable_iterator;
  266. typedef indirect_iterator<citer_t> const_iterator;
  267. };
  268. };
  269. template< class K, class V, class KTraits, class VTraits >
  270. struct customization< ATL::CAtlMap<K, V, KTraits, VTraits> > :
  271. customization< atl_rb_tree_tag >
  272. {
  273. template< class Iterator, class X >
  274. Iterator begin(X& x) // redefine
  275. {
  276. typedef typename Iterator::base_type base_t; // == list_iterator
  277. return Iterator(base_t(x, x.GetStartPosition())); // no 'GetHeadPosition'
  278. }
  279. };
  280. // strings
  281. //
  282. struct atl_string_tag
  283. { };
  284. template< >
  285. struct customization< atl_string_tag >
  286. {
  287. template< class X >
  288. struct meta
  289. {
  290. typedef typename X::PXSTR mutable_iterator;
  291. typedef typename X::PCXSTR const_iterator;
  292. };
  293. template< class Iterator, class X >
  294. typename mutable_<Iterator, X>::type begin(X& x)
  295. {
  296. return x.GetBuffer(0);
  297. }
  298. template< class Iterator, class X >
  299. Iterator begin(X const& x)
  300. {
  301. return x.GetString();
  302. }
  303. template< class Iterator, class X >
  304. Iterator end(X& x)
  305. {
  306. return begin<Iterator>(x) + x.GetLength();
  307. }
  308. };
  309. template< class BaseType, const int t_nSize >
  310. struct customization< ATL::CStaticString<BaseType, t_nSize> >
  311. {
  312. template< class X >
  313. struct meta
  314. {
  315. typedef BaseType const *mutable_iterator;
  316. typedef mutable_iterator const_iterator;
  317. };
  318. template< class Iterator, class X >
  319. Iterator begin(X const& x)
  320. {
  321. return x;
  322. }
  323. template< class Iterator, class X >
  324. Iterator end(X const& x)
  325. {
  326. return begin<Iterator>(x) + X::GetLength();
  327. }
  328. };
  329. #endif // !defined(BOOST_RANGE_ATL_NO_COLLECTIONS)
  330. template< >
  331. struct customization< ATL::CComBSTR >
  332. {
  333. template< class X >
  334. struct meta
  335. {
  336. typedef OLECHAR *mutable_iterator;
  337. typedef OLECHAR const *const_iterator;
  338. };
  339. template< class Iterator, class X >
  340. Iterator begin(X& x)
  341. {
  342. return x.operator BSTR();
  343. }
  344. template< class Iterator, class X >
  345. Iterator end(X& x)
  346. {
  347. return begin<Iterator>(x) + x.Length();
  348. }
  349. };
  350. // simples
  351. //
  352. #if !defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLE_XXX)
  353. template< class T, class TEqual >
  354. struct customization< ATL::CSimpleArray<T, TEqual> > :
  355. #else
  356. template< class T >
  357. struct customization< ATL::CSimpleArray<T> > :
  358. #endif
  359. array_functions
  360. {
  361. template< class X >
  362. struct meta
  363. {
  364. typedef T val_t;
  365. typedef val_t *mutable_iterator;
  366. typedef val_t const *const_iterator;
  367. };
  368. };
  369. #if defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLE_XXX)
  370. template< class T >
  371. struct customization< ATL::CSimpleValArray<T> > :
  372. array_functions
  373. {
  374. template< class X >
  375. struct meta
  376. {
  377. typedef T val_t;
  378. typedef val_t *mutable_iterator;
  379. typedef val_t const *const_iterator;
  380. };
  381. };
  382. #endif // defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLE_XXX)
  383. #if !defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLE_XXX)
  384. template< class TKey, class TVal, class TEqual >
  385. struct customization< ATL::CSimpleMap<TKey, TVal, TEqual> >
  386. #else
  387. template< class TKey, class TVal >
  388. struct customization< ATL::CSimpleMap<TKey, TVal> >
  389. #endif
  390. {
  391. template< class X >
  392. struct meta
  393. {
  394. typedef TKey k_val_t;
  395. typedef k_val_t *k_miter_t;
  396. typedef k_val_t const *k_citer_t;
  397. typedef TVal v_val_t;
  398. typedef v_val_t *v_miter_t;
  399. typedef v_val_t const *v_citer_t;
  400. // Topic:
  401. // 'std::pair' can't contain references
  402. // because of reference to reference problem.
  403. typedef zip_iterator< tuple<k_miter_t, v_miter_t> > mutable_iterator;
  404. typedef zip_iterator< tuple<k_citer_t, v_citer_t> > const_iterator;
  405. };
  406. template< class Iterator, class X >
  407. Iterator begin(X& x)
  408. {
  409. return Iterator(boost::make_tuple(x.m_aKey, x.m_aVal));
  410. }
  411. template< class Iterator, class X >
  412. Iterator end(X& x)
  413. {
  414. return Iterator(boost::make_tuple(x.m_aKey + x.GetSize(), x.m_aVal + x.GetSize()));
  415. }
  416. };
  417. } } // namespace boost::range_detail_microsoft
  418. // range customizations
  419. //
  420. #if !defined(BOOST_RANGE_ATL_NO_COLLECTIONS)
  421. // arrays
  422. //
  423. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  424. boost::range_detail_microsoft::using_type_as_tag,
  425. (ATL, BOOST_PP_NIL), CAtlArray, 2
  426. )
  427. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  428. boost::range_detail_microsoft::using_type_as_tag,
  429. (ATL, BOOST_PP_NIL), CAutoPtrArray, 1
  430. )
  431. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  432. boost::range_detail_microsoft::using_type_as_tag,
  433. (ATL, BOOST_PP_NIL), CInterfaceArray, (class)(const IID *)
  434. )
  435. // lists
  436. //
  437. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  438. boost::range_detail_microsoft::using_type_as_tag,
  439. (ATL, BOOST_PP_NIL), CAtlList, 2
  440. )
  441. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  442. boost::range_detail_microsoft::using_type_as_tag,
  443. (ATL, BOOST_PP_NIL), CAutoPtrList, 1
  444. )
  445. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  446. boost::range_detail_microsoft::using_type_as_tag,
  447. (ATL, BOOST_PP_NIL), CHeapPtrList, 2
  448. )
  449. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  450. boost::range_detail_microsoft::using_type_as_tag,
  451. (ATL, BOOST_PP_NIL), CInterfaceList, (class)(const IID *)
  452. )
  453. //maps
  454. //
  455. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  456. boost::range_detail_microsoft::using_type_as_tag,
  457. (ATL, BOOST_PP_NIL), CAtlMap, 4
  458. )
  459. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  460. boost::range_detail_microsoft::atl_rb_tree_tag,
  461. (ATL, BOOST_PP_NIL), CRBTree, 4
  462. )
  463. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  464. boost::range_detail_microsoft::atl_rb_tree_tag,
  465. (ATL, BOOST_PP_NIL), CRBMap, 4
  466. )
  467. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  468. boost::range_detail_microsoft::atl_rb_tree_tag,
  469. (ATL, BOOST_PP_NIL), CRBMultiMap, 4
  470. )
  471. // strings
  472. //
  473. #if !defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLESTRING)
  474. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  475. boost::range_detail_microsoft::atl_string_tag,
  476. (ATL, BOOST_PP_NIL), CSimpleStringT, (class)(bool)
  477. )
  478. #else
  479. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  480. boost::range_detail_microsoft::atl_string_tag,
  481. (ATL, BOOST_PP_NIL), CSimpleStringT, 1
  482. )
  483. #endif
  484. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  485. boost::range_detail_microsoft::atl_string_tag,
  486. (ATL, BOOST_PP_NIL), CStringT, 2
  487. )
  488. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  489. boost::range_detail_microsoft::atl_string_tag,
  490. (ATL, BOOST_PP_NIL), CFixedStringT, (class)(int)
  491. )
  492. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  493. boost::range_detail_microsoft::using_type_as_tag,
  494. (ATL, BOOST_PP_NIL), CStaticString, (class)(const int)
  495. )
  496. #endif // !defined(BOOST_RANGE_ATL_NO_COLLECTIONS)
  497. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TYPE(
  498. boost::range_detail_microsoft::using_type_as_tag,
  499. (ATL, BOOST_PP_NIL), CComBSTR
  500. )
  501. // simples
  502. //
  503. #if !defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLE_XXX)
  504. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  505. boost::range_detail_microsoft::using_type_as_tag,
  506. (ATL, BOOST_PP_NIL), CSimpleArray, 2
  507. )
  508. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  509. boost::range_detail_microsoft::using_type_as_tag,
  510. (ATL, BOOST_PP_NIL), CSimpleMap, 3
  511. )
  512. #else
  513. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  514. boost::range_detail_microsoft::using_type_as_tag,
  515. (ATL, BOOST_PP_NIL), CSimpleArray, 1
  516. )
  517. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  518. boost::range_detail_microsoft::using_type_as_tag,
  519. (ATL, BOOST_PP_NIL), CSimpleMap, 2
  520. )
  521. BOOST_RANGE_DETAIL_MICROSOFT_CUSTOMIZATION_TEMPLATE(
  522. boost::range_detail_microsoft::using_type_as_tag,
  523. (ATL, BOOST_PP_NIL), CSimpleValArray, 1
  524. )
  525. #endif // !defined(BOOST_RANGE_ATL_HAS_OLD_CSIMPLE_XXX)
  526. #endif