diff options
author | ipknHama <ipknhama@gmail.com> | 2016-09-21 23:11:06 +0900 |
---|---|---|
committer | ipknHama <ipknhama@gmail.com> | 2016-09-21 23:11:06 +0900 |
commit | 3081e4e1a82a4efd8feff68850c4cc04af230cd7 (patch) | |
tree | 3ad16ef2e940abd2d47c4ac3ca224365387b7f37 /include/crow/common.h | |
parent | 8b04940d2f28290451db439ad29155a0b8771ba3 (diff) | |
download | crow-3081e4e1a82a4efd8feff68850c4cc04af230cd7.tar.gz crow-3081e4e1a82a4efd8feff68850c4cc04af230cd7.zip |
Cleanup include folder into crow subfolder
- only crow.h is exposed now
Diffstat (limited to 'include/crow/common.h')
-rw-r--r-- | include/crow/common.h | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/include/crow/common.h b/include/crow/common.h new file mode 100644 index 0000000..84d1361 --- /dev/null +++ b/include/crow/common.h @@ -0,0 +1,137 @@ +#pragma once + +#include <vector> +#include <string> +#include <stdexcept> +#include <iostream> +#include "crow/utility.h" + +namespace crow +{ + enum class HTTPMethod + { +#ifndef DELETE + DELETE = 0, + GET, + HEAD, + POST, + PUT, + CONNECT, + OPTIONS, + TRACE, +#endif + + Delete = 0, + Get, + Head, + Post, + Put, + Connect, + Options, + Trace, + }; + + inline std::string method_name(HTTPMethod method) + { + switch(method) + { + case HTTPMethod::Delete: + return "DELETE"; + case HTTPMethod::Get: + return "GET"; + case HTTPMethod::Head: + return "HEAD"; + case HTTPMethod::Post: + return "POST"; + case HTTPMethod::Put: + return "PUT"; + case HTTPMethod::Connect: + return "CONNECT"; + case HTTPMethod::Options: + return "OPTIONS"; + case HTTPMethod::Trace: + return "TRACE"; + } + return "invalid"; + } + + enum class ParamType + { + INT, + UINT, + DOUBLE, + STRING, + PATH, + + MAX + }; + + struct routing_params + { + std::vector<int64_t> int_params; + std::vector<uint64_t> uint_params; + std::vector<double> double_params; + std::vector<std::string> string_params; + + void debug_print() const + { + std::cerr << "routing_params" << std::endl; + for(auto i:int_params) + std::cerr<<i <<", " ; + std::cerr<<std::endl; + for(auto i:uint_params) + std::cerr<<i <<", " ; + std::cerr<<std::endl; + for(auto i:double_params) + std::cerr<<i <<", " ; + std::cerr<<std::endl; + for(auto& i:string_params) + std::cerr<<i <<", " ; + std::cerr<<std::endl; + } + + template <typename T> + T get(unsigned) const; + + }; + + template<> + inline int64_t routing_params::get<int64_t>(unsigned index) const + { + return int_params[index]; + } + + template<> + inline uint64_t routing_params::get<uint64_t>(unsigned index) const + { + return uint_params[index]; + } + + template<> + inline double routing_params::get<double>(unsigned index) const + { + return double_params[index]; + } + + template<> + inline std::string routing_params::get<std::string>(unsigned index) const + { + return string_params[index]; + } +} + +#ifndef CROW_MSVC_WORKAROUND +constexpr crow::HTTPMethod operator "" _method(const char* str, size_t /*len*/) +{ + return + crow::black_magic::is_equ_p(str, "GET", 3) ? crow::HTTPMethod::Get : + crow::black_magic::is_equ_p(str, "DELETE", 6) ? crow::HTTPMethod::Delete : + crow::black_magic::is_equ_p(str, "HEAD", 4) ? crow::HTTPMethod::Head : + crow::black_magic::is_equ_p(str, "POST", 4) ? crow::HTTPMethod::Post : + crow::black_magic::is_equ_p(str, "PUT", 3) ? crow::HTTPMethod::Put : + crow::black_magic::is_equ_p(str, "OPTIONS", 7) ? crow::HTTPMethod::Options : + crow::black_magic::is_equ_p(str, "CONNECT", 7) ? crow::HTTPMethod::Connect : + crow::black_magic::is_equ_p(str, "TRACE", 5) ? crow::HTTPMethod::Trace : + throw std::runtime_error("invalid http method"); +} +#endif |