- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- Search for |

# task_get_result_impl.hpp

Go to the documentation of this file.

    00001 //  Copyright (c) 2005-2009 Hartmut Kaiser
    00002 //
    00003 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
    00004 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    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>    // include the task implementation we want to use
    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                 // if the type we're interested in isn't directly supported, try to
    00036                 // get a std::string assuming it can be converted explicitly
    00037                 std::string* str_retval = saga::detail::any_cast<std::string>(
    00038                     &get_task_result(t));
    00039
    00040                 if (0 != str_retval)
    00041                 {
    00042                     // now try to convert
    00043                     try {
    00044                         // re-assign stored value
    00045                         get_task_result(t).assign(
    00046                             boost::lexical_cast<Retval>(*str_retval));
    00047
    00048                         // re-fetch the sored value
    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                         ;   // just fall through
    00056                     }
    00057                 }
    00058
    00059                 // give up
    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                 // just give up
    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     // implement the get_result functions for the saga::task object
    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();     // must throw
    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();     // must throw
    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