xtensor
Loading...
Searching...
No Matches
xinfo.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_INFO_HPP
11#define XTENSOR_INFO_HPP
12
13#include <string>
14
15#include "xlayout.hpp"
16
17#ifndef _MSC_VER
18#if __cplusplus < 201103
19#define CONSTEXPR11_TN
20#define CONSTEXPR14_TN
21#define NOEXCEPT_TN
22#elif __cplusplus < 201402
23#define CONSTEXPR11_TN constexpr
24#define CONSTEXPR14_TN
25#define NOEXCEPT_TN noexcept
26#else
27#define CONSTEXPR11_TN constexpr
28#define CONSTEXPR14_TN constexpr
29#define NOEXCEPT_TN noexcept
30#endif
31#else // _MSC_VER
32#if _MSC_VER < 1900
33#define CONSTEXPR11_TN
34#define CONSTEXPR14_TN
35#define NOEXCEPT_TN
36#elif _MSC_VER < 2000
37#define CONSTEXPR11_TN constexpr
38#define CONSTEXPR14_TN
39#define NOEXCEPT_TN noexcept
40#else
41#define CONSTEXPR11_TN constexpr
42#define CONSTEXPR14_TN constexpr
43#define NOEXCEPT_TN noexcept
44#endif
45#endif
46
47namespace xt
48{
49 // see http://stackoverflow.com/a/20170989
51 {
52 template <std::size_t N>
53 explicit CONSTEXPR11_TN static_string(const char (&a)[N]) NOEXCEPT_TN : data(a),
54 size(N - 1)
55 {
56 }
57
58 CONSTEXPR11_TN static_string(const char* a, const std::size_t sz) NOEXCEPT_TN : data(a),
59 size(sz)
60 {
61 }
62
63 const char* const data;
64 const std::size_t size;
65 };
66
67 template <class T>
68 CONSTEXPR14_TN static_string type_name()
69 {
70#ifdef __clang__
72 return static_string(p.data + 39, p.size - 39 - 1);
73#elif defined(__GNUC__)
75#if __cplusplus < 201402
76 return static_string(p.data + 36, p.size - 36 - 1);
77#else
78 return static_string(p.data + 54, p.size - 54 - 1);
79#endif
80#elif defined(_MSC_VER)
81 static const static_string p(__FUNCSIG__);
82 return static_string(p.data + 47, p.size - 47 - 7);
83#endif
84 }
85
86 template <class T>
87 std::string type_to_string()
88 {
89 static_string static_name = type_name<T>();
90 return std::string(static_name.data, static_name.size);
91 }
92
93 template <class T>
94 std::string info(const T& t)
95 {
96 std::string s;
97 s += "\nValue type: " + type_to_string<typename T::value_type>();
98 s += "\nLayout: ";
99 if (t.layout() == layout_type::row_major)
100 {
101 s += "row_major";
102 }
103 else if (t.layout() == layout_type::column_major)
104 {
105 s += "column_major";
106 }
107 else if (t.layout() == layout_type::dynamic)
108 {
109 s += "dynamic";
110 }
111 else
112 {
113 s += "any";
114 }
115 s += "\nShape: (";
116 bool first = true;
117 for (const auto& el : t.shape())
118 {
119 if (!first)
120 {
121 s += ", ";
122 }
123 first = false;
124 s += std::to_string(el);
125 }
126 s += ")\nStrides: (";
127 first = true;
128 for (const auto& el : t.strides())
129 {
130 if (!first)
131 {
132 s += ", ";
133 }
134 first = false;
135 s += std::to_string(el);
136 }
137 s += ")\nSize: " + std::to_string(t.size()) + "\n";
138 return s;
139 }
140}
141
142#endif
auto strides(const E &e, stride_type type=stride_type::normal) noexcept
Get strides of an object.
Definition xstrides.hpp:248
standard mathematical functions for xexpressions