1 // (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
2 
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 //  Authors: Douglas Gregor
8 
9 /** @file request.cpp
10  *
11  *  This file reflects the Boost.MPI @c request class into
12  *  Python.
13  */
14 #include <boost/python.hpp>
15 #include <boost/mpi.hpp>
16 #include "request_with_value.hpp"
17 
18 using namespace boost::python;
19 using namespace boost::mpi;
20 
get_value() const21 const object python::request_with_value::get_value() const
22 {
23   if (m_internal_value.get())
24     return *m_internal_value;
25   else if (m_external_value)
26     return *m_external_value;
27   else
28   {
29     PyErr_SetString(PyExc_ValueError, "request value not available");
30     throw boost::python::error_already_set();
31   }
32 }
33 
get_value_or_none() const34 const object python::request_with_value::get_value_or_none() const
35 {
36   if (m_internal_value.get())
37     return *m_internal_value;
38   else if (m_external_value)
39     return *m_external_value;
40   else
41     return object();
42 }
43 
wrap_wait()44 const object python::request_with_value::wrap_wait()
45 {
46   status stat = request::wait();
47   if (m_internal_value.get() || m_external_value)
48     return boost::python::make_tuple(get_value(), stat);
49   else
50     return object(stat);
51 }
52 
wrap_test()53 const object python::request_with_value::wrap_test()
54 {
55   ::boost::optional<status> stat = request::test();
56   if (stat)
57   {
58     if (m_internal_value.get() || m_external_value)
59       return boost::python::make_tuple(get_value(), *stat);
60     else
61       return object(*stat);
62   }
63   else
64     return object();
65 }
66 
67 
68 namespace boost { namespace mpi { namespace python {
69 
request_test(request & req)70 const object request_test(request &req)
71 {
72   ::boost::optional<status> stat = req.test();
73   if (stat)
74     return object(*stat);
75   else
76     return object();
77 }
78 
79 extern const char* request_docstring;
80 extern const char* request_with_value_docstring;
81 extern const char* request_wait_docstring;
82 extern const char* request_test_docstring;
83 extern const char* request_cancel_docstring;
84 extern const char* request_value_docstring;
85 
export_request()86 void export_request()
87 {
88   using boost::python::arg;
89   using boost::python::object;
90 
91   {
92     typedef request cl;
93     class_<cl>("Request", request_docstring, no_init)
94       .def("wait", &cl::wait, request_wait_docstring)
95       .def("test", &request_test, request_test_docstring)
96       .def("cancel", &cl::cancel, request_cancel_docstring)
97       ;
98   }
99   {
100     typedef request_with_value cl;
101     class_<cl, bases<request> >(
102         "RequestWithValue", request_with_value_docstring, no_init)
103       .def("wait", &cl::wrap_wait, request_wait_docstring)
104       .def("test", &cl::wrap_test, request_test_docstring)
105       ;
106   }
107 
108   implicitly_convertible<request, request_with_value>();
109 }
110 
111 } } } // end namespace boost::mpi::python
112