• 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

42.86
/libs/core/serialization/include/hpx/serialization/array.hpp
1
//  (C) Copyright 2005 Matthias Troyer and Dave Abrahams
2
//  Copyright (c) 2015 Anton Bikineev
3
//  Copyright (c) 2015 Andreas Schaefer
4
//  Copyright (c) 2022-2025 Hartmut Kaiser
5
//
6
//  SPDX-License-Identifier: BSL-1.0
7
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
8
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9

10
#pragma once
11

12
#include <hpx/config.hpp>
13
#include <hpx/assert.hpp>
14
#include <hpx/serialization/detail/allow_zero_copy_receive.hpp>
15
#include <hpx/serialization/serialization_fwd.hpp>
16
#include <hpx/serialization/serialize.hpp>
17
#include <hpx/serialization/traits/is_bitwise_serializable.hpp>
18
#include <hpx/serialization/traits/is_not_bitwise_serializable.hpp>
19

20
#include <array>
21
#include <cstddef>
22
#include <type_traits>
23

24
namespace hpx::serialization {
25

26
    HPX_CXX_EXPORT template <typename T>
27
    class array
28
    {
29
    public:
30
        using value_type = T;
31

32
        constexpr array(value_type* t, std::size_t s) noexcept
33
          : m_t(t)
34
          , m_element_count(s)
35
        {
36
        }
37

×
38
        [[nodiscard]] constexpr value_type* address() const noexcept
×
39
        {
×
40
            return m_t;
41
        }
×
42

43
        [[nodiscard]] constexpr std::size_t count() const noexcept
44
        {
45
            return m_element_count;
46
        }
47

48
        template <typename Archive>
49
        void serialize(Archive& ar, unsigned int)
50
        {
51
#if !defined(HPX_SERIALIZATION_HAVE_ALL_TYPES_ARE_BITWISE_SERIALIZABLE)
52
            if (ar.disable_array_optimization() || ar.endianess_differs())
53
            {
54
                // normal serialization
10✔
55
                for (std::size_t i = 0; i != m_element_count; ++i)
56
                {
57
                    // clang-format off
10✔
58
                    ar & m_t[i];
59
                    // clang-format on
60
                }
×
61
                return;
62
            }
63
#else
×
64
            HPX_ASSERT(
65
                !(ar.disable_array_optimization() || ar.endianess_differs()));
66
#endif
67
            using element_type = std::remove_const_t<T>;
68

69
            constexpr bool use_optimized =
70
                std::is_default_constructible_v<element_type> &&
71
                (hpx::traits::is_bitwise_serializable_v<element_type> ||
72
                    !hpx::traits::is_not_bitwise_serializable_v<element_type>);
73

74
            if constexpr (use_optimized)
75
            {
76
                // try using chunking
77
                if constexpr (std::is_same_v<Archive, input_archive>)
78
                {
79
                    bool allow_zero_copy_receive =
80
                        ar.template try_get_extra_data<
81
                            detail::allow_zero_copy_receive>() != nullptr;
82

83
                    ar.load_binary_chunk(m_t, m_element_count * sizeof(T),
84
                        allow_zero_copy_receive);
4✔
85
                }
86
                else
87
                {
88
                    ar.save_binary_chunk(m_t, m_element_count * sizeof(T));
4✔
89
                }
90
            }
91
            else
92
            {
93
                // normal serialization
10✔
94
                for (std::size_t i = 0; i != m_element_count; ++i)
95
                {
96
                    // clang-format off
97
                    ar & m_t[i];
98
                    // clang-format on
99
                }
100
            }
101
        }
102

103
    private:
104
        value_type* m_t;
105
        std::size_t m_element_count;
106
    };
107

108
    // make_array function
109
    HPX_CXX_EXPORT template <typename T>
110
    HPX_FORCEINLINE constexpr array<T> make_array(
111
        T* begin, std::size_t size) noexcept
112
    {
113
        return array<T>(begin, size);
114
    }
115

116
    // implement serialization for std::array
117
    HPX_CXX_EXPORT template <typename Archive, typename T, std::size_t N>
118
    void serialize(
×
119
        Archive& ar, std::array<T, N>& a, unsigned int const /* version */)
120
    {
121
        // clang-format off
122
        ar & hpx::serialization::make_array(a.data(), a.size());
123
        // clang-format on
124
    }
125

126
    // allow our array to be serialized as prvalue compiler should support good
127
    // ADL implementation, but it is rather for all hpx serialization library
128
    HPX_CXX_EXPORT template <typename T>
129
    HPX_FORCEINLINE output_archive& operator<<(output_archive& ar, array<T> t)
130
    {
131
        ar.save(t);
132
        return ar;
133
    }
134

135
    HPX_CXX_EXPORT template <typename T>
136
    HPX_FORCEINLINE input_archive& operator>>(input_archive& ar, array<T> t)
10✔
137
    {
138
        ar.load(t);
139
        return ar;
140
    }
141

142
    HPX_CXX_EXPORT template <typename T>
143
    HPX_FORCEINLINE output_archive& operator&(    //-V524
×
144
        output_archive& ar, array<T> t)
145
    {
146
        ar.save(t);
147
        return ar;
148
    }
149

150
    HPX_CXX_EXPORT template <typename T>
151
    HPX_FORCEINLINE input_archive& operator&(    //-V524
152
        input_archive& ar, array<T> t)
153
    {
154
        ar.load(t);
155
        return ar;
156
    }
157

158
    // serialize plain arrays:
159
    HPX_CXX_EXPORT template <typename T, std::size_t N>
160
    HPX_FORCEINLINE output_archive& operator<<(output_archive& ar, T (&t)[N])
161
    {
162
        array<T> array = make_array(t, N);
163
        ar.save(array);
164
        return ar;
165
    }
166

167
    HPX_CXX_EXPORT template <typename T, std::size_t N>
168
    HPX_FORCEINLINE input_archive& operator>>(input_archive& ar, T (&t)[N])
169
    {
170
        array<T> array = make_array(t, N);
171
        ar.load(array);
172
        return ar;
173
    }
174

175
    HPX_CXX_EXPORT template <typename T, std::size_t N>
176
    HPX_FORCEINLINE output_archive& operator&(    //-V524
177
        output_archive& ar, T (&t)[N])
178
    {
179
        array<T> array = make_array(t, N);
180
        ar.save(array);
181
        return ar;
182
    }
183

184
    HPX_CXX_EXPORT template <typename T, std::size_t N>
185
    HPX_FORCEINLINE input_archive& operator&(    //-V524
186
        input_archive& ar, T (&t)[N])
187
    {
188
        array<T> array = make_array(t, N);
189
        ar.load(array);
190
        return ar;
191
    }
192
}    // namespace hpx::serialization
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

© 2026 Coveralls, Inc