mirror of
https://github.com/CrowCpp/Crow.git
synced 2024-06-07 21:10:44 +00:00
preparing compile time url parsing
This commit is contained in:
parent
4fdff79719
commit
5e5d696884
3
.gitignore
vendored
3
.gitignore
vendored
@ -19,3 +19,6 @@
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
example
|
||||
unittest
|
||||
|
5
Makefile
5
Makefile
@ -1,4 +1,4 @@
|
||||
all: example
|
||||
all: example unittest
|
||||
example: example.cpp flask.h http_server.h http_connection.h parser.h http_response.h
|
||||
g++ -g -std=c++11 -o example example.cpp http-parser/http_parser.c -pthread -lboost_system -lboost_thread -I http-parser/
|
||||
test: example
|
||||
@ -6,3 +6,6 @@ test: example
|
||||
./example &
|
||||
python test.py || exit 0
|
||||
pkill example
|
||||
unittest: unittest.cpp routing.h
|
||||
g++ -g -std=c++11 -o unittest unittest.cpp
|
||||
|
||||
|
2
flask.h
2
flask.h
@ -32,7 +32,7 @@ namespace flask
|
||||
template <typename F>
|
||||
void route(const std::string& url, F f)
|
||||
{
|
||||
auto yameHandler = [f]{
|
||||
auto yameHandler = [f = std::move(f)]{
|
||||
return response(f());
|
||||
};
|
||||
yameHandlers_.emplace(url, yameHandler);
|
||||
|
39
routing.h
Normal file
39
routing.h
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "utility.h"
|
||||
|
||||
namespace flask
|
||||
{
|
||||
namespace black_magic
|
||||
{
|
||||
constexpr bool is_equ_n(StrWrap a, int ai, StrWrap b, int bi, int n)
|
||||
{
|
||||
return n == 0 ? true : a[ai] != b[bi] ? false : is_equ_n(a,ai+1,b,bi+1,n-1);
|
||||
}
|
||||
|
||||
constexpr bool is_int(StrWrap s, int i)
|
||||
{
|
||||
return is_equ_n(s, i, "<int>", 0, 5);
|
||||
}
|
||||
|
||||
constexpr bool is_str(StrWrap s, int i)
|
||||
{
|
||||
return is_equ_n(s, i, "<str>", 0, 5);
|
||||
}
|
||||
|
||||
//constexpr ? parse_route(StrWrap s)
|
||||
//{
|
||||
//return
|
||||
//}
|
||||
}
|
||||
|
||||
class Router
|
||||
{
|
||||
public:
|
||||
constexpr Router(black_magic::StrWrap s)
|
||||
{
|
||||
}
|
||||
};
|
||||
}
|
27
unittest.cpp
Normal file
27
unittest.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include "routing.h"
|
||||
#include <functional>
|
||||
#include "utility.h"
|
||||
|
||||
using namespace flask;
|
||||
using namespace flask::black_magic;
|
||||
|
||||
template <int N> struct T{};
|
||||
|
||||
int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
throw T<is_int("1<int>22",0)>();
|
||||
}
|
||||
catch(T<0>)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
throw T<is_int("1<int>22",1)>();
|
||||
}
|
||||
catch(T<1>)
|
||||
{
|
||||
}
|
||||
}
|
52
utility.h
Normal file
52
utility.h
Normal file
@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
namespace flask
|
||||
{
|
||||
namespace black_magic
|
||||
{
|
||||
struct OutOfRange
|
||||
{
|
||||
OutOfRange(unsigned pos, unsigned length) {}
|
||||
};
|
||||
constexpr unsigned requires_in_range( unsigned i, unsigned len )
|
||||
{
|
||||
return i >= len ? throw OutOfRange(i, len) : i;
|
||||
}
|
||||
|
||||
// from http://akrzemi1.wordpress.com/2011/05/11/parsing-strings-at-compile-time-part-i/
|
||||
class StrWrap
|
||||
{
|
||||
const char * const begin_;
|
||||
unsigned size_;
|
||||
|
||||
public:
|
||||
template< unsigned N >
|
||||
constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) {
|
||||
static_assert( N >= 1, "not a string literal");
|
||||
}
|
||||
|
||||
constexpr char operator[]( unsigned i ) {
|
||||
return requires_in_range(i, size_), begin_[i];
|
||||
}
|
||||
|
||||
constexpr operator const char *() {
|
||||
return begin_;
|
||||
}
|
||||
|
||||
constexpr unsigned size() {
|
||||
return size_;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
constexpr int find_closing_tag(StrWrap s, std::size_t p)
|
||||
{
|
||||
return s[p] == '>' ? p : find_closing_tag(s, p+1);
|
||||
}
|
||||
|
||||
constexpr int count(StrWrap s, int i=0)
|
||||
{
|
||||
return i == s.size() ? 0 : s[i] == '<' ? 1+count(s,i+1) : count(s,i+1);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user