obs/gs/effect-parameter: Use string_view over std::string

Using std::string_view over std::string (and const std::string&) has the advantage that we skip potential temporary std::string objects that are immediately thrown away, thus slowing down the code. It can also be implicitly cast to std::string, which makes it compatible with existing code that uses std::string.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2020-05-13 08:38:05 +02:00
parent 9db3dfd874
commit 3a40a63832
3 changed files with 11 additions and 12 deletions

View File

@ -36,6 +36,7 @@
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <string_view>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>

View File

@ -112,11 +112,9 @@ try {
return *this; return *this;
} }
std::string gs::effect_parameter::get_name() std::string_view gs::effect_parameter::get_name()
{ {
const char* name_c = get()->name; return std::string_view{get()->name};
std::size_t name_len = strnlen(name_c, 256);
return name_c ? std::string(name_c, name_c + name_len) : std::string();
} }
gs::effect_parameter::type gs::effect_parameter::get_type() gs::effect_parameter::type gs::effect_parameter::get_type()
@ -166,11 +164,11 @@ gs::effect_parameter gs::effect_parameter::get_annotation(std::size_t idx)
return effect_parameter(get()->annotations.array + idx, this); return effect_parameter(get()->annotations.array + idx, this);
} }
gs::effect_parameter gs::effect_parameter::get_annotation(std::string name) gs::effect_parameter gs::effect_parameter::get_annotation(const std::string_view name)
{ {
for (std::size_t idx = 0; idx < get()->annotations.num; idx++) { for (std::size_t idx = 0; idx < get()->annotations.num; idx++) {
auto ptr = get()->annotations.array + idx; auto ptr = get()->annotations.array + idx;
if (strcmp(ptr->name, name.c_str()) == 0) { if (name == std::string_view{ptr->name}) {
return gs::effect_parameter(ptr, this); return gs::effect_parameter(ptr, this);
} }
} }
@ -178,7 +176,7 @@ gs::effect_parameter gs::effect_parameter::get_annotation(std::string name)
return nullptr; return nullptr;
} }
bool gs::effect_parameter::has_annotation(std::string name) bool gs::effect_parameter::has_annotation(const std::string_view name)
{ {
auto eprm = get_annotation(name); auto eprm = get_annotation(name);
if (eprm) if (eprm)
@ -186,7 +184,7 @@ bool gs::effect_parameter::has_annotation(std::string name)
return false; return false;
} }
bool gs::effect_parameter::has_annotation(std::string name, effect_parameter::type type) bool gs::effect_parameter::has_annotation(const std::string_view name, effect_parameter::type type)
{ {
auto eprm = get_annotation(name); auto eprm = get_annotation(name);
if (eprm) if (eprm)

View File

@ -61,15 +61,15 @@ namespace gs {
effect_parameter(effect_parameter&& rhs) noexcept; effect_parameter(effect_parameter&& rhs) noexcept;
effect_parameter& operator=(effect_parameter&& rhs) noexcept; effect_parameter& operator=(effect_parameter&& rhs) noexcept;
std::string get_name(); std::string_view get_name();
type get_type(); type get_type();
std::size_t count_annotations(); std::size_t count_annotations();
effect_parameter get_annotation(std::size_t idx); effect_parameter get_annotation(std::size_t idx);
effect_parameter get_annotation(std::string name); effect_parameter get_annotation(const std::string_view name);
bool has_annotation(std::string name); bool has_annotation(const std::string_view name);
bool has_annotation(std::string name, effect_parameter::type type); bool has_annotation(const std::string_view name, effect_parameter::type type);
public /* Memory API */: public /* Memory API */:
std::size_t get_default_value_size_in_bytes() std::size_t get_default_value_size_in_bytes()