Skip to content

Instantly share code, notes, and snippets.

@sithhell
Created April 4, 2016 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sithhell/4e28a1678cd7328b5f89ae7eb82e0022 to your computer and use it in GitHub Desktop.
Save sithhell/4e28a1678cd7328b5f89ae7eb82e0022 to your computer and use it in GitHub Desktop.
// Copyright (c) 2015 Thomas Heller
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef HPX_NAMING_GID_TRAITS_HPP
#define HPX_NAMING_GID_TRAITS_HPP
#include <hpx/util/function.hpp>
#include <hpx/traits/is_gid.hpp>
#include <hpx/traits/future_access.hpp>
#if defined(HPX_HAVE_ID_TYPE_COMPATIBILITY)
#include <hpx/runtime/naming/id_type.hpp>
#endif
namespace hpx { namespace naming {
namespace detail
{
struct get_gid_helper
{
template <typename Gid>
static auto call(Gid const & gid)
-> decltype(gid.get_gid())
{
return gid.get_gid();
}
};
struct get_id_helper
{
static id_type call(id_type const & gid)
{
return gid;
}
template <typename Gid>
static auto call(Gid const & gid)
-> decltype(gid.get_id())
{
return gid.get_id();
}
};
template <typename Gid>
struct keep_id_alive
{
keep_id_alive()
{}
explicit keep_id_alive(Gid const& gid)
: gid_(gid)
{}
void operator()() const {}
typename hpx::util::decay<Gid>::type gid_;
template <typename Archive>
void serialize(Archive & ar, unsigned)
{
ar & gid_;
}
};
struct keep_alive_helper
{
#if defined(HPX_HAVE_ID_TYPE_COMPATIBILITY)
template <typename Gid, typename Future>
static auto call(Gid const & gid, Future & f)
-> typename util::always_void<
decltype(gid.get_management_type())
>::type
{
typedef typename traits::detail::shared_state_ptr_for<
Future
>::type shared_state_ptr;
if (gid.get_management_type() == naming::id_type::managed)
{
shared_state_ptr const& state
= traits::detail::get_shared_state(f);
state->set_on_completed(keep_id_alive<Gid>(gid));
}
}
#endif
template <typename Gid, typename Future>
static auto call(Gid const & gid, Future & f)
-> typename Gid::is_unmanaged
{
}
template <typename Gid, typename Future>
static auto call(Gid const & gid, Future & f)
-> typename Gid::is_managed
{
typedef typename traits::detail::shared_state_ptr_for<
Future
>::type shared_state_ptr;
shared_state_ptr const& state
= traits::detail::get_shared_state(f);
state->set_on_completed(keep_id_alive<Gid>(gid));
}
};
}
template <typename Gid, typename Enable>
struct gid_traits
{
static_assert(is_gid<Gid>::value, "Not a Gid type");
static gid_type get_gid(Gid const & gid)
{
return detail::get_gid_helper::call(gid);
}
static id_type get_id(Gid const & gid)
{
return detail::get_id_helper::call(gid);
}
template <typename Future>
static void keep_alive(Gid const & gid, Future & f)
{
detail::keep_alive_helper::call(gid, f);
}
};
}}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment