in_place_interface.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_IN_PLACE_INTERFACE_HPP
  11. #define BOOST_INTERPROCESS_IN_PLACE_INTERFACE_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/interprocess/detail/type_traits.hpp>
  22. #include <boost/container/detail/type_traits.hpp> //alignment_of, aligned_storage
  23. #include <typeinfo> //typeid
  24. //!\file
  25. //!Describes an abstract interface for placement construction and destruction.
  26. namespace boost {
  27. namespace interprocess {
  28. namespace ipcdetail {
  29. struct in_place_interface
  30. {
  31. in_place_interface(std::size_t alignm, std::size_t sz, const char *tname)
  32. : alignment(alignm), size(sz), type_name(tname)
  33. {}
  34. std::size_t alignment;
  35. std::size_t size;
  36. const char *type_name;
  37. virtual void construct_n(void *mem, std::size_t num, std::size_t &constructed) = 0;
  38. virtual void destroy_n(void *mem, std::size_t num, std::size_t &destroyed) = 0;
  39. virtual ~in_place_interface(){}
  40. };
  41. template<class T>
  42. struct placement_destroy : public in_place_interface
  43. {
  44. placement_destroy()
  45. : in_place_interface(::boost::container::dtl::alignment_of<T>::value, sizeof(T), typeid(T).name())
  46. {}
  47. virtual void destroy_n(void *mem, std::size_t num, std::size_t &destroyed)
  48. {
  49. T* memory = static_cast<T*>(mem);
  50. for(destroyed = 0; destroyed < num; ++destroyed)
  51. (memory++)->~T();
  52. }
  53. virtual void construct_n(void *, std::size_t, std::size_t &) {}
  54. private:
  55. void destroy(void *mem)
  56. { static_cast<T*>(mem)->~T(); }
  57. };
  58. }
  59. }
  60. } //namespace boost { namespace interprocess { namespace ipcdetail {
  61. #include <boost/interprocess/detail/config_end.hpp>
  62. #endif //#ifndef BOOST_INTERPROCESS_IN_PLACE_INTERFACE_HPP