atl.hpp 17 KB

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