53aa4dedf916c11ebd3642abf945468197ed3db7
[platform/upstream/boost.git] / libs / mpi / src / python / py_request.cpp
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
21 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
34 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
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
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
70 extern const char* request_docstring;
71 extern const char* request_with_value_docstring;
72 extern const char* request_wait_docstring;
73 extern const char* request_test_docstring;
74 extern const char* request_cancel_docstring;
75 extern const char* request_value_docstring;
76
77 void export_request()
78 {
79   using boost::python::arg;
80   using boost::python::object;
81   
82   {
83     typedef request cl;
84     class_<cl>("Request", request_docstring, no_init)
85       .def("wait", &cl::wait, request_wait_docstring)
86       .def("test", &cl::test, request_test_docstring)
87       .def("cancel", &cl::cancel, request_cancel_docstring)
88       ;
89   }
90   {
91     typedef request_with_value cl;
92     class_<cl, bases<request> >(
93         "RequestWithValue", request_with_value_docstring, no_init)
94       .def("wait", &cl::wrap_wait, request_wait_docstring)
95       .def("test", &cl::wrap_test, request_test_docstring)
96       ;
97   }
98
99   implicitly_convertible<request, request_with_value>();
100 }
101
102 } } } // end namespace boost::mpi::python