Commit 0ab17b40 authored by Mathieu STEFANI's avatar Mathieu STEFANI

Played with macros for a more convenient header registration system

parent 5cb7c67f
......@@ -20,6 +20,13 @@ namespace {
std::unordered_map<std::string, Registry::RegistryFunc> registry;
}
RegisterHeader(Accept);
RegisterHeader(ContentLength);
RegisterHeader(ContentType);
RegisterHeader(Host);
RegisterHeader(ContentEncoding);
RegisterHeader(UserAgent);
void
Registry::registerHeader(std::string name, Registry::RegistryFunc func)
{
......@@ -133,19 +140,6 @@ Collection::getImpl(const std::string& name) const {
return std::make_pair(true, it->second);
}
namespace {
struct AtInit {
AtInit() {
Registry::registerHeader<ContentLength>();
Registry::registerHeader<ContentType>();
Registry::registerHeader<Host>();
Registry::registerHeader<Accept>();
Registry::registerHeader<UserAgent>();
Registry::registerHeader<ContentEncoding>();
}
} atInit;
}
} // namespace Header
} // namespace Http
......
......@@ -101,6 +101,27 @@ struct Registry {
static bool isRegistered(const std::string& name);
};
template<typename H>
struct Registrar {
static_assert(IsHeader<H>::value, "Registrar only works with header types");
Registrar() {
Registry::registerHeader<H>();
}
};
/* Crazy macro machinery to generate a unique variable name
* Don't touch it !
*/
#define CAT(a, b) CAT_I(a, b)
#define CAT_I(a, b) a ## b
#define UNIQUE_NAME(base) CAT(base, __LINE__)
#define RegisterHeader(Header) \
Registrar<Header> UNIQUE_NAME(CAT(CAT_I(__, Header), __))
} // namespace Header
} // namespace Http
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment