I'm currently doing some work and I would like to abstract as much code as possible into a simple api. Essentially I want to write something like this:
int main()
{
Server server;
server.get<"/hello">([] { std::cout << "hello" << std::endl; });
server.get<"/goodbye">([] { std::cout << "goodbye" << std::endl; });
server.listen();
}
and have it create the necessary regex (using CTRE) at compile time.
However, when I need to match with these regexes, I need to have a list of them, so that I can loop through them and check each one. This has proven very difficult. One thing I've tried is to create something like this:
template<typename Contained>
class Container {
public:
constexpr Container() = default;
~Container() = default;
constexpr void add(Contained&& item) { m_temp_container.push_back(item); }
constexpr size_t arr_size() const {
size_t a = 64;
while(true)
{
if (size() > a) a+=64;
else break;
}
return a;
}
constexpr auto array() const {
constexpr size_t s = arr_size();
std::array<Contained, s> array;
for(int i = 0; i < size(); i++)
array[i] = std::move(m_temp_container[i]);
return array;
}
constexpr size_t size() const { return m_temp_container.size(); }
std::vector<Contained> m_temp_container;
};
struct CString
{
constexpr CString() : m_a(nullptr) {};
constexpr CString(const char* a) : m_a(a) {}
~CString() = default;
const char* m_a;
};
int main()
{
Container<CString> container;
container.add("hello");
container.add("there");
container.add("how");
container.add("are you");
const auto arr = container.array();
}
however, the compiler complains that
constexpr size_t s = arr_size();
is not a constant expression.
I'm not sure creating a list like this is even possible, at least not with my current knowledge of the standard library.
I could probably do this by having the user pass the Handlers as a complete object, perhaps by doing something like this:
int main()
{
constexpr auto get_handlers = {
Handler<"/hello">([] {...} ),
Handler<"/hello2">([] {...} )
};
Server server(get_handlers, ...);
server.listen();
}
but I would still like to know if it's in any way possible to construct a list by code at compile time and loop through it as if it were a const array at runtime.
Any ideas?