stack_constructor.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef BOOST_SERIALIZATION_DETAIL_STACK_CONSTRUCTOR_HPP
  2. #define BOOST_SERIALIZATION_DETAIL_STACK_CONSTRUCTOR_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // stack_constructor.hpp: serialization for loading stl collections
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. #include <boost/aligned_storage.hpp>
  15. #include <boost/serialization/serialization.hpp>
  16. namespace boost{
  17. namespace serialization {
  18. namespace detail {
  19. // reserve space on stack for an object of type T without actually
  20. // construction such an object
  21. template<typename T >
  22. struct stack_allocate
  23. {
  24. T * address() {
  25. return static_cast<T*>(storage_.address());
  26. }
  27. T & reference() {
  28. return * address();
  29. }
  30. private:
  31. typedef typename boost::aligned_storage<
  32. sizeof(T),
  33. boost::alignment_of<T>::value
  34. > type;
  35. type storage_;
  36. };
  37. // construct element on the stack
  38. template<class Archive, class T>
  39. struct stack_construct : public stack_allocate<T>
  40. {
  41. stack_construct(Archive & ar, const unsigned int version){
  42. // note borland emits a no-op without the explicit namespace
  43. boost::serialization::load_construct_data_adl(
  44. ar,
  45. this->address(),
  46. version
  47. );
  48. }
  49. ~stack_construct(){
  50. this->address()->~T(); // undo load_construct_data above
  51. }
  52. };
  53. } // detail
  54. } // serializaition
  55. } // boost
  56. #endif // BOOST_SERIALIZATION_DETAIL_STACH_CONSTRUCTOR_HPP