• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.47
/libs/full/components_base/src/component_type.cpp
1
//  Copyright (c) 2007-2025 Hartmut Kaiser
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/config.hpp>
8
#include <hpx/components_base/agas_interface.hpp>
9
#include <hpx/components_base/component_type.hpp>
10
#include <hpx/modules/errors.hpp>
11
#include <hpx/modules/functional.hpp>
12
#include <hpx/modules/static_reinit.hpp>
13
#include <hpx/modules/synchronization.hpp>
14
#include <hpx/modules/thread_support.hpp>
15
#include <hpx/naming_base/address.hpp>
16

17
#include <cstdint>
18
#include <map>
19
#include <mutex>
20
#include <string>
21
#include <vector>
22

23
///////////////////////////////////////////////////////////////////////////////
24
namespace hpx::components {
25

26
    namespace detail {
27

28
        struct component_database
29
        {
30
            struct component_entry
31
            {
32
                component_entry() noexcept
33
                  : enabled_(false)
891✔
34
                  , instance_count_(0)
35
                  , deleter_(nullptr)
891✔
36
                {
37
                }
38

39
                // note, this is done to be able to put the entry into the map.
40
                // a component_entry should not be moved otherwise, but this
41
                // saves us a dynamic allocation
42
                component_entry(component_entry&&) noexcept
43
                  : enabled_(false)
891✔
44
                  , instance_count_(0)
45
                  , deleter_(nullptr)
891✔
46
                {
47
                }
48

49
                component_entry& operator=(component_entry&&) noexcept = delete;
50

51
                bool enabled_;
52
                util::atomic_count instance_count_;
53
                component_deleter_type deleter_;
54
            };
55

56
        private:
57
            using mutex_type = hpx::spinlock;
58
            using map_type = std::map<component_type, component_entry>;
59

60
            static mutex_type& mtx()
61
            {
62
                static mutex_type mtx_;
63
                return mtx_;
64
            }
65

66
            static map_type& data()
6,012✔
67
            {
68
                static map_type map;
6,012✔
69
                return map;
6,012✔
70
            }
71

72
        public:
73
            static component_entry& get_entry(component_type type)
6,012✔
74
            {
75
                std::lock_guard l(mtx());
76
                auto& d = data();
6,012✔
77

78
                auto it = d.find(type);
79
                if (it == d.end())
6,012✔
80
                {
81
                    it = d.emplace(type, component_entry()).first;
891✔
82
                }
83

84
                return it->second;
6,012✔
85
            }
86

87
            static bool enumerate_instance_counts(
×
88
                hpx::move_only_function<bool(component_type)> const& f)
89
            {
90
                std::vector<component_type> types;
×
91

92
                {
93
                    std::lock_guard l(mtx());
94
                    types.reserve(data().size());
×
95

96
                    for (auto const& e : data())
×
97
                    {
98
                        types.push_back(e.first);
×
99
                    }
100
                }
101

102
                for (component_type t : types)
×
103
                {
104
                    if (!f(t))
×
105
                        return false;
106
                }
107

108
                return true;
109
            }
110
        };
111
    }    // namespace detail
112

113
    bool& enabled(component_type type)
1,949✔
114
    {
115
        return detail::component_database::get_entry(type).enabled_;
1,949✔
116
    }
117

118
    util::atomic_count& instance_count(component_type type)
2,114✔
119
    {
120
        return detail::component_database::get_entry(type).instance_count_;
2,114✔
121
    }
122

123
    component_deleter_type& deleter(component_type type)
1,949✔
124
    {
125
        return detail::component_database::get_entry(type).deleter_;
1,949✔
126
    }
127

128
    bool enumerate_instance_counts(
×
129
        hpx::move_only_function<bool(component_type)> const& f)
130
    {
131
        return detail::component_database::enumerate_instance_counts(f);
×
132
    }
133

134
    namespace detail {
135

136
        ///////////////////////////////////////////////////////////////////////
137
        // the entries in this array need to be in exactly the same sequence
138
        // as the values defined in the component_type enumerator
139
        char const* const names[] = {
140
            "component_runtime_support",               /*  0 */
141
            "component_plain_function",                /*  1 */
142
            "component_base_lco",                      /*  2 */
143
            "component_base_lco_with_value_unmanaged", /*  3 */
144
            "component_base_lco_with_value",           /*  4 */
145
            "component_latch",                         /*  5 (0x50005) */
146
            "component_barrier",                       /*  6 (0x60006) */
147
            "component_promise",                       /*  7 (0x70004) */
148

149
            "component_agas_locality_namespace",  /*  8 */
150
            "component_agas_primary_namespace",   /*  9 */
151
            "component_agas_component_namespace", /* 10 */
152
            "component_agas_symbol_namespace",    /* 11 */
153
        };
154
    }    // namespace detail
155

156
    // Return the string representation for a given component type id
157
    std::string get_component_type_name(std::int32_t type)
125✔
158
    {
159
        std::string result;
160

161
        if (type == to_int(hpx::components::component_enum_type::invalid))
125✔
162
        {
163
            result = "component_invalid";
164
        }
165
        else if ((type < to_int(hpx::components::component_enum_type::last)) &&
125✔
166
            (get_derived_type(type) == 0))
167
        {
×
168
            result = components::detail::names[type];
169
        }
125✔
170
        else if (get_derived_type(type) <
171
                to_int(hpx::components::component_enum_type::last) &&
172
            (get_derived_type(type) != 0))
34✔
173
        {
174
            result = components::detail::names[get_derived_type(type)];
175
        }
176
        else
177
        {
178
            result = "component";
179
        }
180

181
#if defined(HPX_GCC_VERSION) && HPX_GCC_VERSION >= 110000
182
#pragma GCC diagnostic push
183
#pragma GCC diagnostic ignored "-Wrestrict"
125✔
184
#endif
185
        if (type == get_base_type(type) ||
×
186
            to_int(hpx::components::component_enum_type::invalid) == type)
187
        {
188
            result += "[" + std::to_string(type) + "]";
189
        }
125✔
190
        else
375✔
191
        {
250✔
192
            result += "[" +
193
                std::to_string(static_cast<int>(get_derived_type(type))) + "(" +
194
                std::to_string(static_cast<int>(get_base_type(type))) + ")]";
195
        }
196
#if defined(HPX_GCC_VERSION) && HPX_GCC_VERSION >= 110000
125✔
197
#pragma GCC diagnostic pop
198
#endif
199
        return result;
200
    }
201

859✔
202
    namespace detail {
203

204
        component_type get_agas_component_type(char const* name,
205
            char const* base_name, component_type base_type, bool enabled)
859✔
206
        {
207
            component_type type =
859✔
208
                to_int(hpx::components::component_enum_type::invalid);
859✔
209
            if (enabled)
210
            {
×
211
                type = agas::register_factory(agas::get_locality_id(), name);
212
                if (to_int(hpx::components::component_enum_type::invalid) ==
213
                    type)
214
                {
215
                    HPX_THROW_EXCEPTION(hpx::error::duplicate_component_id,
216
                        "get_agas_component_type",
217
                        "the component name {} is already in use", name);
×
218
                }
219
            }
220
            else
859✔
221
            {
222
                type = agas::get_component_id(name);
223
            }
738✔
224

225
            if (base_name)
1,474✔
226
            {
227
                // NOTE: This assumes that the derived component is loaded.
228
                if (base_type ==
229
                    to_int(hpx::components::component_enum_type::invalid))
230
                {
859✔
231
                    base_type = agas::get_component_id(base_name);
232
                }
233
                type = derived_component_type(type, base_type);
234
            }
235

236
            return type;
237
        }
238
    }    // namespace detail
239
}    // namespace hpx::components
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc