store idents by pointer

master
Daniel Kolesa 2016-08-17 21:21:16 +01:00
parent 32335049fa
commit 03c3145956
2 changed files with 29 additions and 26 deletions

View File

@ -122,20 +122,21 @@ CsState::CsState() {
new_ident(static_cast<char const *>(buf), IDF_ARG); new_ident(static_cast<char const *>(buf), IDF_ARG);
} }
dummy = new_ident("//dummy"); dummy = new_ident("//dummy");
add_ident("numargs", MaxArguments, 0, &numargs); add_ident(new Ident("numargs", MaxArguments, 0, &numargs));
add_ident("dbgalias", 0, 1000, &dbgalias); add_ident(new Ident("dbgalias", 0, 1000, &dbgalias));
cs_init_lib_base(*this); cs_init_lib_base(*this);
} }
CsState::~CsState() { CsState::~CsState() {
for (Ident &i: idents.iter()) { for (auto &p: idents.iter()) {
if (i.type == ID_ALIAS) { Ident *i = p.second;
i.force_null(); if (i->type == ID_ALIAS) {
delete[] reinterpret_cast<ostd::Uint32 *>(i.code); i->force_null();
i.code = nullptr; delete[] reinterpret_cast<ostd::Uint32 *>(i->code);
} else if (i.type == ID_COMMAND || i.type >= ID_LOCAL) { } else if (i->type == ID_COMMAND || i->type >= ID_LOCAL) {
delete[] i.cargs; delete[] i->cargs;
} }
delete i;
} }
} }
@ -171,8 +172,8 @@ void CsState::clear_override(Ident &id) {
} }
void CsState::clear_overrides() { void CsState::clear_overrides() {
for (Ident &id: idents.iter()) { for (auto &p: idents.iter()) {
clear_override(id); clear_override(*(p.second));
} }
} }
@ -185,7 +186,7 @@ Ident *CsState::new_ident(ostd::ConstCharRange name, int flags) {
); );
return dummy; return dummy;
} }
id = add_ident(name, flags); id = add_ident(new Ident(name, flags));
} }
return id; return id;
} }
@ -263,7 +264,7 @@ void CsState::set_alias(ostd::ConstCharRange name, TaggedValue &v) {
cs_debug_code(*this, "cannot alias number %s", name); cs_debug_code(*this, "cannot alias number %s", name);
v.cleanup(); v.cleanup();
} else { } else {
add_ident(name, v, identflags); add_ident(new Ident(name, v, identflags));
} }
} }
@ -1026,7 +1027,7 @@ static bool cs_add_command(
return false; 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; return true;
} }

View File

@ -10,7 +10,7 @@
#include <ostd/types.hh> #include <ostd/types.hh>
#include <ostd/string.hh> #include <ostd/string.hh>
#include <ostd/vector.hh> #include <ostd/vector.hh>
#include <ostd/keyset.hh> #include <ostd/map.hh>
#include <ostd/range.hh> #include <ostd/range.hh>
#include <ostd/utility.hh> #include <ostd/utility.hh>
#include <ostd/maybe.hh> #include <ostd/maybe.hh>
@ -280,10 +280,6 @@ struct OSTD_EXPORT Ident {
void get_cstr(TaggedValue &v) const; void get_cstr(TaggedValue &v) const;
void get_cval(TaggedValue &v) const; void get_cval(TaggedValue &v) const;
ostd::ConstCharRange get_key() const {
return name.iter();
}
void clean_code(); void clean_code();
void push_arg(TaggedValue const &v, IdentStack &st, bool um = true); void push_arg(TaggedValue const &v, IdentStack &st, bool um = true);
@ -337,7 +333,7 @@ struct IdentLink {
}; };
struct OSTD_EXPORT CsState { struct OSTD_EXPORT CsState {
ostd::Keyset<Ident> idents; ostd::Map<ostd::ConstCharRange, Ident *> idents;
ostd::Vector<Ident *> identmap; ostd::Vector<Ident *> identmap;
Ident *dummy = nullptr; Ident *dummy = nullptr;
@ -359,18 +355,24 @@ struct OSTD_EXPORT CsState {
void clear_override(Ident &id); void clear_override(Ident &id);
void clear_overrides(); void clear_overrides();
template<typename ...A> Ident *add_ident(Ident *id) {
Ident *add_ident(A &&...args) { if (!id) {
Ident &def = idents.emplace(ostd::forward<A>(args)...).first.front(); return nullptr;
def.index = identmap.size(); }
return identmap.push(&def); idents[id->name] = id;
id->index = identmap.size();
return identmap.push(id);
} }
Ident *new_ident(ostd::ConstCharRange name, int flags = IDF_UNKNOWN); Ident *new_ident(ostd::ConstCharRange name, int flags = IDF_UNKNOWN);
Ident *force_ident(TaggedValue &v); Ident *force_ident(TaggedValue &v);
Ident *get_ident(ostd::ConstCharRange name) { 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) { bool have_ident(ostd::ConstCharRange name) {