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.

161 lines
5.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/util/placement_aid.hpp
  3. *
  4. * Purpose: A scoping class to aid in placement new-ing.
  5. *
  6. * Created: 9th 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/placement_aid.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::placement_aid class template
  42. * (\ref group__library__utility "Utility" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_PLACEMENT_AID
  45. #define STLSOFT_INCL_STLSOFT_UTIL_HPP_PLACEMENT_AID
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PLACEMENT_AID_MAJOR 4
  48. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PLACEMENT_AID_MINOR 0
  49. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PLACEMENT_AID_REVISION 2
  50. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PLACEMENT_AID_EDIT 33
  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_NEW
  59. # define STLSOFT_INCL_NEW
  60. # include <new>
  61. #endif /* !STLSOFT_INCL_NEW */
  62. /* /////////////////////////////////////////////////////////////////////////
  63. * Namespace
  64. */
  65. #ifndef _STLSOFT_NO_NAMESPACE
  66. namespace stlsoft
  67. {
  68. #endif /* _STLSOFT_NO_NAMESPACE */
  69. /* /////////////////////////////////////////////////////////////////////////
  70. * Classes
  71. */
  72. // class placement_aid
  73. /** \brief Scopes the lifetime of an object created by placement new
  74. *
  75. * \ingroup group__library__utility
  76. *
  77. *
  78. *
  79. * \param T The type of the object created by placement new
  80. */
  81. template <ss_typename_param_k T>
  82. class placement_aid
  83. {
  84. public:
  85. /// The value type
  86. typedef T value_type;
  87. /// The type of the current parameterisation
  88. typedef placement_aid<T> class_type;
  89. // Construction
  90. public:
  91. /// Create an instance of the \c value_type at the given location
  92. ///
  93. /// \param pv The location at which to in-place construct the object instance
  94. ss_explicit_k placement_aid(void *pv)
  95. : m_t(*new(pv) T())
  96. {}
  97. /// In-place destroy the object instance
  98. ~placement_aid() stlsoft_throw_0()
  99. {
  100. m_t.~T();
  101. }
  102. // Accessors
  103. public:
  104. /// Implicit conversion operator to a reference to the in-place object instance
  105. operator T &()
  106. {
  107. return m_t;
  108. }
  109. /// Implicit conversion operator to a non-mutable (const) reference to the in-place object instance
  110. operator T const& () const
  111. {
  112. return m_t;
  113. }
  114. /// Address-of operator, providing pointer access to the in-place object instance
  115. T *operator &()
  116. {
  117. return &m_t;
  118. }
  119. /// Address-of operator, providing non-mutable (const) pointer access to the in-place object instance
  120. T const* operator &() const
  121. {
  122. return &m_t;
  123. }
  124. // Members
  125. private:
  126. T &m_t;
  127. // Not to be implemented
  128. private:
  129. placement_aid(class_type const&);
  130. class_type const& operator =(class_type const&);
  131. };
  132. /* ////////////////////////////////////////////////////////////////////// */
  133. #ifndef _STLSOFT_NO_NAMESPACE
  134. } // namespace stlsoft
  135. #endif /* _STLSOFT_NO_NAMESPACE */
  136. /* ////////////////////////////////////////////////////////////////////// */
  137. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_PLACEMENT_AID */
  138. /* ///////////////////////////// end of file //////////////////////////// */