summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarc Jeanmougin <marc@jeanmougin.fr>2019-01-01 10:08:57 +0000
committerMarc Jeanmougin <marc@jeanmougin.fr>2019-01-01 10:08:57 +0000
commit1f70a51c7136ddc15615cc21ab03beea58df5887 (patch)
treefe128c76844003aef0fb78a6040816847840b4ff /src
parentMerge branch 'items-to-intrusive-list' of gitlab.com:ollip/inkscape (diff)
parentsp_attribute_lookup with std::map (diff)
downloadinkscape-1f70a51c7136ddc15615cc21ab03beea58df5887.tar.gz
inkscape-1f70a51c7136ddc15615cc21ab03beea58df5887.zip
Merge remote-tracking branch 'origin/attribute-lookup-map'
Diffstat (limited to 'src')
-rw-r--r--src/attributes.cpp34
-rw-r--r--src/attributes.h7
-rw-r--r--src/style.cpp15
3 files changed, 38 insertions, 18 deletions
diff --git a/src/attributes.cpp b/src/attributes.cpp
index 1222d2669..d7081d4c9 100644
--- a/src/attributes.cpp
+++ b/src/attributes.cpp
@@ -9,6 +9,9 @@
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
+#include <cstring>
+#include <map>
+
#include <glib.h> // g_assert()
#include "attributes.h"
@@ -555,15 +558,34 @@ static SPStyleProp const props[] = {
#define n_attrs (sizeof(props) / sizeof(props[0]))
-/** Returns an SPAttributeEnum; SP_ATTR_INVALID (of value 0) if key isn't recognized. */
+/**
+ * Inverse to the \c props array for lookup by name.
+ */
+struct AttributeLookupImpl {
+ struct cstrless {
+ bool operator()(char const *lhs, char const *rhs) const { return std::strcmp(lhs, rhs) < 0; }
+ };
+
+ std::map<char const *, SPAttributeEnum, cstrless> m_map;
+
+ AttributeLookupImpl()
+ {
+ for (unsigned int i = 1; i < n_attrs; i++) {
+ // sanity check: order of props array must match SPAttributeEnum
+ g_assert(props[i].code == i);
+
+ m_map[props[i].name] = props[i].code;
+ }
+ }
+};
+
SPAttributeEnum
sp_attribute_lookup(gchar const *key)
{
- for (unsigned int i = 1; i < n_attrs; i++) {
- g_assert(props[i].code == static_cast< gint >(i) );
- // If this g_assert fails, then the sort order of SPAttributeEnum does not match the order in props[]!
- if(g_str_equal(const_cast<void *>(static_cast<void const *>(props[i].name)), key))
- return props[i].code;
+ static AttributeLookupImpl const _instance;
+ auto it = _instance.m_map.find(key);
+ if (it != _instance.m_map.end()) {
+ return it->second;
}
// std::cerr << "sp_attribute_lookup: invalid attribute: "
// << (key?key:"Null") << std::endl;
diff --git a/src/attributes.h b/src/attributes.h
index 1f0178fae..512c00499 100644
--- a/src/attributes.h
+++ b/src/attributes.h
@@ -564,7 +564,14 @@ enum SPAttributeEnum : unsigned {
SP_PROP_PATH_EFFECT,
};
+/**
+ * Get attribute id by name. Return SP_ATTR_INVALID for invalid names.
+ */
SPAttributeEnum sp_attribute_lookup(gchar const *key);
+
+/**
+ * Get attribute name by id. Return NULL for invalid ids.
+ */
gchar const *sp_attribute_name(SPAttributeEnum id);
#endif
diff --git a/src/style.cpp b/src/style.cpp
index aab8f2695..b11a92749 100644
--- a/src/style.cpp
+++ b/src/style.cpp
@@ -76,7 +76,7 @@ static CRSelEng *sp_repr_sel_eng();
class SPStylePropHelper {
SPStylePropHelper() {
#define REGISTER_PROPERTY(id, member, name) \
- _register(reinterpret_cast<SPIBasePtr>(&SPStyle::member), id, name)
+ _register(reinterpret_cast<SPIBasePtr>(&SPStyle::member), id) /* name unused */
// SVG 2: Attributes promoted to properties
REGISTER_PROPERTY(SP_ATTR_D, d, "d");
@@ -215,11 +215,7 @@ public:
* Get property pointer by name
*/
SPIBase *get(SPStyle *style, const std::string &name) {
- auto it = m_name_map.find(name);
- if (it != m_name_map.end()) {
- return _get(style, it->second);
- }
- return nullptr;
+ return get(style, sp_attribute_lookup(name.c_str()));
}
/**
@@ -238,19 +234,14 @@ public:
private:
SPIBase *_get(SPStyle *style, SPIBasePtr ptr) { return &(style->*ptr); }
- void _register(SPIBasePtr ptr, SPAttributeEnum id, const char *name) {
+ void _register(SPIBasePtr ptr, SPAttributeEnum id) {
m_vector.push_back(ptr);
if (id != SP_ATTR_INVALID) {
m_id_map[id] = ptr;
}
-
- if (name[0]) {
- m_name_map[name] = ptr;
- }
}
- std::unordered_map<std::string, SPIBasePtr> m_name_map;
std::unordered_map</* SPAttributeEnum */ int, SPIBasePtr> m_id_map;
std::vector<SPIBasePtr> m_vector;
};