instance_holder.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef INSTANCE_HOLDER_DWA2002517_HPP
  6. # define INSTANCE_HOLDER_DWA2002517_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/noncopyable.hpp>
  9. # include <boost/python/type_id.hpp>
  10. # include <cstddef>
  11. namespace boost { namespace python {
  12. // Base class for all holders
  13. struct BOOST_PYTHON_DECL instance_holder : private noncopyable
  14. {
  15. public:
  16. instance_holder();
  17. virtual ~instance_holder();
  18. // return the next holder in a chain
  19. instance_holder* next() const;
  20. // When the derived holder actually holds by [smart] pointer and
  21. // null_ptr_only is set, only report that the type is held when
  22. // the pointer is null. This is needed for proper shared_ptr
  23. // support, to prevent holding shared_ptrs from being found when
  24. // converting from python so that we can use the conversion method
  25. // that always holds the Python object.
  26. virtual void* holds(type_info, bool null_ptr_only) = 0;
  27. void install(PyObject* inst) throw();
  28. // These functions should probably be located elsewhere.
  29. // Allocate storage for an object of the given size at the given
  30. // offset in the Python instance<> object if bytes are available
  31. // there. Otherwise allocate size bytes of heap memory.
  32. static void* allocate(PyObject*, std::size_t offset, std::size_t size);
  33. // Deallocate storage from the heap if it was not carved out of
  34. // the given Python object by allocate(), above.
  35. static void deallocate(PyObject*, void* storage) throw();
  36. private:
  37. instance_holder* m_next;
  38. };
  39. // This macro is needed for implementation of derived holders
  40. # define BOOST_PYTHON_UNFORWARD(N,ignored) (typename unforward<A##N>::type)(a##N)
  41. //
  42. // implementation
  43. //
  44. inline instance_holder* instance_holder::next() const
  45. {
  46. return m_next;
  47. }
  48. }} // namespace boost::python
  49. #endif // INSTANCE_HOLDER_DWA2002517_HPP