add ident getter by index, + ident count method on state

master
Daniel Kolesa 2021-05-16 00:50:14 +02:00
parent 8086c23a77
commit 4fb6b9a0bc
3 changed files with 53 additions and 3 deletions

View File

@ -215,11 +215,37 @@ struct LIBCUBESCRIPT_EXPORT state {
*/
ident &new_ident(std::string_view n);
/** @brief Get the number of idents in the state
*
* This returns the number of idents that the main state has stored. It
* does not matter which thread you call this on.
*/
std::size_t ident_count() const;
/** @brief Get a specific cubescript::ident */
std::optional<std::reference_wrapper<ident>> get_ident(
std::string_view name
);
/** @brief Get a specific cubescript::ident */
std::optional<std::reference_wrapper<ident const>> get_ident(
std::string_view name
) const;
/** @brief Get a specific cubescript::ident by index
*
* Keep in mind that no bounds checking is performed, so the index must
* be within range.
*/
ident &get_ident(std::size_t index);
/** @brief Get a specific cubescript::ident by index
*
* Keep in mind that no bounds checking is performed, so the index must
* be within range.
*/
ident const &get_ident(std::size_t index) const;
/** @brief Assign a value to a name
*
* This will set something of the given name to the given value. The

View File

@ -314,6 +314,10 @@ LIBCUBESCRIPT_EXPORT void *state::alloc(void *ptr, size_t os, size_t ns) {
return p_tstate->istate->alloc(ptr, os, ns);
}
LIBCUBESCRIPT_EXPORT std::size_t state::ident_count() const {
return p_tstate->istate->identmap.size();
}
LIBCUBESCRIPT_EXPORT std::optional<
std::reference_wrapper<ident>
> state::get_ident(std::string_view name) {
@ -324,6 +328,25 @@ LIBCUBESCRIPT_EXPORT std::optional<
return *id;
}
LIBCUBESCRIPT_EXPORT std::optional<
std::reference_wrapper<ident const>
> state::get_ident(std::string_view name) const {
auto *id = p_tstate->istate->get_ident(name);
if (!id) {
return std::nullopt;
}
return *id;
}
LIBCUBESCRIPT_EXPORT ident &state::get_ident(std::size_t index) {
return *p_tstate->istate->identmap[index];
}
LIBCUBESCRIPT_EXPORT ident const &state::get_ident(std::size_t index) const {
return *p_tstate->istate->identmap[index];
}
LIBCUBESCRIPT_EXPORT span_type<ident *> state::get_idents() {
return span_type<ident *>{
p_tstate->istate->identmap.data(),

View File

@ -15,11 +15,12 @@ static cs::state *ln_cs = nullptr;
inline void ln_complete(char const *buf, std::vector<std::string> &lc) {
std::string_view cmd = get_complete_cmd(buf);
for (auto id: ln_cs->get_idents()) {
if (id->type() != cs::ident_type::COMMAND) {
for (std::size_t i = 0; i < ln_cs->ident_count(); ++i) {
auto &id = ln_cs->get_ident(i);
if (id.type() != cs::ident_type::COMMAND) {
continue;
}
std::string_view idname = id->name();
std::string_view idname = id.name();
if (idname.size() <= cmd.size()) {
continue;
}