capydi
Loading...
Searching...
No Matches
CapydiTarget.hpp
Go to the documentation of this file.
1#ifndef CAPYGATOR_CAPYDI_TARGET_HPP_
2#define CAPYGATOR_CAPYDI_TARGET_HPP_
3
4#include "FileSystem.hpp"
5
6#include <vector>
7#include <format>
8#include <ranges>
9#include <algorithm>
10#include <string>
11
13{
14public:
15 void include(const FileSystem::PathType& file_contents)
16 {
17 includes_.push_back(file_contents);
18 }
19
21 {
22 FileSystem::FileContentType includes_section{};
23 std::vector<FileSystem::FileContentType> config_identifiers{};
24
25 includes_section +=
26 "#define STORE_GENERATE_UNIQUE_NAME GENERATE_UNIQUE_NAME\n";
27
28 for (const FileSystem::PathType& include_path: this->includes_)
29 {
31 config_identifier = as_config_identifier(include_path);
32
33 includes_section += std::format(
34 "#undef GENERATE_UNIQUE_NAME\n"
35 "#define GENERATE_UNIQUE_NAME {}\n"
36 "#include \"{}\"\n\n",
37 config_identifier,
38 include_path.string()
39 );
40 // file_content += "#include \"" + include_path.string() + "\"\n";
41
42 config_identifiers.push_back(std::move(config_identifier));
43 }
44
45 includes_section +=
46 "#undef GENERATE_UNIQUE_NAME\n"
47 "#define GENERATE_UNIQUE_NAME STORE_GENERATE_UNIQUE_NAME\n"
48 "#undef STORE_GENERATE_UNIQUE_NAME\n";
49
50 const FileSystem::FileContentType config_identifiers_string = config_identifiers
51 | std::views::transform([](const auto& s) { return "std::move(" + s + ")"; })
52 | std::views::join_with(FileSystem::FileContentType(",\n "))
53 | std::ranges::to<FileSystem::FileContentType>();
54
55 const FileSystem::FileContentType container_builder_code = std::format(
56 "namespace capy::di\n"
57 "{{\n"
58 "\n"
59 "[[nodiscard]] inline auto build_up_container()\n"
60 "{{\n"
61 " auto configs_tuple = std::tuple_cat(\n"
62 " {}\n"
63 " );\n"
64 "\n"
65 " return std::apply([]<typename... Configs>(Configs&&... configs) {{\n"
66 " return capy::di::DI {{\n"
67 " std::forward<Configs>(configs)...\n"
68 " }};\n"
69 " }}, std::move(configs_tuple));\n"
70 "}}\n"
71 "\n"
72 "}}\n",
73 config_identifiers_string
74 );
75
76 return std::format(
77 "{}\n{}\n{}\n{}\n{}",
78 HPP_INCLUDE_GUARD_BEGIN,
79 HPP_DEFAULT_INCLUDES,
80 includes_section,
81 container_builder_code,
82 HPP_INCLUDE_GUARD_END
83 );
84 }
85
87 const FileSystem::PathType& path
88 ) {
89 FileSystem::FileContentType path_string = path.string();
90
91 std::ranges::replace_if(path_string, [](auto character) {
92 return !std::isalnum(static_cast<unsigned char>(character));
93 }, '_');
94
95 return "CAPYDI_CONFIG_" + path_string;
96 }
97
98private:
99 static constexpr std::string_view HPP_INCLUDE_GUARD_BEGIN
100 = "#ifndef CAPYDI_DI_AGGREGATED_HPP_\n"
101 "#define CAPYDI_DI_AGGREGATED_HPP_\n";
102
103 static constexpr std::string_view HPP_DEFAULT_INCLUDES
104 = "#include <capydi/Container.hpp>\n"
105 "#include <tuple>\n";
106
107 static constexpr std::string_view HPP_INCLUDE_GUARD_END
108 = "#endif // !CAPYDI_DI_AGGREGATED_HPP_\n";
109
110private:
111 std::vector<FileSystem::PathType> includes_;
112};
113
114#endif // !CAPYGATOR_CAPYDI_TARGET_HPP_
Definition CapydiTarget.hpp:13
FileSystem::FileContentType to_file_content() const
Definition CapydiTarget.hpp:20
void include(const FileSystem::PathType &file_contents)
Definition CapydiTarget.hpp:15
static FileSystem::FileContentType as_config_identifier(const FileSystem::PathType &path)
Definition CapydiTarget.hpp:86
std::filesystem::path PathType
Definition FileSystem.hpp:9
std::string FileContentType
Definition FileSystem.hpp:12