• 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

63.64
/examples/quickstart/custom_serialization.cpp
1
//  Copyright (c) 2022 John Sorial
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
// This example is meant for inclusion in the documentation.
8

9
#include <hpx/format.hpp>
10
#include <hpx/future.hpp>
11
#include <hpx/hpx_main.hpp>
12
#include <hpx/include/actions.hpp>
13
#include <hpx/include/runtime.hpp>
14
#include <hpx/include/util.hpp>
15
#include <hpx/serialization.hpp>
16

17
#include <iostream>
18
#include <memory>
19

20
//[point_member_serialization
21
struct point_member_serialization
22
{
23
    int x{0};
24
    int y{0};
25

26
    // Required when defining the serialization function as private
27
    // In this case it isn't
28
    // Provides serialization access to HPX
29
    friend class hpx::serialization::access;
30

31
    // Second argument exists solely for compatibility with boost serialize
32
    // it is NOT processed by HPX in any way.
33
    template <typename Archive>
34
    void serialize(Archive& ar, unsigned int const)
×
35
    {
36
        // clang-format off
37
        ar & x & y;
38
        // clang-format on
39
    }
×
40
};
41

42
// Allow bitwise serialization
43
HPX_IS_BITWISE_SERIALIZABLE(point_member_serialization)
44
//]
45

46
//[rectangle_member_serialization
47
struct rectangle_member_serialization
48
{
49
    point_member_serialization top_left;
50
    point_member_serialization lower_right;
51

52
    template <typename Archive>
53
    void serialize(Archive& ar, unsigned int const)
54
    {
55
        // clang-format off
56
        ar & top_left & lower_right;
57
        // clang-format on
58
    }
59
};
60
//]
61

62
//[rectangle_free
63
struct rectangle_free
64
{
65
    point_member_serialization top_left;
66
    point_member_serialization lower_right;
67
};
68

69
template <typename Archive>
70
void serialize(Archive& ar, rectangle_free& pt, unsigned int const)
×
71
{
72
    // clang-format off
73
    ar & pt.lower_right & pt.top_left;
×
74
    // clang-format on
75
}
×
76
//]
77

78
//[point_class
79
class point_class
80
{
81
public:
82
    point_class(int x, int y)
83
      : x(x)
84
      , y(y)
85
    {
86
    }
87

88
    point_class() = default;
89

90
    [[nodiscard]] int get_x() const noexcept
91
    {
92
        return x;
93
    }
94

95
    [[nodiscard]] int get_y() const noexcept
96
    {
97
        return y;
98
    }
99

100
private:
101
    int x;
102
    int y;
103
};
104

105
template <typename Archive>
106
void load(Archive& ar, point_class& pt, unsigned int const)
107
{
108
    int x, y;
109
    ar >> x >> y;
110
    pt = point_class(x, y);
111
}
112

113
template <typename Archive>
114
void save(Archive& ar, point_class const& pt, unsigned int const)
115
{
116
    ar << pt.get_x() << pt.get_y();
117
}
118

119
// This tells HPX that you have spilt your serialize function into
120
// load and save
121
HPX_SERIALIZATION_SPLIT_FREE(point_class)
122
//]
123

124
//[SendRectangle
125
void send_rectangle_struct(rectangle_free rectangle)
×
126
{
127
    hpx::util::format_to(std::cout,
3✔
128
        "Rectangle(Point(x={1},y={2}),Point(x={3},y={4}))\n",
129
        rectangle.top_left.x, rectangle.top_left.y, rectangle.lower_right.x,
130
        rectangle.lower_right.y);
131
}
×
132
//]
133

134
HPX_PLAIN_ACTION(send_rectangle_struct)
3✔
135

136
//[planet_weight_calculator
137
class planet_weight_calculator
138
{
139
public:
140
    explicit planet_weight_calculator(double g)
141
      : g(g)
3✔
142
    {
143
    }
144

145
    template <class Archive>
146
    friend void save_construct_data(
147
        Archive&, planet_weight_calculator const*, unsigned int);
148

149
    [[nodiscard]] double get_g() const
150
    {
151
        return g;
3✔
152
    }
153

154
private:
155
    // Provides serialization access to HPX
156
    friend class hpx::serialization::access;
157
    template <class Archive>
158
    void serialize(Archive&, unsigned int const)
159
    {
160
        // Serialization will be done in the save_construct_data
161
        // Still needs to be defined
162
    }
163

164
    double g;
165
};
166
//]
167

168
//[save_construct_data
169
template <class Archive>
170
inline void save_construct_data(Archive& ar,
171
    planet_weight_calculator const* weight_calc, unsigned int const)
172
{
173
    ar << weight_calc->g;    // Do all of your serialization here
174
}
175

176
template <class Archive>
177
inline void load_construct_data(
178
    Archive& ar, planet_weight_calculator* weight_calc, unsigned int const)
179
{
180
    double g;
181
    ar >> g;
182

183
    // ::new(ptr) construct new object at given address
184
    hpx::construct_at(weight_calc, g);
185
}
186
//]
187

188
void send_gravity(planet_weight_calculator gravity)
3✔
189
{
190
    std::cout << "gravity.g = " << gravity.get_g() << std::endl;
191
}
3✔
192

193
HPX_PLAIN_ACTION(send_gravity)
3✔
194

195
//[Main
196
int main()
3✔
197
{
198
    // Needs at least two localities to run
199
    // When sending to your current locality, no serialization is done
200
    send_rectangle_struct_action rectangle_action;
201
    auto rectangle = rectangle_free{{0, 0}, {0, 5}};
3✔
202
    hpx::async(rectangle_action, hpx::find_here(), rectangle).get();
6✔
203

204
    send_gravity_action gravityAction;
205
    auto gravity = planet_weight_calculator(9.81);
206

207
    auto remote_localities = hpx::find_remote_localities();
3✔
208
    if (!remote_localities.empty())
3✔
209
    {
210
        hpx::async(gravityAction, remote_localities[0], gravity).get();
6✔
211
    }
212
    else
213
    {
214
        hpx::async(gravityAction, hpx::find_here(), gravity).get();
×
215
    }
216
    return 0;
217
}
3✔
218
//]
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