1 //  Copyright (c) 2007-2018 Hartmut Kaiser
2 //
3 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #if !defined(HPX_RUNTIME_BASENAME_REGISTRATION_AUG_17_2015_0432PM)
7 #define HPX_RUNTIME_BASENAME_REGISTRATION_AUG_17_2015_0432PM
8 
9 #include <hpx/config.hpp>
10 #include <hpx/components_fwd.hpp>
11 
12 #include <hpx/lcos/future.hpp>
13 #include <hpx/runtime/basename_registration_fwd.hpp>
14 #include <hpx/runtime/components/make_client.hpp>
15 #include <hpx/runtime/naming/id_type.hpp>
16 
17 #include <cstddef>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 namespace hpx
23 {
24     ///////////////////////////////////////////////////////////////////////////
25     /// Return all registered clients from all localities from the given base
26     /// name.
27     ///
28     /// This function locates all ids which were registered with the given
29     /// base name. It returns a list of futures representing those ids.
30     ///
31     /// \tparam Client     The client type to return
32     ///
33     /// \param base_name    [in] The base name for which to retrieve the
34     ///                     registered ids.
35     /// \param num_ids      [in] The number of registered ids to expect.
36     ///
37     /// \returns A list of futures representing the ids which were registered
38     ///          using the given base name.
39     ///
40     /// \note   The futures embedded in the returned client objects will become
41     ///         ready even if the event (for instance,
42     ///         binding the name to an id) has already happened in the past.
43     ///         This is important in order to reliably retrieve ids from a
44     ///         name, even if the name was already registered.
45     ///
46     template <typename Client>
47     std::vector<Client>
find_all_from_basename(std::string base_name,std::size_t num_ids)48     find_all_from_basename(std::string base_name, std::size_t num_ids)
49     {
50         return components::make_clients<Client>(
51             find_all_from_basename(std::move(base_name), num_ids));
52     }
53 
54     /// Return registered clients from the given base name and sequence numbers.
55     ///
56     /// This function locates the ids which were registered with the given
57     /// base name and the given sequence numbers. It returns a list of futures
58     /// representing those ids.
59     ///
60     /// \tparam Client     The client type to return
61     ///
62     /// \param base_name    [in] The base name for which to retrieve the
63     ///                     registered ids.
64     /// \param ids          [in] The sequence numbers of the registered ids.
65     ///
66     /// \returns A list of futures representing the ids which were registered
67     ///          using the given base name and sequence numbers.
68     ///
69     /// \note   The futures embedded in the returned client objects will become
70     ///         ready even if the event (for instance,
71     ///         binding the name to an id) has already happened in the past.
72     ///         This is important in order to reliably retrieve ids from a
73     ///         name, even if the name was already registered.
74     ///
75     template <typename Client>
find_from_basename(std::string base_name,std::vector<std::size_t> const & ids)76     std::vector<Client> find_from_basename(std::string base_name,
77         std::vector<std::size_t> const& ids)
78     {
79         return components::make_clients<Client>(
80             find_from_basename(std::move(base_name), ids));
81     }
82 
83     /// \brief Return registered id from the given base name and sequence number.
84     ///
85     /// This function locates the id which was registered with the given
86     /// base name and the given sequence number. It returns a future
87     /// representing those id.
88     ///
89     /// \tparam Client     The client type to return
90     ///
91     /// \param base_name    [in] The base name for which to retrieve the
92     ///                     registered ids.
93     /// \param sequence_nr  [in] The sequence number of the registered id.
94     ///
95     /// \returns A representing the id which was registered using the given
96     ///          base name and sequence numbers.
97     ///
98     /// \note   The future embedded in the returned client object will become
99     ///         ready even if the event (for instance,
100     ///         binding the name to an id) has already happened in the past.
101     ///         This is important in order to reliably retrieve ids from a
102     ///         name, even if the name was already registered.
103     ///
104     template <typename Client>
find_from_basename(std::string base_name,std::size_t sequence_nr)105     Client find_from_basename(std::string base_name, std::size_t sequence_nr)
106     {
107         return components::make_client<Client>(
108             find_from_basename(std::move(base_name), sequence_nr));
109     }
110 
111     /// Register the id wrapped in the given client using the given base name.
112     ///
113     /// The function registers the object the given client refers to using the
114     /// provided base name.
115     ///
116     /// \tparam Client      The client type to register
117     ///
118     /// \param base_name    [in] The base name for which to retrieve the
119     ///                     registered ids.
120     /// \param client       [in] The client which should be registered using
121     ///                     the given base name.
122     /// \param sequence_nr  [in, optional] The sequential number to use for the
123     ///                     registration of the id. This number has to be
124     ///                     unique system wide for each registration using the
125     ///                     same base name. The default is the current locality
126     ///                     identifier. Also, the sequence numbers have to be
127     ///                     consecutive starting from zero.
128     ///
129     /// \returns A future representing the result of the registration operation
130     ///          itself.
131     ///
132     /// \note    The operation will fail if the given sequence number is not
133     ///          unique.
134     ///
135     template <typename Client, typename Stub>
register_with_basename(std::string base_name,components::client_base<Client,Stub> & client,std::size_t sequence_nr)136     hpx::future<bool> register_with_basename(std::string base_name,
137         components::client_base<Client, Stub>& client,
138         std::size_t sequence_nr)
139     {
140         return client.then(
141             [sequence_nr, HPX_CAPTURE_MOVE(base_name)](
142                 components::client_base<Client, Stub> && c
143             ) mutable -> hpx::future<bool>
144             {
145                 return register_with_basename(
146                     std::move(base_name), c.get_id(), sequence_nr);
147             });
148     }
149 
150     /// Unregister the given base name.
151     ///
152     /// The function unregisters the given ids using the provided base name.
153     ///
154     /// \tparam Client      The client type to return
155     ///
156     /// \param base_name    [in] The base name for which to retrieve the
157     ///                     registered ids.
158     /// \param sequence_nr  [in, optional] The sequential number to use for the
159     ///                     un-registration. This number has to be the same
160     ///                     as has been used with \a register_with_basename
161     ///                     before.
162     ///
163     /// \returns A future representing the result of the un-registration
164     ///          operation itself.
165     ///
166     template <typename Client>
unregister_with_basename(std::string base_name,std::size_t sequence_nr)167     Client unregister_with_basename(
168         std::string base_name, std::size_t sequence_nr)
169     {
170         return components::make_client<Client>(
171             unregister_with_basename(std::move(base_name), sequence_nr));
172     }
173 }
174 
175 #endif
176