From 03c3145956a1242f486fc3da3387c6de92826809 Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 17 Aug 2016 21:21:16 +0100 Subject: [PATCH] store idents by pointer --- cubescript.cc | 29 +++++++++++++++-------------- cubescript.hh | 26 ++++++++++++++------------ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/cubescript.cc b/cubescript.cc index 2516174..2705740 100644 --- a/cubescript.cc +++ b/cubescript.cc @@ -122,20 +122,21 @@ CsState::CsState() { new_ident(static_cast(buf), IDF_ARG); } dummy = new_ident("//dummy"); - add_ident("numargs", MaxArguments, 0, &numargs); - add_ident("dbgalias", 0, 1000, &dbgalias); + add_ident(new Ident("numargs", MaxArguments, 0, &numargs)); + add_ident(new Ident("dbgalias", 0, 1000, &dbgalias)); cs_init_lib_base(*this); } CsState::~CsState() { - for (Ident &i: idents.iter()) { - if (i.type == ID_ALIAS) { - i.force_null(); - delete[] reinterpret_cast(i.code); - i.code = nullptr; - } else if (i.type == ID_COMMAND || i.type >= ID_LOCAL) { - delete[] i.cargs; + for (auto &p: idents.iter()) { + Ident *i = p.second; + if (i->type == ID_ALIAS) { + i->force_null(); + delete[] reinterpret_cast(i->code); + } else if (i->type == ID_COMMAND || i->type >= ID_LOCAL) { + delete[] i->cargs; } + delete i; } } @@ -171,8 +172,8 @@ void CsState::clear_override(Ident &id) { } void CsState::clear_overrides() { - for (Ident &id: idents.iter()) { - clear_override(id); + for (auto &p: idents.iter()) { + clear_override(*(p.second)); } } @@ -185,7 +186,7 @@ Ident *CsState::new_ident(ostd::ConstCharRange name, int flags) { ); return dummy; } - id = add_ident(name, flags); + id = add_ident(new Ident(name, flags)); } return id; } @@ -263,7 +264,7 @@ void CsState::set_alias(ostd::ConstCharRange name, TaggedValue &v) { cs_debug_code(*this, "cannot alias number %s", name); v.cleanup(); } else { - add_ident(name, v, identflags); + add_ident(new Ident(name, v, identflags)); } } @@ -1026,7 +1027,7 @@ static bool cs_add_command( return false; } } - cs.add_ident(type, name, args, argmask, nargs, ostd::move(func)); + cs.add_ident(new Ident(type, name, args, argmask, nargs, ostd::move(func))); return true; } diff --git a/cubescript.hh b/cubescript.hh index 8f5bddf..6c8179a 100644 --- a/cubescript.hh +++ b/cubescript.hh @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include @@ -280,10 +280,6 @@ struct OSTD_EXPORT Ident { void get_cstr(TaggedValue &v) const; void get_cval(TaggedValue &v) const; - ostd::ConstCharRange get_key() const { - return name.iter(); - } - void clean_code(); void push_arg(TaggedValue const &v, IdentStack &st, bool um = true); @@ -337,7 +333,7 @@ struct IdentLink { }; struct OSTD_EXPORT CsState { - ostd::Keyset idents; + ostd::Map idents; ostd::Vector identmap; Ident *dummy = nullptr; @@ -359,18 +355,24 @@ struct OSTD_EXPORT CsState { void clear_override(Ident &id); void clear_overrides(); - template - Ident *add_ident(A &&...args) { - Ident &def = idents.emplace(ostd::forward(args)...).first.front(); - def.index = identmap.size(); - return identmap.push(&def); + Ident *add_ident(Ident *id) { + if (!id) { + return nullptr; + } + idents[id->name] = id; + id->index = identmap.size(); + return identmap.push(id); } Ident *new_ident(ostd::ConstCharRange name, int flags = IDF_UNKNOWN); Ident *force_ident(TaggedValue &v); Ident *get_ident(ostd::ConstCharRange name) { - return idents.at(name); + Ident **id = idents.at(name); + if (!id) { + return nullptr; + } + return *id; } bool have_ident(ostd::ConstCharRange name) {