00001
00002
00003
00004
00005
00006 #include <utility>
00007 #include <vector>
00008 #include <string>
00009
00010 #include <boost/assert.hpp>
00011 #include <boost/lexical_cast.hpp>
00012
00013 #include <saga/saga/task.hpp>
00014 #include <saga/saga/url.hpp>
00015 #include <saga/saga/util.hpp>
00016
00017 #include <saga/impl/task.hpp>
00018
00019 namespace saga
00020 {
00021 namespace detail
00022 {
00023 template <typename T>
00024 struct disable_reconvert : is_saga_object<T> {};
00025
00026 template <typename T>
00027 struct disable_reconvert<std::vector<T> > : boost::mpl::true_ {};
00028
00029 template <typename Retval>
00030 struct reconvert_result
00031 {
00032 template <typename Task>
00033 static Retval& call(Task& t, boost::mpl::false_)
00034 {
00035
00036
00037 std::string* str_retval = saga::detail::any_cast<std::string>(
00038 &get_task_result(t));
00039
00040 if (0 != str_retval)
00041 {
00042
00043 try {
00044
00045 get_task_result(t).assign(
00046 boost::lexical_cast<Retval>(*str_retval));
00047
00048
00049 Retval* retval = saga::detail::any_cast<Retval>(
00050 &get_task_result(t));
00051 if (0 != retval)
00052 return *retval;
00053 }
00054 catch (boost::bad_lexical_cast const&) {
00055 ;
00056 }
00057 }
00058
00059
00060 SAGA_THROW_VERBATIM(t,
00061 "Wrong data type requested while calling get_result",
00062 saga::NoSuccess);
00063
00064 static Retval static_retval;
00065 return static_retval;
00066 }
00067
00068 template <typename Task>
00069 static Retval& call(Task& t, boost::mpl::true_)
00070 {
00071
00072 SAGA_THROW_VERBATIM(t,
00073 "Wrong data type requested while calling get_result",
00074 saga::NoSuccess);
00075
00076 static Retval static_retval;
00077 return static_retval;
00078 }
00079
00080 template <typename Task>
00081 static Retval& call(Task& t)
00082 {
00083 return call(t, disable_reconvert<Retval>());
00084 }
00085 };
00086 }
00087
00088
00089 template <typename Retval>
00090 Retval& task::get_result()
00091 {
00092 if (saga::task_base::Failed == get_task_if()->get_state())
00093 {
00094 get_task_if()->rethrow();
00095 }
00096
00097 Retval* retval = saga::detail::any_cast<Retval>(
00098 &get_task_result(*this));
00099 if (0 == retval)
00100 return detail::reconvert_result<Retval>::call(*this);
00101
00102 return *retval;
00103 }
00104
00105 template <typename Retval>
00106 Retval const& task::get_result() const
00107 {
00108 if (saga::task_base::Failed == get_task_if()->get_state())
00109 {
00110 get_task_if()->rethrow();
00111 }
00112
00113 Retval const* retval = saga::detail::any_cast<Retval const>(
00114 &get_task_result(*this));
00115 if (0 == retval)
00116 return detail::reconvert_result<Retval>::call(*this);
00117
00118 return *retval;
00119 }
00120 }
00121