Skip to content

Commit

Permalink
use scn
Browse files Browse the repository at this point in the history
  • Loading branch information
phisko committed Mar 6, 2023
1 parent bf79a83 commit 1eef2c0
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 88 deletions.
21 changes: 14 additions & 7 deletions kengine/adjustable/imgui/systems/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// putils
#include "putils/forward_to.hpp"
#include "putils/ini_file.hpp"
#include "putils/parse.hpp"
#include "putils/scn/scn.hpp"
#include "putils/split.hpp"
#include "putils/static_assert.hpp"
#include "putils/vector.hpp"
Expand Down Expand Up @@ -347,26 +347,33 @@ namespace kengine::adjustable::imgui {
*storage.ptr = storage.value;
};

const std::string_view view(s);
switch (value.type) {
case adjustable::value_type::Int: {
value.int_storage.value = putils::parse<int>(s);
const auto result = scn::scan_default(view, value.int_storage.value);
if (!result)
kengine_assert_failed(r, "{}", result.error().msg());
assign_ptr(value.int_storage);
break;
}
case adjustable::value_type::Float: {
value.float_storage.value = putils::parse<float>(s);
const auto result = scn::scan_default(view, value.float_storage.value);
if (!result)
kengine_assert_failed(r, "{}", result.error().msg());
assign_ptr(value.float_storage);
break;
}
case adjustable::value_type::Bool: {
value.bool_storage.value = putils::parse<bool>(s);
const auto result = scn::scan_default(view, value.bool_storage.value);
if (!result)
kengine_assert_failed(r, "{}", result.error().msg());
assign_ptr(value.bool_storage);
break;
}
case adjustable::value_type::Color: {
putils::color tmp;
tmp.rgba = putils::parse<unsigned int>(s);
value.color_storage.value = putils::to_normalized_color(tmp);
const auto result = scn::scan_default(view, value.color_storage.value);
if (!result)
kengine_assert_failed(r, "{}", result.error().msg());
assign_ptr(value.color_storage);
break;
}
Expand Down
2 changes: 2 additions & 0 deletions kengine/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Components and helpers that are accessed by most (if not all) other libraries.
* [selected](data/selected.md): tags an entity as selected by the user
* [transform](data/transform.md): an entity's position, rotation and scale
* [helpers](helpers/)
* [entt_formatter](helpers/entt_formatter.md): `fmt::formatter` specialization for `entt` types
* [entt_scanner](helpers/entt_scanner.md): `scn::scanner` specialization for `entt` types
* [new_entity_processor](helpers/new_entity_processor.md): automatically call a functor when entities enter a group

Sub-libraries:
Expand Down
23 changes: 23 additions & 0 deletions kengine/core/helpers/entt_scanner.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

// scn
#include <scn/scn.h>

// entt
#include <entt/entity/entity.hpp>
#include <entt/entity/handle.hpp>
#include <entt/entity/registry.hpp>

namespace scn {
template<>
struct scanner<entt::entity> : scn::empty_parser {
template <typename Context>
error scan(entt::entity & val, Context & ctx) {
std::underlying_type_t<entt::entity> non_enum_value;
const auto result = scn::scan_usertype(ctx, "[{}]", non_enum_value);
if (result)
val = entt::entity(non_enum_value);
return result;
}
};
}
3 changes: 3 additions & 0 deletions kengine/core/helpers/entt_scanner.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# [entt_scanner](entt_scanner.hpp)

`scn::scanner` specialization for `entt::entity`.
2 changes: 1 addition & 1 deletion kengine/core/log/helpers/kengine_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// putils
#include "putils/string.hpp"
#include "putils/reflection_helpers/reflectible_formatter.hpp"
#include "putils/fmt/fmt.hpp"

// kengine
#include "kengine/core/helpers/entt_formatter.hpp"
Expand Down
12 changes: 6 additions & 6 deletions kengine/core/log/helpers/parse_command_line_severity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

// putils
#include "putils/command_line_arguments.hpp"
#include "putils/parse.hpp"
#include "putils/reflection_helpers/json_helper.hpp"
#include "putils/scn/scn.hpp"
#include "putils/split.hpp"

// kengine core/profiling
Expand Down Expand Up @@ -41,15 +40,15 @@ namespace kengine::core::log {

static std::optional<severity_control> command_line_severity;
if (command_line_severity != std::nullopt) {
kengine_logf(r, very_verbose, log_category, "Found pre-parsed {}", putils::reflection::to_json(*command_line_severity).dump(4));
kengine_logf(r, very_verbose, log_category, "Found pre-parsed {}", *command_line_severity);
return *command_line_severity;
}

command_line_severity = severity_control{};

for (const auto & [e, command_line] : r.view<command_line::arguments>().each()) {
const auto opts = putils::parse_arguments<options>(command_line.args);
kengine_logf(r, very_verbose, log_category, "Found {} in {}", putils::reflection::to_json(opts).dump(4), e);
kengine_logf(r, very_verbose, log_category, "Found {} in {}", opts, e);

command_line_severity->global_severity = opts.log_level;
const auto categories = putils::split(opts.log_category_levels.c_str(), ',');
Expand All @@ -61,8 +60,9 @@ namespace kengine::core::log {
}

const auto & key = key_value[0];
const auto value = putils::parse<severity>(key_value[1]);
command_line_severity->category_severities[key] = value;
severity value;
if (scn::scan_default(key_value[1], value))
command_line_severity->category_severities[key] = value;
}
}

Expand Down
9 changes: 6 additions & 3 deletions kengine/imgui/tool/systems/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// putils
#include "putils/forward_to.hpp"
#include "putils/ini_file.hpp"
#include "putils/parse.hpp"
#include "putils/scn/scn.hpp"
#include "putils/split.hpp"

// kengine core
Expand Down Expand Up @@ -136,8 +136,11 @@ namespace kengine::imgui::tool {

kengine_logf(r, verbose, log_category, "Initializing {}", name->name);
const auto & section = configuration.sections["Tools"];
if (const auto it = section.values.find(name->name.c_str()); it != section.values.end())
comp.enabled = putils::parse<bool>(it->second);
if (const auto it = section.values.find(name->name.c_str()); it != section.values.end()) {
const auto result = scn::scan_default(it->second, comp.enabled);
if (!result)
kengine_assert_failed(r, "{}", result.error().msg());
}

menu_entry * current_entry = &root_entry;
const auto entry_names = putils::split(name->name.c_str(), '/');
Expand Down
71 changes: 3 additions & 68 deletions kengine/meta/helpers/impl/match_string.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,13 @@
#endif

// putils
#include "putils/parse.hpp"
#include "putils/fmt/fmt.hpp"
#include "putils/reflection_helpers/imgui_helper.hpp"

// kengine core
#include "kengine/core/profiling/helpers/kengine_profiling_scope.hpp"

namespace kengine::meta {
namespace impl {
template<typename Member>
static bool match_attribute(const Member & member, const char * str) noexcept {
KENGINE_PROFILING_SCOPE;

if constexpr (requires { member.c_str(); }) {
return strstr(member.c_str(), str);
}
// else if constexpr (std::is_same_v<Member, const char *>::value)
// ImGui::LabelText(name, member);

else if constexpr (std::ranges::range<Member>
#ifdef KENGINE_SCRIPTING_LUA
&& !std::is_same<sol::object, Member>() && !std::is_base_of<sol::object, Member>()
#endif
) {
for (const auto & val : member)
if (match_attribute(val, str))
return true;
return false;
}

else if constexpr (putils::reflection::has_attributes<Member>() || putils::reflection::has_parents<Member>()) {
bool matches = false;
putils::reflection::for_each_attribute(member, [&](const auto & attr) noexcept {
matches |= match_attribute(attr.member, str);
});
return matches;
}

else if constexpr (std::is_same_v<Member, bool>) {
return (strcmp(str, "false") == 0 || strcmp(str, "true") == 0) && putils::parse<bool>(str) == member;
}
else if constexpr (std::is_same_v<Member, int>) {
return (str[0] == '-' || (str[0] >= '0' && str[0] <= '9')) && putils::parse<int>(str) == member;
}
else if constexpr (std::is_same_v<Member, unsigned int>) {
return str[0] >= '0' && str[0] <= '9' && putils::parse<unsigned int>(str) == member;
}
else if constexpr (std::is_same_v<Member, float>) {
return (str[0] == '-' || str[0] == '.' || (str[0] >= '0' && str[0] <= '9')) && putils::parse<float>(str) == member;
}
else if constexpr (std::is_same_v<Member, double>) {
return (str[0] == '-' || str[0] == '.' || (str[0] >= '0' && str[0] <= '9')) && putils::parse<double>(str) == member;
}
else if constexpr (std::is_same_v<Member, entt::entity> || std::is_same_v<Member, entt::handle>) {
return str[0] >= '0' && str[0] <= '9' && putils::parse<entt::entity>(str) == member;
}

return false;
}
}

template<typename T>
bool meta_component_implementation<match_string, T>::function(entt::const_handle e, const char * str) noexcept {
KENGINE_PROFILING_SCOPE;
Expand All @@ -85,20 +32,8 @@ namespace kengine::meta {
return false;
}

if constexpr (putils::reflection::has_class_name<T>())
if (strstr(putils::reflection::get_class_name<T>(), str)) {
kengine_log(*e.registry(), very_verbose, "meta::match_string", "Component's name matches, returning true");
return true;
}

bool matches = false;
putils::reflection::for_each_attribute(*comp, [&](const auto & attr) noexcept {
if (impl::match_attribute(attr.member, str)) {
kengine_logf(*e.registry(), very_verbose, "meta::match_string", "Component's '{}' attribute matches, returning true", attr.name);
matches = true;
}
});
return matches;
const putils::string<1024> comp_string("{}", *comp);
return comp_string.find(str) != std::string::npos;
}
}
}
6 changes: 4 additions & 2 deletions kengine/meta/imgui/entity_selector/systems/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

// putils
#include "putils/forward_to.hpp"
#include "putils/parse.hpp"
#include "putils/string.hpp"

// kengine core
#include "kengine/core/data/name.hpp"
#include "kengine/core/data/selected.hpp"
#include "kengine/core/helpers/entt_scanner.hpp"
#include "kengine/core/log/helpers/kengine_log.hpp"
#include "kengine/core/profiling/helpers/kengine_profiling_scope.hpp"
#include "kengine/core/sort/helpers/get_name_sorted_entities.hpp"
Expand Down Expand Up @@ -123,7 +123,9 @@ namespace kengine::meta::imgui::entity_selector {
result.display_text += " -- ";

bool matches = false;
if (std::isdigit(name_search[0]) && putils::parse<entt::entity>(name_search) == e) {

entt::entity id;
if (scn::scan_default(name_search, id) && id == e) {
kengine_log(r, very_verbose, log_category, "Match found in ID");
matches = true;
result.display_text += "ID";
Expand Down
2 changes: 1 addition & 1 deletion putils
Submodule putils updated 47 files
+4 −3 CMakeLists.txt
+3 −6 README.md
+18 −5 putils/command_line_arguments.inl
+0 −0 putils/fmt/enum_formatter.hpp
+0 −0 putils/fmt/enum_formatter.md
+12 −0 putils/fmt/fmt.hpp
+3 −0 putils/fmt/fmt.md
+11 −0 putils/fmt/formattable.hpp
+3 −0 putils/fmt/formattable.md
+24 −0 putils/fmt/optional_formatter.hpp
+3 −0 putils/fmt/optional_formatter.md
+70 −0 putils/fmt/reflectible_formatter.hpp
+0 −0 putils/fmt/reflectible_formatter.md
+8 −6 putils/fmt/tests/enum_formatter.tests.cpp
+22 −0 putils/fmt/tests/formattable.tests.cpp
+20 −0 putils/fmt/tests/optional_formatter.tests.cpp
+14 −4 putils/fmt/tests/reflectible_formatter.tests.cpp
+0 −30 putils/parse.cpp
+0 −48 putils/parse.hpp
+0 −51 putils/parse.inl
+0 −8 putils/parse.md
+6 −4 putils/reflection_helpers/json_helper.inl
+0 −38 putils/reflection_helpers/reflectible_formatter.hpp
+5 −2 putils/reflection_helpers/runtime_helper.inl
+40 −0 putils/scn/enum_scanner.hpp
+3 −0 putils/scn/enum_scanner.md
+31 −0 putils/scn/optional_scanner.hpp
+3 −0 putils/scn/optional_scanner.md
+106 −0 putils/scn/range_scanner.hpp
+3 −0 putils/scn/range_scanner.md
+64 −0 putils/scn/reflectible_scanner.hpp
+3 −0 putils/scn/reflectible_scanner.md
+36 −0 putils/scn/scan_element.hpp
+3 −0 putils/scn/scan_element.md
+11 −0 putils/scn/scannable.hpp
+3 −0 putils/scn/scannable.md
+7 −0 putils/scn/scn.hpp
+3 −0 putils/scn/scn.md
+27 −0 putils/scn/tests/enum_scanner.tests.cpp
+25 −0 putils/scn/tests/optional_scanner.tests.cpp
+69 −0 putils/scn/tests/range_scanner.tests.cpp
+101 −0 putils/scn/tests/reflectible_scanner.tests.cpp
+22 −0 putils/scn/tests/scannable.tests.cpp
+10 −11 putils/tests/command_line_arguments.tests.cpp
+0 −52 putils/tests/parse.tests.cpp
+6 −0 putils/tests/string.tests.cpp
+1 −0 vcpkg.json
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"dependencies": [
"gtest",
"fmt",
"scnlib",
"magic-enum",
"nlohmann-json",
"lua",
Expand Down

0 comments on commit 1eef2c0

Please sign in to comment.