dependency_policy.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #ifndef LUABIND_DEPENDENCY_POLICY_HPP_INCLUDED
  20. #define LUABIND_DEPENDENCY_POLICY_HPP_INCLUDED
  21. #include <luabind/config.hpp>
  22. #include <luabind/detail/policy.hpp>
  23. namespace luabind { namespace detail
  24. {
  25. // makes A dependent on B, meaning B will outlive A.
  26. // internally A stores a reference to B
  27. template<int A, int B>
  28. struct dependency_policy
  29. {
  30. static void postcall(lua_State* L, const index_map& indices)
  31. {
  32. int nurse_index = indices[A];
  33. int patient = indices[B];
  34. object_rep* nurse = static_cast<object_rep*>(lua_touserdata(L, nurse_index));
  35. // If the nurse isn't an object_rep, just make this a nop.
  36. if (nurse == 0)
  37. return;
  38. nurse->add_dependency(L, patient);
  39. }
  40. };
  41. }}
  42. #if defined (BOOST_MSVC) && (BOOST_MSVC <= 1200)
  43. namespace luabind
  44. {
  45. // most absurd workaround of all time?
  46. namespace detail
  47. {
  48. template<int N>
  49. struct size_char_array
  50. {
  51. char storage[N + 2];
  52. };
  53. template<int N>
  54. size_char_array<N> deduce_size(LUABIND_PLACEHOLDER_ARG(N));
  55. template<class T>
  56. struct get_index_workaround
  57. {
  58. static T t;
  59. BOOST_STATIC_CONSTANT(int, value = sizeof(deduce_size(t)) - 2);
  60. };
  61. }
  62. template<class A, class B>
  63. detail::policy_cons<detail::dependency_policy<detail::get_index_workaround<A>::value
  64. , detail::get_index_workaround<B>::value>, detail::null_type> dependency(A,B)
  65. {
  66. return detail::policy_cons<detail::dependency_policy<
  67. detail::get_index_workaround<A>::value, detail::get_index_workaround<B>::value>
  68. , detail::null_type>();
  69. }
  70. template<class A>
  71. detail::policy_cons<detail::dependency_policy<0
  72. , detail::get_index_workaround<A>::value>, detail::null_type>
  73. return_internal_reference(A)
  74. {
  75. return detail::policy_cons<detail::dependency_policy<0
  76. , detail::get_index_workaround<A>::value>, detail::null_type>();
  77. }
  78. }
  79. #else
  80. namespace luabind
  81. {
  82. template<int A, int B>
  83. detail::policy_cons<detail::dependency_policy<A, B>, detail::null_type>
  84. dependency(LUABIND_PLACEHOLDER_ARG(A), LUABIND_PLACEHOLDER_ARG(B))
  85. {
  86. return detail::policy_cons<detail::dependency_policy<A, B>, detail::null_type>();
  87. }
  88. template<int A>
  89. detail::policy_cons<detail::dependency_policy<0, A>, detail::null_type>
  90. return_internal_reference(LUABIND_PLACEHOLDER_ARG(A))
  91. {
  92. return detail::policy_cons<detail::dependency_policy<0, A>, detail::null_type>();
  93. }
  94. }
  95. #endif
  96. #endif // LUABIND_DEPENDENCY_POLICY_HPP_INCLUDED