summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/tests/test_penum.cpp
blob: 6347a41d027f7c954f29fb7470b1c0d05b38c8bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// license:BSD-3-Clause
// copyright-holders:Couriersud

///
/// \file test_penum.cpp
///
/// tests for penum
///

#include "plib/putil.h"

#include "plib/pexception.h"
#include "plib/pstring.h"

#include <string>
#include <type_traits>
#include <string_view>

#if 0
namespace plib
{
	template <typename E>
	struct functor { using is_defined = std::false_type; };
	template <std::size_t N, typename E>
	struct functor_base
	{
	public:
		using is_defined = std::true_type;
		using size = std::integral_constant<std::size_t, N>;
		constexpr operator E () const noexcept { return m_v; }
		constexpr functor_base(const E v) noexcept : m_v(v) { };
	protected:
		constexpr functor_base(const char *s, const char * const vals[size::value]) noexcept : m_v(E::PENUM_UNKNOWN)
		{ for (std::size_t i = 0; i < size::value; i++) if (vals[i] == s) { m_v = static_cast<E>(i); return; }  }
		E m_v;
	private:
		//static constexpr const char *m_str[PNARGS(__VA_ARGS__)] = { PSTRINGIFY_VA(__VA_ARGS__) };
	};

} // namespace plib

#define PENUM(ename, ...) \
	enum class ename { __VA_ARGS__ , PENUM_UNKNOWN }; \
	PENUM_FUNCTOR(ename, __VA_ARGS__)

#define PENUM_NS(ns, ename, ...) \
	namespace ns { enum class ename { __VA_ARGS__ , PENUM_UNKNOWN }; } \
	PENUM_FUNCTOR(ns :: ename, __VA_ARGS__)

#define PENUM_FUNCTOR(ename, ...) \
	template <> struct plib::functor<ename> : plib::functor_base<PNARGS(__VA_ARGS__), ename> \
	{ \
	public: \
		using plib::functor_base<size::value, ename>::functor_base; \
		constexpr functor(const char  *s) noexcept : functor_base(s, m_str) { } \
		constexpr operator const char * () const noexcept { return m_str[static_cast<std::size_t>(m_v)]; } \
	private: \
		static constexpr const char *m_str[3] = { PSTRINGIFY_VA(__VA_ARGS__) }; \
	};

#else
#if 1
namespace plib::enum_static
{
	template <typename E>
	struct functor_static
	{
		using is_defined = std::false_type;
		using size = std::integral_constant<std::size_t, 0>;
		static constexpr const char *m_str[1] = { "" };
	};
} // namespace plib::enum_static

#endif
namespace plib
{
	template <typename E>
	struct functor
	{
	public:
		using staticf = enum_static::functor_static<E>;
		using is_defined = typename staticf::is_defined;
		using size = typename staticf::size;
		constexpr functor(const E v) noexcept : m_v(v) { }
		constexpr functor(const char *s) noexcept : m_v(E::PENUM_UNKNOWN)
		{
			for (std::size_t i = 0; i < size::value; i++)
				if (staticf::m_str[i] == s)
				{
					m_v = static_cast<E>(i);
					return;
				}
		}
		constexpr operator const char * () const noexcept { return staticf::m_str[static_cast<std::size_t>(m_v)]; }
		constexpr operator E () const noexcept { return m_v; }
	protected:
		E m_v;
	private:
		//static constexpr const char *m_str[PNARGS(__VA_ARGS__)] = { PSTRINGIFY_VA(__VA_ARGS__) };
	};

	// template deduction guide
	template<typename E> functor(E) -> functor<E>;

	template<typename E>
	static inline typename std::enable_if<plib::enum_static::functor_static<E>::is_defined::value, const char *>::type
	penum_to_string(E e)
	{
		using staticf = enum_static::functor_static<E>;
		return staticf::m_str[static_cast<std::size_t>(e)];
	}

	template<typename E>
	static inline typename std::enable_if<plib::enum_static::functor_static<E>::is_defined::value, E>::type
	string_to_penum(const char *s)
	{
		using staticf = enum_static::functor_static<E>;
		for (std::size_t i = 0; i < staticf::size::value; i++)
			if (staticf::m_str[i] == s)
				return static_cast<E>(i);
		return E::PENUM_UNKNOWN;
	}

} // namespace plib

#define PENUM(ename, ...) \
	enum class ename { __VA_ARGS__ , PENUM_UNKNOWN }; \
	PENUM_FUNCTOR(ename, __VA_ARGS__)

