• 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

89.8
/examples/quickstart/component_inheritance.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
//  Copyright (c) 2012 Bryce Adelstein-Lelbach
3
//  Copyright (c) 2013 Hartmut Kaiser
4
//
5
//  SPDX-License-Identifier: BSL-1.0
6
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
7
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
///////////////////////////////////////////////////////////////////////////////
9

10
#include <hpx/config.hpp>
11
#if !defined(HPX_COMPUTE_DEVICE_CODE)
12
#include <hpx/hpx_main.hpp>
13
#include <hpx/include/actions.hpp>
14
#include <hpx/include/components.hpp>
15
#include <hpx/include/runtime.hpp>
16
#include <hpx/iostream.hpp>
17

18
#include <vector>
19

20
///////////////////////////////////////////////////////////////////////////////
21
// Define a base component which exposes the required interface
22
struct A : hpx::components::abstract_component_base<A>
23
{
24
    A()
2✔
25
    {
2✔
26
        hpx::cout << "A::A\n" << std::flush;
2✔
27
    }
2✔
28
    virtual ~A()
2✔
29
    {
2✔
30
        hpx::cout << "A::~A\n" << std::flush;
2✔
31
    }
2✔
32

33
    virtual void print() const = 0;
34

35
    // It is not possible to bind a virtual function to an action, thus we
36
    // bind a simple forwarding function, which is not virtual.
37
    void print_nonvirt() const
2✔
38
    {
39
        print();
2✔
40
    }
2✔
41
    HPX_DEFINE_COMPONENT_ACTION(A, print_nonvirt, print_action)
42

43
    // Overload this function to allow to extract the proper base address
44
    // of the component that is used as part of the generated global id.
45
    hpx::naming::address get_current_address() const override
×
46
    {
47
        return hpx::naming::address(
48
            hpx::naming::get_gid_from_locality_id(hpx::get_locality_id()),
×
49
            hpx::components::get_component_type<A>(), const_cast<A*>(this));
×
50
    }
51
};
52

53
HPX_DEFINE_GET_COMPONENT_TYPE(A)
×
54

55
typedef A::print_action print_action;
56
HPX_REGISTER_ACTION_DECLARATION(print_action)
57
HPX_REGISTER_ACTION(print_action)
×
58

59
///////////////////////////////////////////////////////////////////////////////
60
// Define a component which implements the required interface by deriving from
61
// the base component 'A' defined above.
62
struct B
63
  : A
64
  , hpx::components::component_base<B>
65
{
66
    using wrapping_type = hpx::components::component_base<B>::wrapping_type;
67

68
    using type_holder = B;
69
    using base_type_holder = A;
70

71
    using A::get_current_address;
72

73
    B()
1✔
74
      : value_(0)
1✔
75
    {
76
        hpx::cout << "B::B\n" << std::flush;
1✔
77
    }
1✔
78

79
    B(int i)
1✔
80
      : value_(i)
1✔
81
    {
82
        hpx::cout << "B::B(int) " << i << "\n" << std::flush;
1✔
83
    }
1✔
84

85
    ~B()
2✔
86
    {
2✔
87
        hpx::cout << "B::~B\n" << std::flush;
2✔
88
    }
2✔
89

90
    // Overload this function to allow to extract the proper base address
91
    // of the component that is used as part of the generated global id.
92
    hpx::naming::address get_current_address() const override
2✔
93
    {
94
        return hpx::naming::address(
95
            hpx::naming::get_gid_from_locality_id(hpx::get_locality_id()),
2✔
96
            hpx::components::get_component_type<B>(), const_cast<B*>(this));
2✔
97
    }
98

99
    void print() const override
2✔
100
    {
101
        hpx::cout << "B::print from locality: " << hpx::find_here()
2✔
102
                  << ", value: " << value_ << "\n"
2✔
103
                  << std::flush;
2✔
104
    }
2✔
105

106
    int value_;
107
};
108

109
typedef hpx::components::component<B> server_type;
110
HPX_REGISTER_DERIVED_COMPONENT_FACTORY(server_type, B, "A")
8✔
111

112
///////////////////////////////////////////////////////////////////////////////
113
// Define a client side representation for a remote component instance 'A',
114
// Note: this client has no notion of using 'B', but wraps the base 'A' only.
115
struct client : hpx::components::client_base<client, A>
1✔
116
{
117
    typedef hpx::components::client_base<client, A> base_type;
118

119
    client(hpx::shared_future<hpx::id_type> const& gid)
120
      : base_type(gid)
121
    {
122
    }
123

124
    void print()
2✔
125
    {
126
        print_action act;
127
        act(base_type::get_id());
2✔
128
    }
2✔
129
};
130

131
///////////////////////////////////////////////////////////////////////////////
132
int main()
1✔
133
{
134
    // Use the client class to invoke the print functionality of the compound
135
    // component 'B'.
136
    std::vector<hpx::id_type> localities = hpx::find_all_localities();
1✔
137
    for (hpx::id_type const& id : localities)
2✔
138
    {
139
        client hw1(hpx::components::new_<B>(id));
1✔
140
        hw1.print();
1✔
141

142
        client hw2(hpx::components::new_<B>(id, 1));
1✔
143
        hw2.print();
1✔
144
    }
145

146
    return 0;
147
}
1✔
148

149
#endif
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