in_place_interface.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/interprocess/detail/type_traits.hpp>
  18. #include <boost/type_traits/alignment_of.hpp>
  19. #include <typeinfo> //typeid
  20. //!\file
  21. //!Describes an abstract interface for placement construction and destruction.
  22. namespace boost {
  23. namespace interprocess {
  24. namespace ipcdetail {
  25. struct in_place_interface
  26. {
  27. in_place_interface(std::size_t alignm, std::size_t sz, const char *tname)
  28. : alignment(alignm), size(sz), type_name(tname)
  29. {}
  30. std::size_t alignment;
  31. std::size_t size;
  32. const char *type_name;
  33. virtual void construct_n(void *mem, std::size_t num, std::size_t &constructed) = 0;
  34. virtual void destroy_n(void *mem, std::size_t num, std::size_t &destroyed) = 0;
  35. virtual ~in_place_interface(){}
  36. };
  37. template<class T>
  38. struct placement_destroy : public in_place_interface
  39. {
  40. placement_destroy()
  41. : in_place_interface(::boost::alignment_of<T>::value, sizeof(T), typeid(T).name())
  42. {}
  43. virtual void destroy_n(void *mem, std::size_t num, std::size_t &destroyed)
  44. {
  45. T* memory = static_cast<T*>(mem);
  46. for(destroyed = 0; destroyed < num; ++destroyed)
  47. (memory++)->~T();
  48. }
  49. virtual void construct_n(void *, std::size_t, std::size_t &) {}
  50. private:
  51. void destroy(void *mem)
  52. { static_cast<T*>(mem)->~T(); }
  53. };
  54. }
  55. }
  56. } //namespace boost { namespace interprocess { namespace ipcdetail {
  57. #include <boost/interprocess/detail/config_end.hpp>
  58. #endif //#ifndef BOOST_INTERPROCESS_IN_PLACE_INTERFACE_HPP