summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/penum.h
diff options
context:
space:
mode:
author couriersud <couriersud@gmx.org>2020-05-23 10:48:33 +0200
committer couriersud <couriersud@gmx.org>2020-05-24 10:48:06 +0200
commitde615ac138316726da6158bfaa8567dde96ddcc0 (patch)
treeb9b53339ddfe40054b42942701f8aa7dd9a927e6 /src/lib/netlist/plib/penum.h
parentfff7f6ef55f93ba53ceea798d1c65a9910c41f27 (diff)
netlist: move penum into own header file. (nw)
Diffstat (limited to 'src/lib/netlist/plib/penum.h')
-rw-r--r--src/lib/netlist/plib/penum.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/lib/netlist/plib/penum.h b/src/lib/netlist/plib/penum.h
new file mode 100644
index 00000000000..1e0042f7dd2
--- /dev/null
+++ b/src/lib/netlist/plib/penum.h
@@ -0,0 +1,57 @@
+// license:GPL-2.0+
+// copyright-holders:Couriersud
+
+#ifndef PENUM_H_
+#define PENUM_H_
+
+///
+/// \file penum.h
+///
+
+#include "pstring.h"
+#include "pstrutil.h"
+#include "putil.h"
+
+namespace plib
+{
+
+ //============================================================
+ // penum - strongly typed enumeration
+ //============================================================
+ struct penum_base
+ {
+ protected:
+ // Implementation in putil.cpp.
+ // Putting the code here leads to a performance decrease.
+ static int from_string_int(const pstring &str, const pstring &x);
+ static pstring nthstr(int n, const pstring &str);
+ };
+
+} // namespace plib
+
+#define PENUM(ename, ...) \
+ struct ename : public plib::penum_base { \
+ enum E { __VA_ARGS__ }; \
+ constexpr ename (const E &v) : m_v(v) { } \
+ constexpr ename (E && v) : m_v(v) { } \
+ template <typename T> explicit constexpr ename(const T &val) { m_v = static_cast<E>(val); } \
+ template <typename T> explicit constexpr ename(T && val) { m_v = static_cast<E>(val); } \
+ bool set_from_string (const pstring &s) { \
+ int f = from_string_int(strings(), s); \
+ if (f>=0) { m_v = static_cast<E>(f); return true; } \
+ return false;\
+ } \
+ constexpr operator E() const noexcept {return m_v;} \
+ constexpr bool operator==(const ename &rhs) const noexcept {return m_v == rhs.m_v;} \
+ constexpr bool operator==(const E &rhs) const noexcept {return m_v == rhs;} \
+ pstring name() const { \
+ return nthstr(static_cast<int>(m_v), strings()); \
+ } \
+ private: E m_v; \
+ static pstring strings() {\
+ static const char * lstrings = # __VA_ARGS__; \
+ return pstring(lstrings); \
+ } \
+ };
+
+#endif // PENUM_H_