| helics 3.6.1

data_view.hpp
1 /*
2 Copyright (c) 2017-2025,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8
9 #include "../core/core-data.hpp"
10 #include "../core/helicsTime.hpp"
11 #include "helics/helics-config.h"
12 #include "helicsTypes.hpp"
13
14 #include <memory>
15 #include <string>
16 #include <string_view>
17 #include <utility>
18 #include <vector>
19
20 namespace helics {
22 class data_view {
23 private:
24 std::string_view dblock;
25 std::shared_ptr<const SmallBuffer>
26 ref;
27 public:
29 data_view() = default;
31 ~data_view() = default;
33 data_view(std::shared_ptr<const SmallBuffer> dt):
34 dblock(dt->to_string()), ref(std::move(dt)) {} // NOLINT
36 data_view(const SmallBuffer& dt) noexcept: dblock(dt.to_string()) {} // NOLINT
38 data_view(const data_view& dt) noexcept = default;
40 data_view(data_view&& dv) noexcept: dblock(dv.dblock), ref(std::move(dv.ref)) {}
41 template<typename U,
42 typename T = std::enable_if_t<std::is_constructible_v<std::string_view, U>>>
43 data_view(U&& u) noexcept: dblock(std::forward<U>(u)) // NOLINT
44 {
45 }
47 data_view(const char* dt, size_t len) noexcept: dblock(dt, len) {}
49 data_view(SmallBuffer&& sb):
50 data_view(std::make_shared<SmallBuffer>(std::move(sb))) {} // NOLINT
52 data_view(const std::vector<char>& dvec) noexcept:
53 dblock(dvec.data(), dvec.size()) {} // NOLINT
54
55 data_view(const std::vector<double>& dvec) noexcept:
56 dblock(reinterpret_cast<const char*>(dvec.data()), dvec.size() * sizeof(double))
57 {
58 } // NOLINT
60 data_view& operator=(const data_view& dv) noexcept = default;
61
62 data_view& operator=(data_view&& dv) noexcept
63 {
64 dblock = dv.dblock;
65 ref = std::move(dv.ref);
66 return *this;
67 }
68
70 data_view& operator=(std::shared_ptr<const SmallBuffer> dt) noexcept
71 {
72 dblock = dt->to_string();
73 ref = std::move(dt);
74 return *this;
75 }
77 data_view& operator=(const SmallBuffer& dt) noexcept
78 {
79 dblock = dt.to_string();
80 ref = nullptr;
81 return *this;
82 }
84 data_view& operator=(const std::string_view& str) noexcept
85 {
86 dblock = str;
87 ref = nullptr;
88 return *this;
89 }
91 data_view& operator=(const char* s) noexcept
92 {
93 dblock = s;
94 ref = nullptr;
95 return *this;
96 }
98 SmallBuffer to_buffer() const { return SmallBuffer(dblock); }
101 void swap(data_view& dv2) noexcept
102 {
103 dblock.swap(dv2.dblock);
104 ref.swap(dv2.ref);
105 }
107 const char* data() const noexcept { return dblock.data(); }
109 const std::byte* bytes() const noexcept
110 {
111 return reinterpret_cast<const std::byte*>(dblock.data());
112 }
114 size_t size() const noexcept { return dblock.length(); }
116 bool empty() const noexcept { return dblock.empty(); }
120 std::string string() const { return std::string(dblock); }
122 std::string_view string_view() const { return dblock; }
124 char operator[](int index) const { return dblock[index]; }
126 auto begin() { return dblock.begin(); }
128 auto end() { return dblock.end(); }
130 auto cbegin() const { return dblock.cbegin(); }
132 auto cend() const { return dblock.cend(); }
133 };
134
135 constexpr auto bvecstr = "block_vector";
136
137 template<>
138 inline const char* typeNameString<std::vector<SmallBuffer>>()
139 {
140 return bvecstr;
141 }
142
143 } // namespace helics
144
145 namespace std {
146 template<>
147 inline void swap(helics::data_view& db1, helics::data_view& db2) noexcept
148 {
149 db1.swap(db2);
150 }
151 } // namespace std
helics::SmallBuffer
Definition: SmallBuffer.hpp:25
helics::data_view
Definition: data_view.hpp:22
helics::data_view::operator=
data_view & operator=(const char *s) noexcept
Definition: data_view.hpp:91
helics::data_view::cend
auto cend() const
Definition: data_view.hpp:132
helics::data_view::string_view
std::string_view string_view() const
Definition: data_view.hpp:122
helics::data_view::size
size_t size() const noexcept
Definition: data_view.hpp:114
helics::data_view::data_view
data_view(const data_view &dt) noexcept=default
helics::data_view::cbegin
auto cbegin() const
Definition: data_view.hpp:130
helics::data_view::data
const char * data() const noexcept
Definition: data_view.hpp:107
helics::data_view::bytes
const std::byte * bytes() const noexcept
Definition: data_view.hpp:109
helics::data_view::operator=
data_view & operator=(const data_view &dv) noexcept=default
helics::data_view::data_view
data_view(const SmallBuffer &dt) noexcept
Definition: data_view.hpp:36
helics::data_view::data_view
data_view(const char *dt, size_t len) noexcept
Definition: data_view.hpp:47
helics::data_view::data_view
data_view(std::shared_ptr< const SmallBuffer > dt)
Definition: data_view.hpp:33
helics::data_view::data_view
data_view(const std::vector< char > &dvec) noexcept
Definition: data_view.hpp:52
helics::data_view::operator=
data_view & operator=(const SmallBuffer &dt) noexcept
Definition: data_view.hpp:77
helics::data_view::operator[]
char operator[](int index) const
Definition: data_view.hpp:124
helics::data_view::swap
void swap(data_view &dv2) noexcept
Definition: data_view.hpp:101
helics::data_view::end
auto end()
Definition: data_view.hpp:128
helics::data_view::empty
bool empty() const noexcept
Definition: data_view.hpp:116
helics::data_view::string
std::string string() const
Definition: data_view.hpp:120
helics::data_view::~data_view
~data_view()=default
helics::data_view::data_view
data_view(SmallBuffer &&sb)
Definition: data_view.hpp:49
helics::data_view::begin
auto begin()
Definition: data_view.hpp:126
helics::data_view::operator=
data_view & operator=(const std::string_view &str) noexcept
Definition: data_view.hpp:84
helics::data_view::data_view
data_view()=default
helics::data_view::operator=
data_view & operator=(std::shared_ptr< const SmallBuffer > dt) noexcept
Definition: data_view.hpp:70
helics::data_view::to_buffer
SmallBuffer to_buffer() const
Definition: data_view.hpp:98
helics::data_view::data_view
data_view(data_view &&dv) noexcept
Definition: data_view.hpp:40
helicsTypes.hpp
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::to_string
std::string to_string(CoreType type)
Definition: typeOperations.cpp:15