Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
enums.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Authors:
4 * Nicholas Bishop <nicholasbishop@gmail.com>
5 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
6 *
7 * Copyright (C) 2007 Authors
8 *
9 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
10 */
11#ifndef INKSCAPE_UTIL_ENUMS_H
12#define INKSCAPE_UTIL_ENUMS_H
13
14#include <type_traits>
15#include <glibmm/ustring.h>
16
17namespace Inkscape::Util {
18
25template<typename E>
27{
28 E id;
29 const Glib::ustring label;
30 const Glib::ustring key;
31};
32
33const Glib::ustring empty_string("");
34
41template<typename E> class EnumDataConverter
42{
43public:
45
46 EnumDataConverter(const EnumData<E>* cd, const unsigned int length)
47 : _length(length), _data(cd)
48 {}
49
50 E get_id_from_label(const Glib::ustring& label) const
51 {
52 for(unsigned int i = 0; i < _length; ++i) {
53 if(_data[i].label == label)
54 return _data[i].id;
55 }
56
57 return (E)0;
58 }
59
60 E get_id_from_key(const Glib::ustring& key) const
61 {
62 for(unsigned int i = 0; i < _length; ++i) {
63 if(_data[i].key == key)
64 return _data[i].id;
65 }
66
67 return (E)0;
68 }
69
70 bool is_valid_key(const Glib::ustring& key) const
71 {
72 for(unsigned int i = 0; i < _length; ++i) {
73 if(_data[i].key == key)
74 return true;
75 }
76
77 return false;
78 }
79
80 bool is_valid_id(const E id) const
81 {
82 for(unsigned int i = 0; i < _length; ++i) {
83 if(_data[i].id == id)
84 return true;
85 }
86 return false;
87 }
88
89 const Glib::ustring& get_label(const E id) const
90 {
91 for(unsigned int i = 0; i < _length; ++i) {
92 if(_data[i].id == id)
93 return _data[i].label;
94 }
95
96 return empty_string;
97 }
98
99 const Glib::ustring& get_key(const E id) const
100 {
101 for(unsigned int i = 0; i < _length; ++i) {
102 if(_data[i].id == id)
103 return _data[i].key;
104 }
105
106 return empty_string;
107 }
108
109 const EnumData<E>& data(const unsigned int i) const
110 {
111 return _data[i];
112 }
113
114 const unsigned int _length;
115private:
117};
118
119template <typename T>
120requires std::is_enum_v<T>
121constexpr bool any_flag(T test)
122{
123 return test != T{};
124}
125
126template <typename T>
127requires std::is_enum_v<T>
128constexpr bool has_flag(T test, T flag)
129{
130 return any_flag(test & flag);
131}
132
133} // namespace Inkscape::Util
134
135#endif // INKSCAPE_UTIL_ENUMS_H
136
137/*
138 Local Variables:
139 mode:c++
140 c-file-style:"stroustrup"
141 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
142 indent-tabs-mode:nil
143 fill-column:99
144 End:
145*/
146// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
int test()
Definition 2junctions.cpp:5
Simplified management of enumerations of svg items with UI labels.
Definition enums.h:42
E get_id_from_key(const Glib::ustring &key) const
Definition enums.h:60
const unsigned int _length
Definition enums.h:114
bool is_valid_id(const E id) const
Definition enums.h:80
const EnumData< E > & data(const unsigned int i) const
Definition enums.h:109
const EnumData< E > * _data
Definition enums.h:116
E get_id_from_label(const Glib::ustring &label) const
Definition enums.h:50
bool is_valid_key(const Glib::ustring &key) const
Definition enums.h:70
const Glib::ustring & get_label(const E id) const
Definition enums.h:89
const Glib::ustring & get_key(const E id) const
Definition enums.h:99
EnumDataConverter(const EnumData< E > *cd, const unsigned int length)
Definition enums.h:46
Glib::ustring label
Miscellaneous supporting code.
Definition document.h:93
const Glib::ustring empty_string("")
constexpr bool has_flag(T test, T flag)
Definition enums.h:128
constexpr bool any_flag(T test)
Definition enums.h:121
static cairo_user_data_key_t key
Simplified management of enumerations of svg items with UI labels.
Definition enums.h:27
const Glib::ustring label
Definition enums.h:29
const Glib::ustring key
Definition enums.h:30