You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

680 lines
21 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/obsolete/conversion_veneer.hpp
  3. *
  4. * Purpose: Raw conversion veneer class.
  5. *
  6. * Created: 30th July 2002
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /// \file stlsoft/obsolete/conversion_veneer.hpp
  40. ///
  41. /// Raw conversion veneer class.
  42. #ifndef STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_CONVERSION_VENEER
  43. #define STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_CONVERSION_VENEER
  44. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  45. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_CONVERSION_VENEER_MAJOR 3
  46. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_CONVERSION_VENEER_MINOR 2
  47. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_CONVERSION_VENEER_REVISION 2
  48. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_CONVERSION_VENEER_EDIT 47
  49. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  50. /* /////////////////////////////////////////////////////////////////////////
  51. * Includes
  52. */
  53. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  54. # include <stlsoft/stlsoft.h>
  55. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  56. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS
  57. # include <stlsoft/util/constraints.hpp>
  58. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS */
  59. /* /////////////////////////////////////////////////////////////////////////
  60. * Namespace
  61. */
  62. #ifndef _STLSOFT_NO_NAMESPACE
  63. namespace stlsoft
  64. {
  65. #endif /* _STLSOFT_NO_NAMESPACE */
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Classes
  68. */
  69. // class invalid_conversion
  70. /** \brief Prevents any conversion
  71. *
  72. * \ingroup group__library__obsolete
  73. *
  74. * \param T The value type
  75. * \param C The conversion type
  76. */
  77. template< ss_typename_param_k T
  78. , ss_typename_param_k C
  79. >
  80. struct invalid_conversion
  81. {
  82. protected:
  83. /// The invalid type
  84. typedef void invalid_type;
  85. public:
  86. /// The value type
  87. typedef T value_type;
  88. /// The conversion type
  89. typedef C conversion_type;
  90. public:
  91. /// Converts a pointer to the \c value_type to a pointer to the \c conversion_type
  92. static invalid_type convert_pointer(value_type * /* pv */)
  93. {}
  94. /// Converts a pointer-to-const to the \c value_type to a pointer-to-const to the \c conversion_type
  95. static invalid_type convert_const_pointer(value_type const* /* pv */)
  96. {}
  97. /// Converts a reference to the \c value_type to a reference to the \c conversion_type
  98. static invalid_type convert_reference(value_type &/* v */)
  99. {}
  100. /// Converts a reference-to-const to the \c value_type to a reference-to-const to the \c conversion_type
  101. static invalid_type convert_const_reference(value_type const& /* v */)
  102. {}
  103. /// Pointer conversion type
  104. struct pointer_conversion
  105. {
  106. invalid_type operator ()(value_type * /* pv */)
  107. {}
  108. };
  109. /// Pointer-to-const conversion type
  110. struct pointer_const_conversion
  111. {
  112. invalid_type operator ()(value_type const* /* pv */)
  113. {}
  114. };
  115. /// Reference conversion type
  116. struct reference_conversion
  117. {
  118. invalid_type operator ()(value_type &/* v */)
  119. {}
  120. };
  121. /// Reference-to-const conversion type
  122. struct reference_const_conversion
  123. {
  124. invalid_type operator ()(value_type const& /* v */)
  125. {}
  126. };
  127. };
  128. // class static_conversion
  129. /** \brief Implements conversion via C++'s <code>static_cast</code>
  130. *
  131. * \ingroup group__library__obsolete
  132. *
  133. * \param T The value type
  134. * \param C The conversion type
  135. */
  136. template< ss_typename_param_k T
  137. , ss_typename_param_k C
  138. >
  139. struct static_conversion
  140. {
  141. public:
  142. /// The value type
  143. typedef T value_type;
  144. /// The conversion type
  145. typedef C conversion_type;
  146. public:
  147. /// Converts a pointer to the \c value_type to a pointer to the \c conversion_type
  148. static conversion_type *convert_pointer(value_type *pv)
  149. {
  150. return static_cast<conversion_type*>(pv);
  151. }
  152. /// Converts a pointer-to-const to the \c value_type to a pointer-to-const to the \c conversion_type
  153. static conversion_type const* convert_const_pointer(value_type const* pv)
  154. {
  155. return static_cast<conversion_type const*>(pv);
  156. }
  157. /// Converts a reference to the \c value_type to a reference to the \c conversion_type
  158. static conversion_type &convert_reference(value_type &v)
  159. {
  160. return static_cast<conversion_type&>(v);
  161. }
  162. /// Converts a reference-to-const to the \c value_type to a reference-to-const to the \c conversion_type
  163. static conversion_type const& convert_const_reference(value_type const& v)
  164. {
  165. return static_cast<conversion_type const&>(v);
  166. }
  167. /// Pointer conversion type
  168. struct pointer_conversion
  169. {
  170. conversion_type *operator ()(value_type *pv)
  171. {
  172. return convert_pointer(pv);
  173. }
  174. };
  175. /// Pointer-to-const conversion type
  176. struct pointer_const_conversion
  177. {
  178. conversion_type const* operator ()(value_type const* pv)
  179. {
  180. return convert_const_pointer(pv);
  181. }
  182. };
  183. /// Reference conversion type
  184. struct reference_conversion
  185. {
  186. conversion_type& operator ()(value_type &v)
  187. {
  188. return convert_reference(v);
  189. }
  190. };
  191. /// Reference-to-const conversion type
  192. struct reference_const_conversion
  193. {
  194. conversion_type const& operator ()(value_type const& v)
  195. {
  196. return convert_const_reference(v);
  197. }
  198. };
  199. };
  200. // class dynamic_conversion
  201. /** \brief Implements conversion via C++'s <code>dynamic_cast</code>
  202. *
  203. * \ingroup group__library__obsolete
  204. *
  205. * \param T The value type
  206. * \param C The conversion type
  207. */
  208. template< ss_typename_param_k T
  209. , ss_typename_param_k C
  210. >
  211. struct dynamic_conversion
  212. {
  213. public:
  214. /// The value type
  215. typedef T value_type;
  216. /// The conversion type
  217. typedef C conversion_type;
  218. public:
  219. /// Converts a pointer to the \c value_type to a pointer to the \c conversion_type
  220. static conversion_type *convert_pointer(value_type *pv)
  221. {
  222. return dynamic_cast<conversion_type*>(pv);
  223. }
  224. /// Converts a pointer-to-const to the \c value_type to a pointer-to-const to the \c conversion_type
  225. static conversion_type const* convert_const_pointer(value_type const* pv)
  226. {
  227. return dynamic_cast<conversion_type const*>(pv);
  228. }
  229. /// Converts a reference to the \c value_type to a reference to the \c conversion_type
  230. static conversion_type &convert_reference(value_type &v)
  231. {
  232. return dynamic_cast<conversion_type&>(v);
  233. }
  234. /// Converts a reference-to-const to the \c value_type to a reference-to-const to the \c conversion_type
  235. static conversion_type const& convert_const_reference(value_type const& v)
  236. {
  237. return dynamic_cast<conversion_type const&>(v);
  238. }
  239. /// Pointer conversion type
  240. struct pointer_conversion
  241. {
  242. conversion_type *operator ()(value_type *pv)
  243. {
  244. return convert_pointer(pv);
  245. }
  246. };
  247. /// Pointer-to-const conversion type
  248. struct pointer_const_conversion
  249. {
  250. conversion_type const* operator ()(value_type const* pv)
  251. {
  252. return convert_const_pointer(pv);
  253. }
  254. };
  255. /// Reference conversion type
  256. struct reference_conversion
  257. {
  258. conversion_type& operator ()(value_type &v)
  259. {
  260. return convert_reference(v);
  261. }
  262. };
  263. /// Reference-to-const conversion type
  264. struct reference_const_conversion
  265. {
  266. conversion_type const& operator ()(value_type const& v)
  267. {
  268. return convert_const_reference(v);
  269. }
  270. };
  271. };
  272. // class reinterpret_conversion
  273. /** \brief Implements conversion via C++'s <code>reinterpret_cast</code>
  274. *
  275. * \ingroup group__library__obsolete
  276. *
  277. * \param T The value type
  278. * \param C The conversion type
  279. */
  280. template< ss_typename_param_k T
  281. , ss_typename_param_k C
  282. >
  283. struct reinterpret_conversion
  284. {
  285. public:
  286. /// The value type
  287. typedef T value_type;
  288. /// The conversion type
  289. typedef C conversion_type;
  290. public:
  291. /// Converts a pointer to the \c value_type to a pointer to the \c conversion_type
  292. static conversion_type *convert_pointer(value_type *pv)
  293. {
  294. return reinterpret_cast<conversion_type*>(pv);
  295. }
  296. /// Converts a pointer-to-const to the \c value_type to a pointer-to-const to the \c conversion_type
  297. static conversion_type const* convert_const_pointer(value_type const* pv)
  298. {
  299. return reinterpret_cast<conversion_type const*>(pv);
  300. }
  301. /// Converts a reference to the \c value_type to a reference to the \c conversion_type
  302. static conversion_type &convert_reference(value_type &v)
  303. {
  304. return reinterpret_cast<conversion_type&>(v);
  305. }
  306. /// Converts a reference-to-const to the \c value_type to a reference-to-const to the \c conversion_type
  307. static conversion_type const& convert_const_reference(value_type const& v)
  308. {
  309. return reinterpret_cast<conversion_type const&>(v);
  310. }
  311. /// Pointer conversion type
  312. struct pointer_conversion
  313. {
  314. conversion_type *operator ()(value_type *pv)
  315. {
  316. return convert_pointer(pv);
  317. }
  318. };
  319. /// Pointer-to-const conversion type
  320. struct pointer_const_conversion
  321. {
  322. conversion_type const* operator ()(value_type const* pv)
  323. {
  324. return convert_const_pointer(pv);
  325. }
  326. };
  327. /// Reference conversion type
  328. struct reference_conversion
  329. {
  330. conversion_type& operator ()(value_type &v)
  331. {
  332. return convert_reference(v);
  333. }
  334. };
  335. /// Reference-to-const conversion type
  336. struct reference_const_conversion
  337. {
  338. conversion_type const& operator ()(value_type const& v)
  339. {
  340. return convert_const_reference(v);
  341. }
  342. };
  343. };
  344. // class c_conversion
  345. /** \brief Implements conversion via C-style casts
  346. *
  347. * \ingroup group__library__obsolete
  348. *
  349. * \param T The value type
  350. * \param C The conversion type
  351. */
  352. template< ss_typename_param_k T
  353. , ss_typename_param_k C
  354. >
  355. struct c_conversion
  356. {
  357. public:
  358. /// The value type
  359. typedef T value_type;
  360. /// The conversion type
  361. typedef C conversion_type;
  362. public:
  363. /// Converts a pointer to the \c value_type to a pointer to the \c conversion_type
  364. static conversion_type *convert_pointer(value_type *pv)
  365. {
  366. return (conversion_type*)(pv);
  367. }
  368. /// Converts a pointer-to-const to the \c value_type to a pointer-to-const to the \c conversion_type
  369. static conversion_type const* convert_const_pointer(value_type const* pv)
  370. {
  371. return (conversion_type const*)(pv);
  372. }
  373. /// Converts a reference to the \c value_type to a reference to the \c conversion_type
  374. static conversion_type &convert_reference(value_type &v)
  375. {
  376. return (conversion_type&)(v);
  377. }
  378. /// Converts a reference-to-const to the \c value_type to a reference-to-const to the \c conversion_type
  379. static conversion_type const& convert_const_reference(value_type const& v)
  380. {
  381. return (conversion_type const&)(v);
  382. }
  383. /// Pointer conversion type
  384. struct pointer_conversion
  385. {
  386. conversion_type *operator ()(value_type *pv)
  387. {
  388. return convert_pointer(pv);
  389. }
  390. };
  391. /// Pointer-to-const conversion type
  392. struct pointer_const_conversion
  393. {
  394. conversion_type const* operator ()(value_type const* pv)
  395. {
  396. return convert_const_pointer(pv);
  397. }
  398. };
  399. /// Reference conversion type
  400. struct reference_conversion
  401. {
  402. conversion_type& operator ()(value_type &v)
  403. {
  404. return convert_reference(v);
  405. }
  406. };
  407. /// Reference-to-const conversion type
  408. struct reference_const_conversion
  409. {
  410. conversion_type const& operator ()(value_type const& v)
  411. {
  412. return convert_const_reference(v);
  413. }
  414. };
  415. };
  416. // class conversion_veneer
  417. /** \brief This class allows policy-based control of the four conversions: pointer, non-mutable pointer, reference, non-mutable reference
  418. *
  419. * \param T The type that will be subjected to the <a href = "http://synesis.com.au/resources/articles/cpp/veneers.pdf">veneer</a>
  420. * \param C The type that T will be converted to
  421. * \param V The value type. On translators that support default template arguments this defaults to T.
  422. * \param P The type that controls the pointer conversion
  423. * \param R The type that controls the reference conversion
  424. * \param PC The type that controls the pointer-to-const conversion
  425. * \param RC The type that controls the reference-to-const conversion
  426. *
  427. * \ingroup concepts_veneer
  428. */
  429. template< ss_typename_param_k T
  430. , ss_typename_param_k C
  431. #ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_FUNDAMENTAL_ARGUMENT_SUPPORT
  432. , ss_typename_param_k V = T
  433. , ss_typename_param_k P = invalid_conversion<T, C>
  434. , ss_typename_param_k R = invalid_conversion<T, C>
  435. , ss_typename_param_k PC = P
  436. , ss_typename_param_k RC = R
  437. #else
  438. , ss_typename_param_k V
  439. , ss_typename_param_k P
  440. , ss_typename_param_k R
  441. , ss_typename_param_k PC
  442. , ss_typename_param_k RC
  443. #endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_FUNDAMENTAL_ARGUMENT_SUPPORT */
  444. >
  445. class conversion_veneer
  446. : public T
  447. {
  448. public:
  449. /// The parent class type
  450. typedef T parent_class_type;
  451. /// The conversion type
  452. typedef C conversion_type;
  453. /// The value type
  454. typedef V value_type;
  455. /// The pointer conversion type
  456. typedef ss_typename_type_k P::pointer_conversion pointer_conversion_type;
  457. /// The reference conversion type
  458. typedef ss_typename_type_k R::reference_conversion reference_conversion_type;
  459. /// The pointer-to-const conversion type
  460. typedef ss_typename_type_k PC::pointer_const_conversion pointer_const_conversion_type;
  461. /// The reference-to-const conversion type
  462. typedef ss_typename_type_k RC::reference_const_conversion reference_const_conversion_type;
  463. /// The current parameterisation of the type
  464. typedef conversion_veneer<T, C, V, P, R, PC, RC> class_type;
  465. // Construction
  466. public:
  467. /// The default constructor
  468. conversion_veneer()
  469. {
  470. stlsoft_constraint_must_be_same_size(T, class_type);
  471. }
  472. /// The copy constructor
  473. conversion_veneer(class_type const& rhs)
  474. : parent_class_type(rhs)
  475. {
  476. stlsoft_constraint_must_be_same_size(T, class_type);
  477. }
  478. /// Initialise from a value
  479. conversion_veneer(value_type const& rhs)
  480. : parent_class_type(rhs)
  481. {
  482. stlsoft_constraint_must_be_same_size(T, class_type);
  483. }
  484. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
  485. // For compilers that support member templates, the following constructors
  486. // are provided.
  487. /// Single parameter constructor
  488. template <ss_typename_param_k N1>
  489. ss_explicit_k conversion_veneer(N1 &n1)
  490. : parent_class_type(n1)
  491. {}
  492. /// Single parameter constructor
  493. template <ss_typename_param_k N1>
  494. ss_explicit_k conversion_veneer(N1 *n1)
  495. : parent_class_type(n1)
  496. {}
  497. /// Two parameter constructor
  498. template <ss_typename_param_k N1, ss_typename_param_k N2>
  499. conversion_veneer(N1 n1, N2 n2)
  500. : parent_class_type(n1, n2)
  501. {}
  502. /// Three parameter constructor
  503. template <ss_typename_param_k N1, ss_typename_param_k N2, ss_typename_param_k N3>
  504. conversion_veneer(N1 n1, N2 n2, N3 n3)
  505. : parent_class_type(n1, n2, n3)
  506. {}
  507. /// Four parameter constructor
  508. template <ss_typename_param_k N1, ss_typename_param_k N2, ss_typename_param_k N3, ss_typename_param_k N4>
  509. conversion_veneer(N1 n1, N2 n2, N3 n3, N4 n4)
  510. : parent_class_type(n1, n2, n3, n4)
  511. {}
  512. /// Five parameter constructor
  513. template <ss_typename_param_k N1, ss_typename_param_k N2, ss_typename_param_k N3, ss_typename_param_k N4, ss_typename_param_k N5>
  514. conversion_veneer(N1 n1, N2 n2, N3 n3, N4 n4, N5 n5)
  515. : parent_class_type(n1, n2, n3, n4, n5)
  516. {}
  517. /// Six parameter constructor
  518. template <ss_typename_param_k N1, ss_typename_param_k N2, ss_typename_param_k N3, ss_typename_param_k N4, ss_typename_param_k N5, ss_typename_param_k N6>
  519. conversion_veneer(N1 n1, N2 n2, N3 n3, N4 n4, N5 n5, N6 n6)
  520. : parent_class_type(n1, n2, n3, n4, n5, n6)
  521. {}
  522. /// Seven parameter constructor
  523. template <ss_typename_param_k N1, ss_typename_param_k N2, ss_typename_param_k N3, ss_typename_param_k N4, ss_typename_param_k N5, ss_typename_param_k N6, ss_typename_param_k N7>
  524. conversion_veneer(N1 n1, N2 n2, N3 n3, N4 n4, N5 n5, N6 n6, N7 n7)
  525. : parent_class_type(n1, n2, n3, n4, n5, n6, n7)
  526. {}
  527. /// Eight parameter constructor
  528. template <ss_typename_param_k N1, ss_typename_param_k N2, ss_typename_param_k N3, ss_typename_param_k N4, ss_typename_param_k N5, ss_typename_param_k N6, ss_typename_param_k N7, ss_typename_param_k N8>
  529. conversion_veneer(N1 n1, N2 n2, N3 n3, N4 n4, N5 n5, N6 n6, N7 n7, N8 n8)
  530. : parent_class_type(n1, n2, n3, n4, n5, n6, n7, n8)
  531. {}
  532. #endif // STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
  533. /// Copy assignment operator
  534. class_type& operator =(class_type const& rhs)
  535. {
  536. static_cast<parent_class_type&>(*this) = rhs;
  537. return *this;
  538. }
  539. /// Copy from a value
  540. class_type& operator =(value_type const& rhs)
  541. {
  542. static_cast<parent_class_type&>(*this) = rhs;
  543. return *this;
  544. }
  545. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  546. /// Copy from a value
  547. template <ss_typename_param_k T1>
  548. class_type& operator =(T1 rhs)
  549. {
  550. static_cast<parent_class_type&>(*this) = rhs;
  551. return *this;
  552. }
  553. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  554. // Note that the copy constructor is not defined, and will NOT be defined copy ctor/operator not made
  555. // Conversions
  556. public:
  557. /// Implicit conversion to a reference to the conversion_type
  558. operator conversion_type &()
  559. {
  560. return reference_conversion_type()(*this);
  561. }
  562. /// Implicit conversion to a reference-to-const to the conversion_type
  563. operator conversion_type const& () const
  564. {
  565. return reference_const_conversion_type()(*this);
  566. }
  567. /// Address-of operator, returning a pointer to the conversion type
  568. conversion_type * operator &()
  569. {
  570. // Take a local reference, such that the application of the address-of
  571. // operator will allow user-defined conversions of the parent_class_type
  572. // to be applied.
  573. parent_class_type &_this = *this;
  574. return pointer_conversion_type()(&_this);
  575. }
  576. /// Address-of operator, returning a pointer-to-const to the conversion type
  577. conversion_type const* operator &() const
  578. {
  579. // Take a local reference, such that the application of the address-of
  580. // operator will allow user-defined conversions of the parent_class_type
  581. // to be applied.
  582. parent_class_type const& _this = *this;
  583. return pointer_const_conversion_type()(&_this);
  584. }
  585. };
  586. /* ////////////////////////////////////////////////////////////////////// */
  587. #ifndef _STLSOFT_NO_NAMESPACE
  588. } // namespace stlsoft
  589. #endif /* _STLSOFT_NO_NAMESPACE */
  590. /* ////////////////////////////////////////////////////////////////////// */
  591. #endif /* !STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_CONVERSION_VENEER */
  592. /* ///////////////////////////// end of file //////////////////////////// */