From 360646e68070ae6b976e2943f13faab00abe546d Mon Sep 17 00:00:00 2001 From: whitglint Date: Thu, 12 Jun 2014 16:55:44 +0800 Subject: [PATCH] Add C++ example. --- configure.ac | 1 + examples/Makefile.am | 4 +- examples/simple_cpp.cpp | 97 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 examples/simple_cpp.cpp diff --git a/configure.ac b/configure.ac index 2464b96..03a2bff 100644 --- a/configure.ac +++ b/configure.ac @@ -7,6 +7,7 @@ AM_INIT_AUTOMAKE([foreign subdir-objects]) LT_INIT AC_PROG_CC AC_PROG_CC_STDC +AC_PROG_CXX AC_PROG_INSTALL # older debian diff --git a/examples/Makefile.am b/examples/Makefile.am index 4343019..906d99f 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,5 +1,7 @@ AM_CFLAGS=$(DEPS_CFLAGS) $(GVC_DEPS_CFLAGS) -I$(top_builddir)/include -Wall -std=c99 +AM_CXXFLAGS=$(DEPS_CXXFLAGS) $(GVC_DEPS_CXXFLAGS) -I$(top_builddir)/include -Wall AM_LDFLAGS=$(DEPS_LIBS) $(GVC_DEPS_LIBS) $(top_builddir)/libr3.la -noinst_PROGRAMS = simple +noinst_PROGRAMS = simple simple_cpp simple_SOURCES = simple.c +simple_cpp_SOURCES = simple_cpp.cpp diff --git a/examples/simple_cpp.cpp b/examples/simple_cpp.cpp new file mode 100644 index 0000000..ddade90 --- /dev/null +++ b/examples/simple_cpp.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + +using namespace std; + +void example_1() { + // create a router tree with 10 children capacity (this capacity can grow dynamically) + r3::Tree tree(10); + + // insert the route path into the router tree + int route_data_1 = 1; + tree.insert_path("/bar", &route_data_1); // ignore the length of path + + int route_data_2 = 2; + tree.insert_pathl("/zoo", strlen("/zoo"), &route_data_2); + int route_data_3 = 3; + tree.insert_pathl("/foo/bar", strlen("/foo/bar"), &route_data_3); + + int route_data_4 = 4; + tree.insert_pathl("/post/{id}", strlen("/post/{id}") , &route_data_4); + + int route_data_5 = 5; + tree.insert_pathl("/user/{id:\\d+}", strlen("/user/{id:\\d+}"), + &route_data_5); + + // if you want to catch error, you may call the extended path function for insertion + int data = 10; + char* errstr; + r3::Node ret = tree.insert_pathl("/foo/{name:\\d{5}", + strlen("/foo/{name:\\d{5}"), &data, &errstr); + if (ret == NULL) { + // failed insertion + cout << "error: " << errstr << endl; + free(errstr); // errstr is created from `asprintf`, so you have to free it manually. + } + + // let's compile the tree! + int err = tree.compile(&errstr); + if (err != 0) { + cout << "error: " << errstr << endl; + free(errstr); // errstr is created from `asprintf`, so you have to free it manually. + } + + // dump the compiled tree + tree.dump(0); + + // match a route + r3::Node matched_node = tree.matchl("/foo/bar", strlen("/foo/bar")); + if (matched_node) { + int ret = *static_cast(matched_node.data()); + cout << "match path ret: " << ret << endl; + } + + r3::MatchEntry entry("/foo/bar"); + matched_node = tree.match_entry(entry); + if (matched_node) { + int ret = *static_cast(matched_node.data()); + cout << "match entry ret: " << ret << endl; + } +} + +void example_2() { + // create a router tree with 10 children capacity (this capacity can grow dynamically) + r3::Tree tree(10); + + // insert the route path into the router tree + int route_data = 1; + tree.insert_routel(METHOD_GET | METHOD_POST, "/blog/post", + sizeof("/blog/post") - 1, &route_data); + + char* errstr; + int err = tree.compile(&errstr); + if (err != 0) { + cout << "errstr: " << errstr << endl; + free(errstr); // errstr is created from `asprintf`, so you have to free it manually. + } + + // in your http server handler + + // create the match entry for capturing dynamic variables. + r3::MatchEntry entry("/blog/post"); + entry.set_request_method(METHOD_GET); + + r3::Route matched_route = tree.match_route(entry); + if (matched_route) { + int ret = *static_cast(matched_route.data()); + cout << "match route ret: " << ret << endl; + } +} + +int main() { + example_1(); + example_2(); + return 0; +}