Skip to content

Commit 37b0681

Browse files
author
Chrono Law
committed
ngx_cpp_module
1 parent c9302a2 commit 37b0681

28 files changed

+3188
-0
lines changed

ngxpp/Nginx.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NGX_COMMON_HEADERS_HPP
4+
#define _NGX_COMMON_HEADERS_HPP
5+
6+
extern "C" {
7+
8+
#include <ngx_http.h>
9+
10+
}
11+
12+
#include <cassert>
13+
#include <boost/core/ignore_unused.hpp>
14+
15+
#define ngx_cpp_version 1000000
16+
#define NGX_CPP_VERSION "1.0.0"
17+
18+
#endif //_NGX_COMMON_HEADERS_HPP

ngxpp/NgxAll.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NGX_ALL_HPP
4+
#define _NGX_ALL_HPP
5+
6+
#include "Nginx.hpp"
7+
8+
#include "NgxWrapper.hpp"
9+
10+
#include "NgxValue.hpp"
11+
#include "NgxException.hpp"
12+
#include "NgxPool.hpp"
13+
#include "NgxAllocator.hpp"
14+
#include "NgxString.hpp"
15+
#include "NgxLog.hpp"
16+
#include "NgxClock.hpp"
17+
#include "NgxDatetime.hpp"
18+
19+
#include "NgxArray.hpp"
20+
#include "NgxList.hpp"
21+
#include "NgxQueue.hpp"
22+
#include "NgxBuf.hpp"
23+
//#include "NgxFile.hpp"
24+
25+
#include "NgxConfig.hpp"
26+
27+
#include "NgxModule.hpp"
28+
#include "NgxHttpModule.hpp"
29+
30+
#include "NgxRequest.hpp"
31+
#include "NgxFilter.hpp"
32+
#include "NgxUpstream.hpp"
33+
#include "NgxLoadBalance.hpp"
34+
#include "NgxSubRequest.hpp"
35+
36+
#include "NgxDigest.hpp"
37+
#include "NgxTimer.hpp"
38+
#include "NgxVariable.hpp"
39+
40+
#endif //_NGX_ALL_HPP

ngxpp/NgxAllocator.hpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NGX_ALLOCATOR_HPP
4+
#define _NGX_ALLOCATOR_HPP
5+
6+
#include "NgxWrapper.hpp"
7+
8+
template<typename T>
9+
class NgxAlloctor : public NgxWrapper<ngx_pool_t>
10+
{
11+
public:
12+
typedef NgxWrapper<ngx_pool_t> super_type;
13+
typedef NgxAlloctor this_type;
14+
public:
15+
typedef std::size_t size_type;
16+
typedef std::ptrdiff_t difference_type;
17+
typedef T* pointer;
18+
typedef const T* const_pointer;
19+
typedef T& reference;
20+
typedef const T& const_reference;
21+
typedef T value_type;
22+
23+
template<typename U>
24+
struct rebind
25+
{
26+
typedef NgxAlloctor<U> other;
27+
};
28+
public:
29+
NgxAlloctor(ngx_pool_t* p) : super_type(p)
30+
{}
31+
32+
~NgxAlloctor() = default;
33+
/*
34+
public:
35+
pointer address(reference r) const
36+
{
37+
return &r;
38+
}
39+
40+
const_pointer address(const_reference r) const
41+
{
42+
return &r;
43+
}
44+
45+
size_type max_size() const
46+
{
47+
return std::numeric_limits<size_type>::max();
48+
}
49+
*/
50+
public:
51+
pointer allocate(size_type n)
52+
{
53+
return reinterpret_cast<pointer>(
54+
ngx_pnalloc(get(), n * sizeof(T)));
55+
}
56+
57+
void deallocate(pointer ptr, size_type n)
58+
{
59+
boost::ignore_unused(n);
60+
ngx_pfree(get(), ptr);
61+
}
62+
/*
63+
public:
64+
bool operator==(const NgxAlloctor& o) const
65+
{
66+
return get() == o.get();
67+
}
68+
69+
bool operator!=(const NgxAlloctor& o) const
70+
{
71+
return get() != o.get();
72+
}
73+
*/
74+
};
75+
76+
#ifdef NGX_STD_CONTAINER
77+
#include <vector>
78+
79+
template<typename T>
80+
using NgxStdVector = std::vector<T, NgxAlloctor<T> >;
81+
82+
#endif
83+
84+
#endif //_NGX_ALLOCATOR_HPP

ngxpp/NgxArray.hpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NGX_ARRAY_HPP
4+
#define _NGX_ARRAY_HPP
5+
6+
#include "NgxPool.hpp"
7+
8+
template<typename T>
9+
class NgxArray final : public NgxWrapper<ngx_array_t>
10+
{
11+
public:
12+
typedef NgxWrapper<ngx_array_t> super_type;
13+
typedef NgxArray this_type;
14+
15+
typedef T value_type;
16+
public:
17+
NgxArray(const NgxPool& p, ngx_uint_t n = 10):
18+
super_type(p.array<T>(n))
19+
{}
20+
21+
NgxArray(ngx_array_t* arr):super_type(arr)
22+
{}
23+
24+
NgxArray(ngx_array_t& arr):super_type(arr)
25+
{}
26+
27+
~NgxArray() = default;
28+
public:
29+
ngx_uint_t size() const
30+
{
31+
return get()?get()->nelts:0;
32+
}
33+
34+
T& operator[](ngx_uint_t i) const
35+
{
36+
NgxException::require(i < size() && get());
37+
38+
return elts()[i];
39+
}
40+
41+
public:
42+
template<typename V>
43+
void visit(V v) const
44+
{
45+
auto p = elts();
46+
47+
for(ngx_uint_t i = 0;i < size(); ++i)
48+
{
49+
v(p[i]);
50+
}
51+
}
52+
public:
53+
T& prepare() const
54+
{
55+
auto tmp = ngx_array_push(get());
56+
57+
NgxException::require(tmp);
58+
59+
assert(tmp);
60+
return *reinterpret_cast<T*>(tmp);
61+
}
62+
63+
void push(const T& x) const
64+
{
65+
prepare() = x;
66+
}
67+
public:
68+
void merge(const this_type& a) const
69+
{
70+
auto f = [this](const value_type& v)
71+
{
72+
prepare() = v;
73+
};
74+
75+
a.visit(f);
76+
}
77+
private:
78+
T* elts() const
79+
{
80+
return reinterpret_cast<T*>(get()->elts);
81+
}
82+
};
83+
84+
typedef NgxArray<ngx_str_t> NgxStrArray;
85+
typedef NgxArray<ngx_keyval_t> NgxKvArray;
86+
87+
#endif //_NGX_ARRAY_HPP

0 commit comments

Comments
 (0)