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.

193 lines
6.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/util/pod_veneer.hpp
  3. *
  4. * Purpose: Contains the pod_veneer template class.
  5. *
  6. * Created: 19th January 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/util/pod_veneer.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::pod_veneer class template
  42. * (\ref group__library__utility "Utility" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_POD_VENEER
  45. #define STLSOFT_INCL_STLSOFT_UTIL_HPP_POD_VENEER
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_UTIL_HPP_POD_VENEER_MAJOR 4
  48. # define STLSOFT_VER_STLSOFT_UTIL_HPP_POD_VENEER_MINOR 0
  49. # define STLSOFT_VER_STLSOFT_UTIL_HPP_POD_VENEER_REVISION 1
  50. # define STLSOFT_VER_STLSOFT_UTIL_HPP_POD_VENEER_EDIT 49
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  56. # include <stlsoft/stlsoft.h>
  57. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  58. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS
  59. # include <stlsoft/util/constraints.hpp>
  60. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Namespace
  63. */
  64. #ifndef _STLSOFT_NO_NAMESPACE
  65. namespace stlsoft
  66. {
  67. #endif /* _STLSOFT_NO_NAMESPACE */
  68. /* /////////////////////////////////////////////////////////////////////////
  69. * Classes
  70. */
  71. // class pod_veneer
  72. /** \brief Bolts construction and/or destruction to Plain Old Data types
  73. *
  74. * The parameterisation of this <a href = "http://synesis.com.au/resources/articles/cpp/veneers.pdf">veneer</a>
  75. * can be used to apply RRID or RAII semantics to a plain old data type. For
  76. * example,
  77. *
  78. \code
  79. struct Simple
  80. {
  81. public:
  82. char *buffer;
  83. };
  84. void Init_Simple(Simple *simple);
  85. void Uninit_Simple(Simple *simple);
  86. struct Simple_Init { void operator ()(Simple *simple) { Init_Simple(simple); } };
  87. struct Simple_Uninit { void operator ()(Simple *simple) { Uninit_Simple(simple); } };
  88. void fn()
  89. {
  90. typedef pod_veneer<Simple, Simple_Init, Simple_Uninit> Simple_raii_t;
  91. Simple_raii_t simple; // Init_Simple() called here
  92. . . .
  93. } // Uninit_Simple() called here
  94. \endcode
  95. *
  96. * \param T The POD type
  97. * \param CF The function class applied during construction
  98. * \param DF The function class applied during destruction
  99. *
  100. * \ingroup concepts_veneer
  101. */
  102. template< ss_typename_param_k T
  103. , ss_typename_param_k CF
  104. , ss_typename_param_k DF
  105. >
  106. class pod_veneer
  107. : public T
  108. {
  109. public:
  110. /// The value type
  111. typedef T value_type;
  112. /// The Plain Old Data type
  113. typedef T pod_type;
  114. /// The constructor function class
  115. typedef CF constructor_function_type;
  116. /// The destructor function class
  117. typedef DF destructor_function_type;
  118. /// The type of the current parameterisation
  119. typedef pod_veneer<T, CF, DF> class_type;
  120. public:
  121. /// The constructor, within which CF is applied to \c this
  122. pod_veneer()
  123. {
  124. // It is entirely appropriate to constrain instantiations of this class
  125. // to default construction only, since the whole raison d'etre of this
  126. // template is to bring RAII semantics to POD types.
  127. // Construct the pod
  128. constructor_function_type()(static_cast<pod_type *>(this));
  129. }
  130. /// The destructor, within which DF is applied to \c this
  131. ~pod_veneer() stlsoft_throw_0()
  132. {
  133. // There are four important points to note about the destructor
  134. //
  135. // 1. It does not throw an exception. Therefore, the destroying type
  136. // must not throw an exception in its function call operator
  137. // 2. It uses the must_be_pod constraint to ensure that the pod_type is
  138. // a POD type
  139. // 3. It uses the must_be_same_size constraint to ensure that the
  140. // generated class is the same size as the pod_type, so that arrays of
  141. // the generated class can be used where arrays of the pod_type could be
  142. // used. The runtime assert is included for those compilers that do not
  143. // implement compile-time asserts.
  144. // 4. It is non-virtual, since it should not introduce a vtable (which
  145. // would change the size of the generated class)
  146. // 2. Must be POD
  147. stlsoft_constraint_must_be_pod(pod_type);
  148. // 3. Must use EDO
  149. stlsoft_constraint_must_be_same_size(class_type, pod_type);
  150. #if defined(STLSOFT_COMPILER_IS_WATCOM)
  151. STLSOFT_ASSERT(sizeof(class_type) == sizeof(pod_type));
  152. #else /* ? compiler */
  153. STLSOFT_MESSAGE_ASSERT("pod_veneer used for inappropriate type", sizeof(class_type) == sizeof(pod_type));
  154. #endif /* compiler */
  155. // Destroy the pod
  156. destructor_function_type()(static_cast<pod_type *>(this));
  157. }
  158. };
  159. /* ////////////////////////////////////////////////////////////////////// */
  160. #ifndef _STLSOFT_NO_NAMESPACE
  161. } // namespace stlsoft
  162. #endif /* _STLSOFT_NO_NAMESPACE */
  163. /* ////////////////////////////////////////////////////////////////////// */
  164. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_POD_VENEER */
  165. /* ///////////////////////////// end of file //////////////////////////// */