VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
Reflection.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "Types.h"
16#include <functional>
17#include <map>
18
19namespace vc64::util {
20
21#define assert_enum(e,v) assert(e##Enum::isValid(v))
22
23template <class T, typename E> struct Reflection {
24
25 // Returns the key including the section prefix (if any)
26 static const char *key(isize nr) { return T::key((E)nr); }
27
28 // Returns the key without the section prefix (if any)
29 static const char *plainkey(isize nr) {
30 auto *p = key(nr);
31 for (isize i = 0; p[i]; i++) if (p[i] == '.') return p + i + 1;
32 return p;
33 }
34
35 // Collects all key / value pairs
36 static std::map <string, E> pairs() {
37
38 std::map <string, E> result;
39
40 for (auto i = T::minVal; i <= T::maxVal; i++) {
41 if (T::isValid(i)) result.insert(std::make_pair(key(i), E(i)));
42 }
43
44 return result;
45 }
46
47 // Returns a list in form of a colon seperated string
48 static string keyList(std::function<bool(E)> filter = [](E){ return true; }, const string &delim = ", ") {
49
50 string result;
51
52 for (auto i = T::minVal; i <= T::maxVal; i++) {
53
54 if (T::isValid(i) && filter(E(i))) {
55
56 if (result != "") result += delim;
57 result += (key(i));
58 }
59 }
60
61 return result;
62 }
63
64 // Convenience wrapper
65 static string argList(std::function<bool(E)> filter = [](E){ return true; }) {
66
67 return "{ " + keyList(filter, " | ") + " }";
68 }
69};
70
71}