helics  2.8.1
data_view.hpp
1 /*
2 Copyright (c) 2017-2021,
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/helics-time.hpp"
11 #include "helics/external/string_view.hpp"
12 #include "helics/helics-config.h"
13 #include "helicsTypes.hpp"
14 
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 namespace helics {
22 class data_view {
23  private:
24  stx::string_view dblock;
25  std::shared_ptr<const data_block>
26  ref;
27  public:
29  data_view() = default;
31  ~data_view() = default;
33  data_view(std::shared_ptr<const data_block> dt):
34  dblock(dt->m_data), ref(std::move(dt)) {} // NOLINT
36  data_view(const data_block& dt) noexcept: dblock(dt.m_data) {} // 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 
43  data_view(const char* dt) noexcept: dblock(dt) {} // NOLINT
45  data_view(const char* dt, size_t len) noexcept: dblock(dt, len) {}
47  data_view(const std::string& str) noexcept: dblock(str) {} // NOLINT
49  data_view(std::string&& str):
50  data_view(std::make_shared<data_block>(std::move(str))) {} // NOLINT
52  data_view(const std::vector<char>& dvec) noexcept:
53  dblock(dvec.data(), dvec.size()) {} // NOLINT
55  data_view(const stx::string_view& sview) noexcept:
56  dblock(sview){}; // NOLINT (intended implicit)
58  data_view& operator=(const data_view& dv) noexcept = default;
59 
60  data_view& operator=(data_view&& dv) noexcept
61  {
62  dblock = dv.dblock;
63  ref = std::move(dv.ref);
64  return *this;
65  }
66 
68  data_view& operator=(std::shared_ptr<const data_block> dt) noexcept
69  {
70  dblock = dt->m_data;
71  ref = std::move(dt);
72  return *this;
73  }
75  data_view& operator=(const data_block& dt) noexcept
76  {
77  dblock = dt.m_data;
78  ref = nullptr;
79  return *this;
80  }
82  data_view& operator=(const stx::string_view& str) noexcept
83  {
84  dblock = str;
85  ref = nullptr;
86  return *this;
87  }
89  data_view& operator=(const char* s) noexcept
90  {
91  dblock = s;
92  ref = nullptr;
93  return *this;
94  }
96  data_block to_data_block() const { return data_block(dblock.data(), dblock.length()); }
99  void swap(data_view& dv2) noexcept
100  {
101  dblock.swap(dv2.dblock);
102  ref.swap(dv2.ref);
103  }
105  const char* data() const noexcept { return dblock.data(); }
107  size_t size() const noexcept { return dblock.length(); }
109  bool empty() const noexcept { return dblock.empty(); }
113  std::string string() const { return dblock.to_string(); }
115  char operator[](int index) const { return dblock[index]; }
117  auto begin() { return dblock.begin(); }
119  auto end() { return dblock.end(); }
121  auto cbegin() const { return dblock.cbegin(); }
123  auto cend() const { return dblock.cend(); }
124 };
125 
126 constexpr auto bvecstr = "block_vector";
127 
128 template<>
129 inline const char* typeNameString<std::vector<data_block>>()
130 {
131  return bvecstr;
132 }
133 
134 } // namespace helics
135 
136 namespace std {
137 template<>
138 inline void swap(helics::data_view& db1, helics::data_view& db2) noexcept
139 {
140  db1.swap(db2);
141 }
142 } // namespace std
helics::data_view::data_view
data_view(std::shared_ptr< const data_block > dt)
Definition: data_view.hpp:33
helics::data_view::data_view
data_view(const std::vector< char > &dvec) noexcept
Definition: data_view.hpp:52
helicsTypes.hpp
helics::data_view::data_view
data_view(const char *dt, size_t len) noexcept
Definition: data_view.hpp:45
helics::data_view::empty
bool empty() const noexcept
Definition: data_view.hpp:109
helics::data_block
Definition: core-data.hpp:31
helics::data_view::data_view
data_view(const char *dt) noexcept
Definition: data_view.hpp:43
helics::data_view::data_view
data_view(const data_block &dt) noexcept
Definition: data_view.hpp:36
helics::data_view
Definition: data_view.hpp:22
helics::data_view::operator=
data_view & operator=(const data_view &dv) noexcept=default
helics::data_view::operator=
data_view & operator=(const char *s) noexcept
Definition: data_view.hpp:89
helics::data_view::string
std::string string() const
Definition: data_view.hpp:113
helics::data_view::~data_view
~data_view()=default
helics::data_view::data_view
data_view(const std::string &str) noexcept
Definition: data_view.hpp:47
helics::data_view::data_view
data_view(data_view &&dv) noexcept
Definition: data_view.hpp:40
helics::data_view::data_view
data_view(std::string &&str)
Definition: data_view.hpp:49
helics::data_view::end
auto end()
Definition: data_view.hpp:119
helics::data_view::operator=
data_view & operator=(std::shared_ptr< const data_block > dt) noexcept
Definition: data_view.hpp:68
helics::data_view::data_view
data_view(const stx::string_view &sview) noexcept
Definition: data_view.hpp:55
helics::data_view::cbegin
auto cbegin() const
Definition: data_view.hpp:121
helics::data_view::operator=
data_view & operator=(const data_block &dt) noexcept
Definition: data_view.hpp:75
helics::data_view::to_data_block
data_block to_data_block() const
Definition: data_view.hpp:96
helics::data_view::data
const char * data() const noexcept
Definition: data_view.hpp:105
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::data_view::size
size_t size() const noexcept
Definition: data_view.hpp:107
helics::data_view::data_view
data_view()=default
helics::data_view::swap
void swap(data_view &dv2) noexcept
Definition: data_view.hpp:99
helics::data_view::operator[]
char operator[](int index) const
Definition: data_view.hpp:115
helics::data_view::cend
auto cend() const
Definition: data_view.hpp:123
helics::data_view::operator=
data_view & operator=(const stx::string_view &str) noexcept
Definition: data_view.hpp:82
helics::data_view::begin
auto begin()
Definition: data_view.hpp:117