Skip to content

Commit

Permalink
Minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-jung committed Aug 6, 2023
1 parent a1d800d commit 16e04bc
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 208 deletions.
6 changes: 2 additions & 4 deletions examples/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ std::string to_json(const auto& value) {
}

namespace json_example {

struct foo_t {
int i { 42 };
float f { 1337.0f };
const char* s = "Hello World!";
struct bar_t {
int i { 0 };
} bar;
// todo
// std::array<int, 4> numbers { 1, 2, 3, 4 };
std::vector<int> numbers { 1, 2, 3, 4 };
std::array<int, 4> numbers { 1, 2, 3, 4 };
};

}
Expand Down
1 change: 0 additions & 1 deletion tooling/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
add_library(introspect_lib STATIC
data/aggregator.cpp
data/types.cpp
data/renderer.cpp
engine/ast_traversal_tool.cpp
Expand Down
10 changes: 0 additions & 10 deletions tooling/data/aggregator.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions tooling/data/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,6 @@ struct fmt::formatter<namespace_wrapper_t>

namespace data {

// std::string erase_substring(std::string input, const std::string& substring) {
// std::string::size_type pos = 0u;
// while((pos = input.find(substring, pos)) < input.size()) {
// input.erase(pos, substring.size());
// }
// return input;
// }

// std::string generate_unique_parameter_name() {
// static std::uint64_t id = 0;
// return fmt::format("p{}", ++id);
// }

std::string render_class_definition(const data::reflection_aggregator_t::entry_pointer_t<data::record_t>& record) {
std::string template_declaration;
if(!record->template_arguments.empty()) {
Expand Down Expand Up @@ -210,11 +197,6 @@ R"(struct global_t {{
return fmt::format(global_template, global);
}

// bool is_overloaded(const function_decl_t& function, const std::set<function_decl_t>& function_list) {
// const auto count = std::count_if(function_list.begin(), function_list.end(), [&function](const auto& elem){ return function.name == elem.name;});
// return count > 1;
// }

std::string render_field_description(const std::vector<field_decl_t>& fields) {
std::string result;
int i = 0;
Expand Down
188 changes: 165 additions & 23 deletions tooling/data/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,183 @@

namespace data {

bool operator==(const field_decl_t& lhs, const field_decl_t& rhs) {
return lhs.name == rhs.name;
std::string remove_substring(std::string str, std::string removal) {
std::string::size_type pos = 0;
while(pos = str.find(removal, pos), pos != str.npos) {
str.erase(pos, removal.size());
}
return str;
}

bool operator<(const field_decl_t& lhs, const field_decl_t& rhs) {
return lhs.name < rhs.name;

std::string template_argument_t::get_value(std::string prefix) const {
if(auto* type = std::get_if<type_t*>(&value)) {
return (*type)->get_name(prefix);
} else {
return std::get<std::string>(value);
}
}

bool operator==(const parameter_decl_t& lhs, const parameter_decl_t& rhs) {
return lhs.type == rhs.type;
decl_t::decl_t(std::string name, std::string qualified_namespace) :
name(std::move(name)),
qualified_namespace(std::move(qualified_namespace))
{}

std::string decl_t::get_namespace(namespace_option_t option) const {
switch(option) {
case namespace_option_t::qualified:
return qualified_namespace;
case namespace_option_t::unqualified:
return remove_substring(qualified_namespace, "inline ");
case namespace_option_t::none:
return "";
}
return "";
}

bool operator<(const parameter_decl_t& lhs, const parameter_decl_t& rhs) {
return lhs.type < rhs.type;
std::string decl_t::get_name(std::string prefix, namespace_option_t namespace_option) const {
std::string namespace_str = get_namespace(namespace_option);
return fmt::format(
"{}{}{}{}",
prefix,
namespace_str,
namespace_str.empty() ? "" : "::",
name
);
}

bool operator==(const function_decl_t& lhs, const function_decl_t& rhs) {
return
lhs.name == rhs.name &&
lhs.is_const == rhs.is_const &&
lhs.ref_qualifier == rhs.ref_qualifier &&
std::equal(lhs.parameter.begin(), lhs.parameter.end(), rhs.parameter.begin(), rhs.parameter.end());
pointer_t::pointer_t(const type_t* type, cv_qualifier_t cv_qualifier) : type(std::move(type)), cv_qualifier(cv_qualifier) {}

std::string pointer_t::get_name(std::string prefix, namespace_option_t namespace_option) const {
return fmt::format(
"{}{}{}{}*",
cv_qualifier == cv_qualifier_t::const_ ? "const " : "",
cv_qualifier == cv_qualifier_t::volatile_ ? "volatile " : "",
cv_qualifier == cv_qualifier_t::const_volatile ? "const volatile " : "",
type->get_name(prefix, namespace_option)
);
}

bool operator<(const function_decl_t& lhs, const function_decl_t& rhs) {
if(lhs.name != rhs.name) {
return lhs.name < rhs.name;
}
if(lhs.is_const != rhs.is_const) {
return lhs.is_const < rhs.is_const;
constant_array_t::constant_array_t(
const type_t* type,
std::size_t size
) :
type(type),
size(size)
{}

std::string constant_array_t::get_name(std::string prefix, namespace_option_t namespace_option) const {
return fmt::format(
"{}[{}]",
type->get_name(prefix, namespace_option),
size
);
}

cv_qualified_type_t::cv_qualified_type_t(const type_t* type, cv_qualifier_t cv_qualifier) :
type(std::move(type)),
cv_qualifier(cv_qualifier)
{}

std::string cv_qualified_type_t::get_name(std::string prefix, namespace_option_t namespace_option) const {
if(dynamic_cast<const pointer_t*>(type) == nullptr) {
return fmt::format(
"{0}{1}{2}{3}",
cv_qualifier == cv_qualifier_t::const_ ? "const " : "",
cv_qualifier == cv_qualifier_t::volatile_ ? "volatile " : "",
cv_qualifier == cv_qualifier_t::const_volatile ? "const volatile " : "",
type->get_name(prefix, namespace_option)
);
} else {
return fmt::format(
"{0}{1}{2}{3}",
type->get_name(prefix, namespace_option),
cv_qualifier == cv_qualifier_t::const_ ? " const" : "",
cv_qualifier == cv_qualifier_t::volatile_ ? " volatile" : "",
cv_qualifier == cv_qualifier_t::const_volatile ? " const volatile" : ""
);
}
if(lhs.ref_qualifier != rhs.ref_qualifier) {
return lhs.ref_qualifier < rhs.ref_qualifier;
}

reference_t::reference_t(const type_t* type, cv_qualifier_t cv_qualifier, ref_qualifier_t ref_qualifier) : type(std::move(type)), cv_qualifier(cv_qualifier), ref_qualifier(ref_qualifier) {}

std::string reference_t::get_name(std::string prefix , namespace_option_t namespace_option) const {
return fmt::format(
"{}{}{}{}{}{}",
cv_qualifier == cv_qualifier_t::const_ ? "const " : "",
cv_qualifier == cv_qualifier_t::volatile_ ? "volatile " : "",
cv_qualifier == cv_qualifier_t::const_volatile ? "const volatile " : "",
type->get_name(prefix, namespace_option),
ref_qualifier == ref_qualifier_t::lvalue ? "&" : "",
ref_qualifier == ref_qualifier_t::rvalue ? "&&" : ""
);
}

enum_t::enum_t(
std::string name,
std::string qualified_namespace,
bool scoped,
std::vector<std::string> values,
std::optional<const type_t*> underlying_type
) :
decl_t(std::move(name),
std::move(qualified_namespace)),
scoped(scoped),
values(std::move(values)),
underlying_type(underlying_type)
{}

std::string function_decl_t::signature(std::string record_name) const {
return fmt::format(
"{}{}{} {}::{}({}) {}{}",
is_static ? "static " : "",
is_constexpr ? "constexpr " : "",
result ? result->get_name() : "<unknown>",
record_name,
name,
fmt::join(parameter, ", "),
is_const ? "const" : "",
ref_qualifier
);
}

builtin_t::builtin_t(std::string name) : name(name) {}

std::string builtin_t::get_name(std::string prefix, namespace_option_t namespace_option) const {
return name;
}

record_t::record_t(
std::string name,
std::string qualified_namespace,
bool is_struct
) :
decl_t(std::move(name),
std::move(qualified_namespace)),
is_struct(is_struct)
{}

std::string record_t::get_name(std::string prefix, namespace_option_t namespace_option) const {
auto template_parameter_list = template_arguments.empty() ? "" : fmt::format("<{}>", transform_join(template_arguments, ", ", [prefix](const template_argument_t& arg) { return arg.get_value(prefix); }));
std::string namespace_str;
if(parent != nullptr) {
namespace_str = fmt::format("{}{}{}", namespace_str, namespace_str.empty() ? "" : "::", parent->get_name("", namespace_option_t::unqualified));
} else {
namespace_str = get_namespace(namespace_option);
}
return std::lexicographical_compare(lhs.parameter.begin(), lhs.parameter.end(), rhs.parameter.begin(), rhs.parameter.end());
auto decl = fmt::format(
"{}{}{}{}{}",
prefix,
namespace_str,
namespace_str.empty() ? "" : "::",
template_parameter_list.empty() ? "" : "template ",
name
);
return fmt::format(
"{}{}",
decl,
template_parameter_list
);
}

}
Loading

0 comments on commit 16e04bc

Please sign in to comment.