xtensor
Loading...
Searching...
No Matches
xexpression_holder.hpp
1/***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3 * Copyright (c) QuantStack *
4 * *
5 * Distributed under the terms of the BSD 3-Clause License. *
6 * *
7 * The full license is in the file LICENSE, distributed with this software. *
8 ****************************************************************************/
9
10#ifndef XTENSOR_XEXPRESSION_HOLDER_HPP
11#define XTENSOR_XEXPRESSION_HOLDER_HPP
12
13#include <concepts>
14#include <memory>
15#include <type_traits>
16
17#include <nlohmann/json.hpp>
18
19#include "../containers/xarray.hpp"
20#include "../core/xtensor_config.hpp"
21#include "../io/xjson.hpp"
22
23namespace xt
24{
25
26 namespace detail
27 {
28 class xexpression_holder_impl;
29
30 template <class CTE>
31 class xexpression_wrapper;
32 }
33
34 class xexpression_holder // Value semantic
35 {
36 public:
37
38 using implementation_type = detail::xexpression_holder_impl;
39
40 xexpression_holder() = default;
41
42 template <class E>
43 requires(!std::same_as<std::decay_t<E>, xexpression_holder>)
44 xexpression_holder(E&& expr);
45
46 xexpression_holder(implementation_type* holder);
47 xexpression_holder(const xexpression_holder& holder);
48 xexpression_holder(xexpression_holder&& holder);
49
50 xexpression_holder& operator=(const xexpression_holder&);
51 xexpression_holder& operator=(xexpression_holder&&);
52
53 void swap(xexpression_holder&);
54
55 void to_json(nlohmann::json&) const;
56 void from_json(const nlohmann::json&);
57
58 private:
59
60 void init_pointer_from_json(const nlohmann::json&);
61 void check_holder() const;
62
63 std::unique_ptr<implementation_type> p_holder;
64 };
65
66 /*************************************
67 * to_json and from_json declaration *
68 *************************************/
69
71 void to_json(nlohmann::json& j, const xexpression_holder& o);
72 void from_json(const nlohmann::json& j, xexpression_holder& o);
73
75
76 namespace detail
77 {
78 class xexpression_holder_impl // Entity semantic
79 {
80 public:
81
82 xexpression_holder_impl(xexpression_holder_impl&&) = delete;
83
84 xexpression_holder_impl& operator=(const xexpression_holder_impl&) = delete;
85 xexpression_holder_impl& operator=(xexpression_holder_impl&&) = delete;
86
87 virtual xexpression_holder_impl* clone() const = 0;
88 virtual void to_json(nlohmann::json&) const = 0;
89 virtual void from_json(const nlohmann::json&) = 0;
90 virtual ~xexpression_holder_impl() = default;
91
92 protected:
93
94 xexpression_holder_impl() = default;
95 xexpression_holder_impl(const xexpression_holder_impl&) = default;
96 };
97
98 template <class CTE>
99 class xexpression_wrapper : public xexpression_holder_impl
100 {
101 public:
102
103 template <class E>
104 xexpression_wrapper(E&& expr);
105
106 xexpression_wrapper* clone() const;
107
108 void to_json(nlohmann::json&) const;
109 void from_json(const nlohmann::json&);
110
111 ~xexpression_wrapper() = default;
112
113 protected:
114
115 xexpression_wrapper(const xexpression_wrapper&);
116
117 private:
118
119 CTE m_expression;
120 };
121 }
122
123 template <class E>
124 requires(!std::same_as<std::decay_t<E>, xexpression_holder>)
125 inline xexpression_holder::xexpression_holder(E&& expr)
126 : p_holder(new detail::xexpression_wrapper<E>(std::forward<E>(expr)))
127 {
128 }
129
130 inline xexpression_holder::xexpression_holder(implementation_type* holder)
131 : p_holder(holder)
132 {
133 }
134
135 inline xexpression_holder::xexpression_holder(const xexpression_holder& holder)
136 : p_holder(holder.p_holder->clone())
137 {
138 }
139
140 inline xexpression_holder::xexpression_holder(xexpression_holder&& holder)
141 : p_holder(std::move(holder.p_holder))
142 {
143 }
144
145 inline xexpression_holder& xexpression_holder::operator=(const xexpression_holder& holder)
146 {
147 xexpression_holder tmp(holder);
148 swap(tmp);
149 return *this;
150 }
151
152 inline xexpression_holder& xexpression_holder::operator=(xexpression_holder&& holder)
153 {
154 swap(holder);
155 return *this;
156 }
157
158 inline void xexpression_holder::swap(xexpression_holder& holder)
159 {
160 std::swap(p_holder, holder.p_holder);
161 }
162
163 inline void xexpression_holder::to_json(nlohmann::json& j) const
164 {
165 if (p_holder == nullptr)
166 {
167 return;
168 }
169 p_holder->to_json(j);
170 }
171
172 inline void xexpression_holder::from_json(const nlohmann::json& j)
173 {
174 if (!j.is_array())
175 {
176 XTENSOR_THROW(std::runtime_error, "Received a JSON that does not contain a tensor");
177 }
178
179 if (p_holder == nullptr)
180 {
181 init_pointer_from_json(j);
182 }
183 p_holder->from_json(j);
184 }
185
186 inline void xexpression_holder::init_pointer_from_json(const nlohmann::json& j)
187 {
188 if (j.is_array() && !j.empty())
189 {
190 init_pointer_from_json(j[0]);
191 }
192 else if (j.is_number())
193 {
194 xt::xarray<double> empty_arr;
195 p_holder.reset(new detail::xexpression_wrapper<xt::xarray<double>>(std::move(empty_arr)));
196 }
197 else if (j.is_boolean())
198 {
199 xt::xarray<bool> empty_arr;
200 p_holder.reset(new detail::xexpression_wrapper<xt::xarray<bool>>(std::move(empty_arr)));
201 }
202 else if (j.is_string())
203 {
204 xt::xarray<std::string> empty_arr;
205 p_holder.reset(new detail::xexpression_wrapper<xt::xarray<std::string>>(std::move(empty_arr)));
206 }
207 else
208 {
209 XTENSOR_THROW(std::runtime_error, "Received a JSON with a tensor that contains unsupported data type");
210 }
211 }
212
213 inline void xexpression_holder::check_holder() const
214 {
215 if (p_holder == nullptr)
216 {
217 XTENSOR_THROW(std::runtime_error, "The holder does not contain an expression");
218 }
219 }
220
221 /****************************************
222 * to_json and from_json implementation *
223 ****************************************/
224
226 inline void to_json(nlohmann::json& j, const xexpression_holder& o)
227 {
228 o.to_json(j);
229 }
230
231 inline void from_json(const nlohmann::json& j, xexpression_holder& o)
232 {
233 o.from_json(j);
234 }
235
237
238 namespace detail
239 {
240 template <class CTE>
241 template <class E>
242 inline xexpression_wrapper<CTE>::xexpression_wrapper(E&& expr)
243 : xexpression_holder_impl()
244 , m_expression(std::forward<E>(expr))
245 {
246 }
247
248 template <class CTE>
249 inline xexpression_wrapper<CTE>* xexpression_wrapper<CTE>::clone() const
250 {
251 return new xexpression_wrapper<CTE>(*this);
252 }
253
254 template <class CTE>
255 inline void xexpression_wrapper<CTE>::to_json(nlohmann::json& j) const
256 {
257 ::xt::to_json(j, m_expression);
258 }
259
260 template <class CTE>
261 inline void xexpression_wrapper<CTE>::from_json(const nlohmann::json& j)
262 {
263 ::xt::from_json(j, m_expression);
264 }
265
266 template <class CTE>
267 inline xexpression_wrapper<CTE>::xexpression_wrapper(const xexpression_wrapper& wrapper)
268 : xexpression_holder_impl()
269 , m_expression(wrapper.m_expression)
270 {
271 }
272 }
273}
274
275#endif
standard mathematical functions for xexpressions
xarray_container< uvector< T, A >, L, xt::svector< typename uvector< T, A >::size_type, 4, SA, true > > xarray
Alias template on xarray_container with default parameters for data container type and shape / stride...
enable_xcontainer_semantics< E > from_json(const nlohmann::basic_json< M > &, E &)
JSON deserialization of a xtensor expression with a container or a view semantics.
Definition xjson.hpp:156
enable_xexpression< E > to_json(nlohmann::basic_json< M > &, const E &)
JSON serialization of an xtensor expression.
Definition xjson.hpp:133