factory.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef BOOST_SERIALIZATION_FACTORY_HPP
  2. #define BOOST_SERIALIZATION_FACTORY_HPP
  3. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  4. // MS compatible compilers support #pragma once
  5. #if defined(_MSC_VER)
  6. # pragma once
  7. #endif
  8. // factory.hpp: create an instance from an extended_type_info instance.
  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 <cstdarg> // valist
  15. #include <cstddef> // NULL
  16. #include <boost/preprocessor/control/if.hpp>
  17. #include <boost/preprocessor/comparison/greater.hpp>
  18. #include <boost/assert.hpp>
  19. namespace std{
  20. #if defined(__LIBCOMO__)
  21. using ::va_list;
  22. #endif
  23. } // namespace std
  24. namespace boost {
  25. namespace serialization {
  26. // default implementation does nothing.
  27. template<class T, int N>
  28. T * factory(std::va_list){
  29. BOOST_ASSERT(false);
  30. // throw exception here?
  31. return NULL;
  32. }
  33. } // namespace serialization
  34. } // namespace boost
  35. #define BOOST_SERIALIZATION_FACTORY(N, T, A0, A1, A2, A3) \
  36. namespace boost { \
  37. namespace serialization { \
  38. template<> \
  39. T * factory<T, N>(std::va_list ap){ \
  40. BOOST_PP_IF(BOOST_PP_GREATER(N, 0) \
  41. , A0 a0 = va_arg(ap, A0);, BOOST_PP_EMPTY()) \
  42. BOOST_PP_IF(BOOST_PP_GREATER(N, 1) \
  43. , A1 a1 = va_arg(ap, A1);, BOOST_PP_EMPTY()) \
  44. BOOST_PP_IF(BOOST_PP_GREATER(N, 2) \
  45. , A2 a2 = va_arg(ap, A2);, BOOST_PP_EMPTY()) \
  46. BOOST_PP_IF(BOOST_PP_GREATER(N, 3) \
  47. , A3 a3 = va_arg(ap, A3);, BOOST_PP_EMPTY()) \
  48. return new T( \
  49. BOOST_PP_IF(BOOST_PP_GREATER(N, 0) \
  50. , a0, BOOST_PP_EMPTY()) \
  51. BOOST_PP_IF(BOOST_PP_GREATER(N, 1)) \
  52. , BOOST_PP_COMMA, BOOST_PP_EMPTY)() \
  53. BOOST_PP_IF(BOOST_PP_GREATER(N, 1) \
  54. , a1, BOOST_PP_EMPTY()) \
  55. BOOST_PP_IF(BOOST_PP_GREATER(N, 2)) \
  56. , BOOST_PP_COMMA, BOOST_PP_EMPTY)() \
  57. BOOST_PP_IF(BOOST_PP_GREATER(N, 2) \
  58. , a2, BOOST_PP_EMPTY()) \
  59. BOOST_PP_IF(BOOST_PP_GREATER(N, 3)) \
  60. , BOOST_PP_COMMA, BOOST_PP_EMPTY)() \
  61. BOOST_PP_IF(BOOST_PP_GREATER(N, 3) \
  62. , a3, BOOST_PP_EMPTY()) \
  63. ); \
  64. } \
  65. } \
  66. } /**/
  67. #define BOOST_SERIALIZATION_FACTORY_4(T, A0, A1, A2, A3) \
  68. BOOST_SERIALIZATION_FACTORY(4, T, A0, A1, A2, A3)
  69. #define BOOST_SERIALIZATION_FACTORY_3(T, A0, A1, A2) \
  70. BOOST_SERIALIZATION_FACTORY(3, T, A0, A1, A2, 0)
  71. #define BOOST_SERIALIZATION_FACTORY_2(T, A0, A1) \
  72. BOOST_SERIALIZATION_FACTORY(2, T, A0, A1, 0, 0)
  73. #define BOOST_SERIALIZATION_FACTORY_1(T, A0) \
  74. BOOST_SERIALIZATION_FACTORY(1, T, A0, 0, 0, 0)
  75. #define BOOST_SERIALIZATION_FACTORY_0(T) \
  76. namespace boost { \
  77. namespace serialization { \
  78. template<> \
  79. T * factory<T, 0>(std::va_list){ \
  80. return new T(); \
  81. } \
  82. } \
  83. } \
  84. /**/
  85. #endif // BOOST_SERIALIZATION_FACTORY_HPP