opaque.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright David Abrahams and Gottfried Ganssauge 2003.
  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. # include <boost/python/return_opaque_pointer.hpp>
  6. # include <boost/python/def.hpp>
  7. # include <boost/python/module.hpp>
  8. # include <boost/python/return_value_policy.hpp>
  9. typedef struct opaque_ *opaque;
  10. typedef struct opaque2_ *opaque2;
  11. opaque the_op = ((opaque) 0x47110815);
  12. opaque2 the_op2 = ((opaque2) 0x08154711);
  13. opaque get() { return the_op; }
  14. void use(opaque op)
  15. {
  16. if (op != the_op)
  17. throw std::runtime_error (std::string ("failed"));
  18. }
  19. int useany(opaque op)
  20. {
  21. return op ? 1 : 0;
  22. }
  23. opaque getnull()
  24. {
  25. return 0;
  26. }
  27. void failuse (opaque op)
  28. {
  29. if (op == the_op)
  30. throw std::runtime_error (std::string ("success"));
  31. }
  32. opaque2 get2 () { return the_op2; }
  33. void use2 (opaque2 op)
  34. {
  35. if (op != the_op2)
  36. throw std::runtime_error (std::string ("failed"));
  37. }
  38. void failuse2 (opaque2 op)
  39. {
  40. if (op == the_op2)
  41. throw std::runtime_error (std::string ("success"));
  42. }
  43. BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(opaque_)
  44. BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(opaque2_)
  45. namespace bpl = boost::python;
  46. BOOST_PYTHON_MODULE(opaque_ext)
  47. {
  48. bpl::def (
  49. "get", &::get, bpl::return_value_policy<bpl::return_opaque_pointer>());
  50. bpl::def ("use", &::use);
  51. bpl::def ("useany", &::useany);
  52. bpl::def ("getnull", &::getnull, bpl::return_value_policy<bpl::return_opaque_pointer>());
  53. bpl::def ("failuse", &::failuse);
  54. bpl::def (
  55. "get2",
  56. &::get2,
  57. bpl::return_value_policy<bpl::return_opaque_pointer>());
  58. bpl::def ("use2", &::use2);
  59. bpl::def ("failuse2", &::failuse2);
  60. }
  61. # include "module_tail.cpp"