Skip to content

Commit

Permalink
[#30] script: make script::find_symbol_by_name take std::string_view
Browse files Browse the repository at this point in the history
Since we do a copy internally anyways, it doesn't hurt to allow users to
pass a `std::string_view` instead of a `std::string` which might avoid
unnecessary string copies. This change cascades to many other APIs related
to the script and VM.
  • Loading branch information
lmichaelis committed Nov 6, 2022
1 parent 7b6fa48 commit ae77600
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
12 changes: 6 additions & 6 deletions include/phoenix/script.hh
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ namespace phoenix {
std::is_same_v<_member, std::int32_t> ||
(std::is_enum_v<_member> && sizeof(_member) == 4),
void>::type
register_member(const std::string& name, _member (_class::*field)[N]) { // clang-format on
register_member(std::string_view name, _member (_class::*field)[N]) { // clang-format on
auto* type = &typeid(_class);
auto* sym = _check_member<_class, _member, N>(name, type);

Expand All @@ -668,7 +668,7 @@ namespace phoenix {
std::is_same_v<_member, std::int32_t> ||
(std::is_enum_v<_member> && sizeof(_member) == 4),
void>::type
register_member(const std::string& name, _member _class::*field) {
register_member(std::string_view name, _member _class::*field) {
auto* type = &typeid(_class);
auto* sym = _check_member<_class, _member, 1>(name, type);

Expand Down Expand Up @@ -701,7 +701,7 @@ namespace phoenix {
/// \brief Retrieves the symbol with the given \p name.
/// \param name The name of the symbol to get.
/// \return The symbol or `nullptr` if no symbol with that name was found.
[[nodiscard]] const symbol* find_symbol_by_name(const std::string& name) const;
[[nodiscard]] const symbol* find_symbol_by_name(std::string_view name) const;

/// \brief Retrieves the symbol with the given \p index
/// \param index The index of the symbol to get
Expand All @@ -721,13 +721,13 @@ namespace phoenix {
/// \brief Retrieves the symbol with the given \p name.
/// \param name The name of the symbol to get.
/// \return The symbol or `nullptr` if no symbol with that name was found.
[[nodiscard]] symbol* find_symbol_by_name(const std::string& name);
[[nodiscard]] symbol* find_symbol_by_name(std::string_view name);

/// \brief Call the given callback function for every instance symbol which is a descendant of the class with
/// the given name.
/// \param name The name of the parent class.
/// \param callback The function to call with each instance symbol.
void enumerate_instances_by_class_name(const std::string& name, const std::function<void(symbol&)>& callback);
void enumerate_instances_by_class_name(std::string_view name, const std::function<void(symbol&)>& callback);

/// \brief Decodes the instruction at \p address and returns it.
/// \param address The address of the instruction to decode
Expand Down Expand Up @@ -781,7 +781,7 @@ namespace phoenix {
script(script&& move) = default;

template <typename _class, typename _member, int N>
symbol* _check_member(const std::string& name, const std::type_info* type) {
symbol* _check_member(std::string_view name, const std::type_info* type) {
auto* sym = find_symbol_by_name(name);

if (sym == nullptr)
Expand Down
28 changes: 14 additions & 14 deletions include/phoenix/vm.hh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace phoenix {
/// \param sym The name of the function to call.
/// \param args The arguments for the function call.
template <typename R = _ignore_return_value, typename... P>
R call_function(const std::string& name, P... args) {
R call_function(std::string_view name, P... args) {
return call_function<R, P...>(find_symbol_by_name(name), args...);
}

Expand Down Expand Up @@ -158,7 +158,7 @@ namespace phoenix {
/// \return The initialized instance.
template <typename _instance_t>
typename std::enable_if<std::is_base_of_v<instance, _instance_t>, std::shared_ptr<_instance_t>>::type
init_instance(const std::string& name) {
init_instance(std::string_view name) {
return init_instance<_instance_t>(find_symbol_by_name(name));
}

Expand All @@ -168,7 +168,7 @@ namespace phoenix {
/// \param name The name of the instance to initialize (ie. 'STT_309_WHISTLER')
template <typename _instance_t>
typename std::enable_if<std::is_base_of_v<instance, _instance_t>, void>::type
init_instance(const std::shared_ptr<_instance_t>& instance, const std::string& name) {
init_instance(const std::shared_ptr<_instance_t>& instance, std::string_view name) {
init_instance<_instance_t>(instance, find_symbol_by_name(name));
}

Expand Down Expand Up @@ -225,7 +225,7 @@ namespace phoenix {
/// \return The initialized instance.
template <typename _instance_t>
typename std::enable_if<std::is_base_of_v<instance, _instance_t>, std::shared_ptr<_instance_t>>::type
allocate_instance(const std::string& name) {
allocate_instance(std::string_view name) {
return allocate_instance<_instance_t>(find_symbol_by_name(name));
}

Expand All @@ -239,7 +239,7 @@ namespace phoenix {
/// \param name The name of the instance to initialize (ie. 'STT_309_WHISTLER')
template <typename _instance_t>
typename std::enable_if<std::is_base_of_v<instance, _instance_t>, void>::type
allocate_instance(const std::shared_ptr<_instance_t>& instance, const std::string& name) {
allocate_instance(const std::shared_ptr<_instance_t>& instance, std::string_view name) {
allocate_instance<_instance_t>(instance, find_symbol_by_name(name));
}

Expand Down Expand Up @@ -389,7 +389,7 @@ namespace phoenix {
/// \throws illegal_external if The number of parameters of the definition and callback is not the same.
/// \throws runtime_error if any other error occurs.
template <typename R, typename... P>
void register_external(const std::string& name, const std::function<R(P...)>& callback) {
void register_external(std::string_view name, const std::function<R(P...)>& callback) {
auto* sym = find_symbol_by_name(name);
if (sym == nullptr)
return;
Expand Down Expand Up @@ -460,15 +460,15 @@ namespace phoenix {

/// \brief Registers an external function.
///
/// Same as #register_external(const std::string&, std::function). This method is here to bypass a template
/// Same as #register_external(std::string_view, std::function). This method is here to bypass a template
/// deduction failure when passing lambdas directly.
///
/// \tparam T The type of external function to register.
/// \param name The name of the external to register.
/// \param cb The callback function to register.
/// \see #register_external(const std::string&, std::function)
/// \see #register_external(std::string_view, std::function)
template <typename T>
void register_external(const std::string& name, const T& cb) {
void register_external(std::string_view name, const T& cb) {
register_external(name, std::function {cb});
}

Expand All @@ -482,10 +482,10 @@ namespace phoenix {
/// \tparam P The parameters types of the external.
/// \param name The name of the function to override.
/// \param callback The C++ function to register as the external.
/// \see #register_external(const std::string&, std::function)
/// \see #register_external(std::string_view, std::function)
/// \todo Deduplicate source code!
template <typename R, typename... P>
void override_function(const std::string& name, const std::function<R(P...)>& callback) {
void override_function(std::string_view name, const std::function<R(P...)>& callback) {
auto* sym = find_symbol_by_name(name);
if (sym == nullptr)
throw vm_exception {"symbol not found"};
Expand Down Expand Up @@ -555,15 +555,15 @@ namespace phoenix {

/// \brief Overrides a function in Daedalus code with an external definition.
///
/// Same as #override_function(const std::string&, std::function). This method is here to bypass a template
/// Same as #override_function(std::string_view, std::function). This method is here to bypass a template
/// deduction failure when passing lambdas directly.
///
/// \tparam T The type of external function to register.
/// \param name The name of the function to override.
/// \param cb The callback function to register.
/// \see #override_function(const std::string&, std::function)
/// \see #override_function(std::string_view, std::function)
template <typename T>
void override_function(const std::string& name, const T& cb) {
void override_function(std::string_view name, const T& cb) {
override_function(name, std::function {cb});
}

Expand Down
6 changes: 3 additions & 3 deletions source/script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace phoenix {
return &_m_symbols[index];
}

const symbol* script::find_symbol_by_name(const std::string& name) const {
const symbol* script::find_symbol_by_name(std::string_view name) const {
std::string up {name};
std::transform(up.begin(), up.end(), up.begin(), ::toupper);

Expand All @@ -155,7 +155,7 @@ namespace phoenix {
return &_m_symbols[index];
}

symbol* script::find_symbol_by_name(const std::string& name) {
symbol* script::find_symbol_by_name(std::string_view name) {
std::string up {name};
std::transform(up.begin(), up.end(), up.begin(), ::toupper);

Expand All @@ -174,7 +174,7 @@ namespace phoenix {
return nullptr;
}

void script::enumerate_instances_by_class_name(const std::string& name,
void script::enumerate_instances_by_class_name(std::string_view name,
const std::function<void(symbol&)>& callback) {
auto* cls = find_symbol_by_name(name);
if (cls == nullptr)
Expand Down

0 comments on commit ae77600

Please sign in to comment.