|
| 1 | +#include <meta> |
| 2 | +#include <string> |
| 3 | +#include <fstream> |
| 4 | +#include <filesystem> |
| 5 | +#include <format> |
| 6 | +#include <unordered_map> |
| 7 | +#include <set> |
| 8 | + |
| 9 | +#include <rsl/span> |
| 10 | + |
| 11 | +namespace __impl { |
| 12 | + |
| 13 | +struct InjectPath { |
| 14 | + char const* msg; |
| 15 | +}; |
| 16 | +} // namespace __impl |
| 17 | + |
| 18 | +static consteval auto Inject(char const* msg) { |
| 19 | + return __impl::InjectPath{std::define_static_string(msg)}; |
| 20 | +}; |
| 21 | + |
| 22 | +using injections_type = std::unordered_map<std::filesystem::path, std::string>; |
| 23 | + |
| 24 | +struct injection_info { |
| 25 | + injections_type entries; |
| 26 | +}; |
| 27 | + |
| 28 | +injection_info& injection_info() { |
| 29 | + static struct injection_info injected{}; |
| 30 | + return injected; |
| 31 | +} |
| 32 | + |
| 33 | +injections_type& injections() { |
| 34 | + return injection_info().entries; |
| 35 | +} |
| 36 | + |
| 37 | +std::string generate_cmake() { |
| 38 | + std::string out{}; |
| 39 | + auto& info = injection_info(); |
| 40 | + // boilerplate |
| 41 | + |
| 42 | + return out; |
| 43 | +} |
| 44 | + |
| 45 | +template <std::meta::info func> |
| 46 | +void process_injection() { |
| 47 | + constexpr auto vec = rsl::span{std::define_static_array(std::meta::annotations_of(func))}; |
| 48 | + constexpr std::string_view annotationInfo = |
| 49 | + std::define_static_string(std::meta::extract<__impl::InjectPath>(vec[0]).msg); |
| 50 | + auto const path = std::filesystem::path(annotationInfo); |
| 51 | + std::format_to(std::back_inserter(injections()[path]), "{}\n", [:func:]()); |
| 52 | +} |
| 53 | + |
| 54 | +void generate_from_injection() { |
| 55 | + const auto common_includes_file = std::filesystem::path("injected.cpp"); |
| 56 | + std::ofstream all_injections{common_includes_file}; |
| 57 | + for (const auto [path, injection] : injections()) { |
| 58 | + if (path.has_parent_path()) |
| 59 | + std::filesystem::create_directories(path.parent_path()); |
| 60 | + std::ofstream file{path}; |
| 61 | + if (file) |
| 62 | + file << injection; |
| 63 | + all_injections << std::format("#include \"{}\"\n", path.string()); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +template <std::meta::info source> |
| 68 | +constexpr bool gather_injections() { |
| 69 | + template for (constexpr auto member : |
| 70 | + define_static_array(members_of(source, std::meta::access_context::current()))) { |
| 71 | + if constexpr (is_function(member)) |
| 72 | + process_injection<member>(); |
| 73 | + } |
| 74 | + return true; |
| 75 | +} |
| 76 | + |
| 77 | +#define RSLINJECT_ENABLE_NS(NS) \ |
| 78 | + namespace { \ |
| 79 | + [[maybe_unused]] static bool const _rsl_injection_enabled = gather_injections<^^NS>(); \ |
| 80 | + } |
| 81 | + |
| 82 | +int main() { |
| 83 | + generate_from_injection(); |
| 84 | +} |
0 commit comments