From 6834f0eccd749dba6a7871c9cbb7dba7d8e2324b Mon Sep 17 00:00:00 2001 From: whitglint Date: Wed, 4 Jun 2014 16:46:31 +0800 Subject: [PATCH 1/2] Add C++ wrapper. --- include/r3.hpp | 140 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 include/r3.hpp diff --git a/include/r3.hpp b/include/r3.hpp new file mode 100644 index 0000000..5c4be54 --- /dev/null +++ b/include/r3.hpp @@ -0,0 +1,140 @@ +/* + * r3.hpp + * Copyright (C) 2014 whitglint + * + * Distributed under terms of the MIT license. + */ +#ifndef R3_HPP +#define R3_HPP + +#include +#include + +namespace r3 { + template + class Base { + public: + Base(T* p) + : p_(p) { + } + + void* data() const { + return p_->data; + } + + T* get() const { + return p_; + } + + bool is_null() const { + return p_ == NULL; + } + + operator void*() const { + return p_; + } + + private: + T* p_; + }; + typedef Base Node; + typedef Base Route; + + class MatchEntry : public Base { + public: + explicit MatchEntry(const char* path) + : Base(match_entry_create(path)) { + } + + MatchEntry(const char* path, int path_len) + : Base(match_entry_createl(path, path_len)) { + } + + ~MatchEntry() { + if (get()) { + match_entry_free(get()); + } + } + + int request_method() const { + return get()->request_method; + } + + void set_request_method(int request_method) { + get()->request_method = request_method; + } + + private: + MatchEntry(const MatchEntry&); + MatchEntry& operator =(const MatchEntry&); + }; + + class Tree : public Base { + public: + explicit Tree(int cap) + : Base(r3_tree_create(cap)) { + } + + ~Tree() { + if (get()) { + r3_tree_free(get()); + } + } + + int compile(char** errstr = NULL) { + return r3_tree_compile(get(), errstr); + } + + void dump(int level) const { + r3_tree_dump(get(), level); + } + + Node insert_path(const char* path, void* data, char** errstr = NULL) { + return r3_tree_insert_pathl_ex(get(), path, std::strlen(path), NULL, + data, errstr); + } + + Node insert_pathl(const char* path, int path_len, void* data, + char** errstr = NULL) { + return r3_tree_insert_pathl_ex(get(), path, path_len, NULL, data, + errstr); + } + + Route insert_route(int method, const char* path, void* data, + char** errstr = NULL) { + return r3_tree_insert_routel_ex(get(), method, path, + std::strlen(path), data, errstr); + } + + Route insert_routel(int method, const char* path, int path_len, + void* data, char** errstr = NULL) { + return r3_tree_insert_routel_ex(get(), method, path, path_len, data, + errstr); + } + + Node match(const char* path, MatchEntry* entry = NULL) const { + return r3_tree_match(get(), path, + entry != NULL ? entry->get() : NULL); + } + + Node matchl(const char* path, int path_len, MatchEntry* entry = NULL) + const { + return r3_tree_matchl(get(), path, path_len, + entry != NULL ? entry->get() : NULL); + } + + Node match_entry(MatchEntry& entry) const { + return r3_tree_match_entry(get(), entry.get()); + } + + Route match_route(MatchEntry& entry) const { + return r3_tree_match_route(get(), entry.get()); + } + + private: + Tree(const Tree&); + Tree& operator =(const Tree&); + }; +} + +#endif // R3_HPP From a361e1fe5876da4f9b1880d579131bc7bfeab2f5 Mon Sep 17 00:00:00 2001 From: whitglint Date: Wed, 4 Jun 2014 17:24:29 +0800 Subject: [PATCH 2/2] Add r3.hpp to Makefile.am. --- Makefile.am | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile.am b/Makefile.am index 1bf3b06..5be955f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -28,6 +28,7 @@ r3_include_HEADERS = \ include/r3_gvc.h \ include/r3_json.h \ include/str_array.h \ + include/r3.hpp \ $(NULL) pkgconfigdir = $(libdir)/pkgconfig