aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoripknHama <ipknhama@gmail.com>2014-04-03 01:38:08 +0900
committeripknHama <ipknhama@gmail.com>2014-04-03 08:29:22 +0900
commit5e5d69688435ffe8fc148af73fe33b029d9f6110 (patch)
tree1a37f2927e6825482404307346661a7b1c34300d
parent4fdff79719af8d024f176bbdc03a57516e932602 (diff)
downloadcrow-5e5d69688435ffe8fc148af73fe33b029d9f6110.tar.gz
crow-5e5d69688435ffe8fc148af73fe33b029d9f6110.zip
preparing compile time url parsing
-rw-r--r--.gitignore3
-rw-r--r--Makefile5
-rw-r--r--flask.h2
-rw-r--r--routing.h39
-rw-r--r--unittest.cpp27
-rw-r--r--utility.h52
6 files changed, 126 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 0def275..8245f7c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,3 +19,6 @@
*.exe
*.out
*.app
+
+example
+unittest
diff --git a/Makefile b/Makefile
index 9270e44..c5ef90c 100644
--- a/Makefile
+++ b/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
+
diff --git a/flask.h b/flask.h
index f61da16..fd33c61 100644
--- a/flask.h
+++ b/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);
diff --git a/routing.h b/routing.h
new file mode 100644
index 0000000..ca9c0ec
--- /dev/null
+++ b/routing.h
@@ -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)
+ {
+ }
+ };
+}
diff --git a/unittest.cpp b/unittest.cpp
new file mode 100644
index 0000000..0cea0c7
--- /dev/null
+++ b/unittest.cpp
@@ -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>)
+ {
+ }
+}
diff --git a/utility.h b/utility.h
new file mode 100644
index 0000000..c8575d3
--- /dev/null
+++ b/utility.h
@@ -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);
+ }
+ }
+}