#define PENUM_NS(ns, ename, ...) \
	namespace ns { enum class ename { __VA_ARGS__ , PENUM_UNKNOWN }; } \
	PENUM_FUNCTOR(ns :: ename, __VA_ARGS__)

#define PENUM_FUNCTOR(ename, ...) \
	namespace plib::enum_static { \
	template <> struct functor_static<ename> \
	{ \
	public: \
		using is_defined = std::true_type; \
		using size = std::integral_constant<std::size_t, PNARGS(__VA_ARGS__) >; \
		static constexpr const char *m_str[size::value] = { PSTRINGIFY_VA(__VA_ARGS__) }; \
	}; \
	} // namespace plib::enum_static

#endif

template <class O, class E>
typename std::enable_if<!plib::has_ostream_operator<O, E>::value && plib::enum_static::functor_static<E>::is_defined::value, O&>::type
operator << (O& os, const E &p)
{
	os << static_cast<const char *>(plib::functor<E>(p));
	return os;
}

#if 1
template <typename E>
inline typename std::enable_if<plib::enum_static::functor_static<E>::is_defined::value, bool>::type
operator == (const char * lhs, const E &rhs)
{
	return lhs == plib::functor<E>(rhs);
}
template <typename E>
inline typename std::enable_if<plib::enum_static::functor_static<E>::is_defined::value, bool>::type
operator == (const E &lhs, const char * rhs)
{
	return lhs == plib::functor<E>(rhs);
}
#endif

// ---------------------------------------

template <typename T>
struct ret_t
{
	bool success;
	T value;
};

template <typename T, T BASE>
constexpr ret_t<T> stl_h(std::string_view sv)
{
	T r(0);
	for (const auto &c : sv)
	{
		if (c >= '0' && c < '0' + std::min(10,BASE))
		{
			r = r * BASE + (c - '0');
		}
		else if (c >= 'A' && c < ('A' + BASE - 10))
		{
			r = r * BASE + (10 + c - 'A');
		}
		else if (c >= 'a' && c < ('a' + BASE - 10))
		{
			r = r * BASE + (10 + c - 'a');
		}
		else
			return { false, T(0) };
	}
	return { true, r };
}

template <typename T, T BASE = 0>
constexpr ret_t<T> stl(std::string_view sv)
{
	if constexpr (BASE == 0)
	{
		if (sv.size() > 2)
		{
			if (sv.substr(0,2) == "0x" || sv.substr(0,2) == "0X")
					return stl_h<T,16>(sv.substr(2));
			if (sv.substr(0,2) == "0b")
					return stl_h<T,2>(sv.substr(2));
		}
		if (sv.size() > 1 && sv.substr(0,1) == "0" )
			return stl_h<T,8>(sv.substr(1));
		return stl_h<T, 10>(sv);
	}
	return stl_h<T, BASE>(sv);
}

#include "plib/ptests.h"

//PENUM_NS(plibx, teste, A, B, C)

namespace plibx { enum class teste; }
PENUM(plibx::teste, A, B, C)

enum class pure { X, Y };

PENUM(testf, A, B, C)

PTEST(penum, conversion)
{
	static_assert(stl<int,0>("0xa0bc").success,"no");
	static_assert(stl<int,0>("0xa0bc").value==0xa0bc,"no");
	static_assert(stl<int,0>("0b1010").value==0b1010,"no");
	static_assert(stl<int,0>("0123").value==0123,"no");
	//const auto x(stl<int,0>("123"));
	//static_assert(x.value==123,"no");

	plibx::teste x = plib::functor<plibx::teste>("A");
	PEXPECT_NE(x, plibx::teste::B);
	PEXPECT_NE(plib::functor<plibx::teste>("A"), plibx::teste::B);
	PEXPECT_NE(plib::functor<testf>("A"), testf::B);
	//PEXPECT_EQ(plib::functor("A"), testf::B);
	PEXPECT_EQ(testf::B, "B");
	PEXPECT_EQ("A", testf::A);
	PEXPECT_NE(plib::functor(testf::A), "B");
	PEXPECT_FALSE(plib::functor<pure>::is_defined::value);
	PEXPECT_EQ(plib::penum_to_string(testf::B), "B");
	PEXPECT_NE(plib::string_to_penum<testf>("B"), testf::A);
	//PEXPECT_EQ(pure::X, pure::Y);
}