Skip to content

Commit 2cc06ab

Browse files
committed
V1.4.4_3 | function.h -> reimplementation
1 parent 85b91fb commit 2cc06ab

12 files changed

Lines changed: 156 additions & 182 deletions

File tree

include/nodepp/any.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ namespace nodepp { class any_t {
8888
class any_impl : public any_base {
8989
public:
9090
any_impl( const T& f ) noexcept : any( type::bind(f) ) {}
91-
virtual ulong size() /*-------*/ const noexcept { return any.null() ?0 : sizeof(T) ; }
92-
virtual void ptr( void*& argc ) const noexcept { argc = &any; }
91+
virtual ulong size() /*-------*/ const noexcept override { return any.null() ?0 : sizeof(T) ; }
92+
virtual void ptr( void*& argc ) const noexcept override { argc = &any; }
9393
private:
9494
ptr_t<T> any;
9595
};

include/nodepp/date.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,37 @@ namespace nodepp { class date_t {
9696

9797
/*─······································································─*/
9898

99-
void set_stamp( const time_t& time, const bool& utc ) const noexcept {
99+
void set_stamp( const time_t& time, bool utc ) const noexcept {
100100
set_time ( time, utc );
101101
}
102102

103103
/*─······································································─*/
104104

105-
void set_date( const bool& utc ) const noexcept {
105+
void set_date( bool utc ) const noexcept {
106106
set_utc(utc); set_time( ::time(nullptr), utc );
107107
}
108108

109-
void set_date( const uint& year, const bool& utc ) const noexcept {
109+
void set_date( uint year, bool utc ) const noexcept {
110110
set_utc(utc); set_year(year);
111111
}
112112

113-
void set_date( const uint& year, const uint& month, const bool& utc ) const noexcept {
113+
void set_date( uint year, uint month, bool utc ) const noexcept {
114114
set_utc(utc); set_year(year); set_month(month);
115115
}
116116

117-
void set_date( const uint& year, const uint& month, const uint& day, const bool& utc ) const noexcept {
117+
void set_date( uint year, uint month, uint day, bool utc ) const noexcept {
118118
set_utc(utc); set_year(year); set_month(month); set_day(day);
119119
}
120120

121-
void set_date( const uint& year, const uint& month, const uint& day, const uint& hour, const bool& utc ) const noexcept {
121+
void set_date( uint year, uint month, uint day, uint hour, bool utc ) const noexcept {
122122
set_utc(utc); set_year(year); set_month(month); set_day(day); set_hour(hour);
123123
}
124124

125-
void set_date( const uint& year, const uint& month, const uint& day, const uint& hour, const uint& min, const bool& utc ) const noexcept {
125+
void set_date( uint year, uint month, uint day, uint hour, uint min, bool utc ) const noexcept {
126126
set_utc(utc); set_year(year); set_month(month); set_day(day); set_hour(hour); set_minute(min);
127127
}
128128

129-
void set_date( const uint& year, const uint& month, const uint& day, const uint& hour, const uint& min, const uint& second, const bool& utc ) const noexcept {
129+
void set_date( uint year, uint month, uint day, uint hour, uint min, uint second, bool utc ) const noexcept {
130130
set_utc(utc); set_year(year); set_month(month); set_day(day); set_hour(hour); set_minute(min); set_second(second);
131131
}
132132

@@ -215,17 +215,17 @@ namespace nodepp { namespace date {
215215

216216
inline string_t fulltime(){ return date_t().get_fulltime(); }
217217

218-
inline uint day( const bool& utc ){ return date_t(utc).get_day(); }
218+
inline uint day( bool utc ){ return date_t(utc).get_day(); }
219219

220-
inline uint year( const bool& utc ){ return date_t(utc).get_year(); }
220+
inline uint year( bool utc ){ return date_t(utc).get_year(); }
221221

222-
inline uint hour( const bool& utc ){ return date_t(utc).get_hour(); }
222+
inline uint hour( bool utc ){ return date_t(utc).get_hour(); }
223223

224-
inline uint month( const bool& utc ){ return date_t(utc).get_month(); }
224+
inline uint month( bool utc ){ return date_t(utc).get_month(); }
225225

226-
inline uint minute( const bool& utc ){ return date_t(utc).get_minute(); }
226+
inline uint minute( bool utc ){ return date_t(utc).get_minute(); }
227227

228-
inline uint second( const bool& utc ){ return date_t(utc).get_second(); }
228+
inline uint second( bool utc ){ return date_t(utc).get_second(); }
229229

230230
}}
231231

include/nodepp/encoder.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ namespace nodepp { namespace encoder { namespace hash {
8787

8888
namespace nodepp { namespace encoder { namespace XOR {
8989

90-
inline string_t atob( string_t data, const string_t& key ){
90+
inline string_t atob( const string_t& data, const string_t& key ){
9191
auto tmp= data.copy();
9292
ulong pos= 0; forEach( x, tmp ) {
9393
x = x^ key[pos]; ++pos;
9494
pos %= key.size();
9595
} return tmp;
9696
}
9797

98-
inline string_t btoa( string_t data, const string_t& key ){
98+
inline string_t btoa( const string_t& data, const string_t& key ){
9999
auto tmp= data.copy();
100100
ulong pos= 0; forEach( x, tmp ) {
101101
x = x^ key[pos]; ++pos;
@@ -161,7 +161,7 @@ namespace nodepp { namespace encoder { namespace hex {
161161
}
162162

163163
template< class T, class = typename type::enable_if<type::is_integral<T>::value,T>::type >
164-
T btoa( string_t num ){ if ( num.empty() ){ return 0; }
164+
T btoa( const string_t& num ){ if ( num.empty() ){ return 0; }
165165
T out = 0; for ( auto c: num ){ out = out<<4;
166166
if ( c >= '0' && c <= '9' ){ out |= c - '0' ; }
167167
elif ( c >= 'a' && c <= 'f' ){ out |= c - 'a' + 10; }
@@ -257,22 +257,22 @@ namespace nodepp { namespace encoder { namespace base64 {
257257
/*────────────────────────────────────────────────────────────────────────────*/
258258

259259
namespace nodepp { namespace encoder { namespace utf8 {
260-
inline ptr_t<uchar_16> to_utf16( ptr_t<uchar_8> inp ){ return utf::utf8_to_utf16( inp ); }
261-
inline ptr_t<uchar_32> to_utf32( ptr_t<uchar_8> inp ){ return utf::utf8_to_utf32( inp ); }
260+
inline ptr_t<uchar_16> to_utf16( const ptr_t<uchar_8>& inp ){ return utf::utf8_to_utf16( inp ); }
261+
inline ptr_t<uchar_32> to_utf32( const ptr_t<uchar_8>& inp ){ return utf::utf8_to_utf32( inp ); }
262262
}}}
263263

264264
/*────────────────────────────────────────────────────────────────────────────*/
265265

266266
namespace nodepp { namespace encoder { namespace utf16 {
267-
inline ptr_t<uchar_8> to_utf8 ( ptr_t<uchar_16> inp ){ return utf::utf16_to_utf8 ( inp ); }
268-
inline ptr_t<uchar_32> to_utf32( ptr_t<uchar_16> inp ){ return utf::utf16_to_utf32( inp ); }
267+
inline ptr_t<uchar_8> to_utf8 ( const ptr_t<uchar_16>& inp ){ return utf::utf16_to_utf8 ( inp ); }
268+
inline ptr_t<uchar_32> to_utf32( const ptr_t<uchar_16>& inp ){ return utf::utf16_to_utf32( inp ); }
269269
}}}
270270

271271
/*────────────────────────────────────────────────────────────────────────────*/
272272

273273
namespace nodepp { namespace encoder { namespace utf32 {
274-
inline ptr_t<uchar_8> to_utf8 ( ptr_t<uchar_32> inp ){ return utf::utf32_to_utf8 ( inp ); }
275-
inline ptr_t<uchar_16> to_utf16( ptr_t<uchar_32> inp ){ return utf::utf32_to_utf16( inp ); }
274+
inline ptr_t<uchar_8> to_utf8 ( const ptr_t<uchar_32>& inp ){ return utf::utf32_to_utf8 ( inp ); }
275+
inline ptr_t<uchar_16> to_utf16( const ptr_t<uchar_32>& inp ){ return utf::utf32_to_utf16( inp ); }
276276
}}}
277277

278278
/*────────────────────────────────────────────────────────────────────────────*/

include/nodepp/function.h

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ namespace nodepp { template< class V, class... T > class function_t {
1818
public:
1919

2020
template< class F >
21-
function_t( F f ) : func_ptr( new func_impl<F>(f) ) {}
21+
function_t( const F& f ) : func_ptr( new func_impl<F>(f) ) {}
2222

2323
function_t( null_t ) noexcept : func_ptr(nullptr) {}
2424

2525
function_t() noexcept : func_ptr(nullptr) {}
2626

2727
/*─······································································─*/
2828

29+
explicit operator bool(void) const noexcept { return func_ptr.null(); }
30+
31+
/*─······································································─*/
32+
2933
bool has_value() const noexcept { return func_ptr.has_value(); }
3034
ulong count() const noexcept { return func_ptr.count(); }
3135
bool empty() const noexcept { return func_ptr.null (); }
@@ -34,30 +38,62 @@ namespace nodepp { template< class V, class... T > class function_t {
3438
void clear() const noexcept { /*--*/ func_ptr.free (); }
3539

3640
/*─······································································─*/
37-
38-
explicit operator bool(void) const noexcept { return func_ptr.null(); }
3941

40-
V operator()( const T&... arg ) const /*----*/ { return emit( arg... ); }
42+
template< typename U = V >
43+
typename type::enable_if< type::is_same<U,void>::value, U >::type
44+
operator()( const T&... arg ) const { emit( arg... ); }
45+
46+
template< typename U = V >
47+
typename type::enable_if< type::is_same<U,void>::value, U >::type
48+
emit( const T&... arg ) const { if( has_value() ){
49+
func_ptr->invoke ( (void*) nullptr, arg... );
50+
}}
51+
52+
/*─······································································─*/
53+
54+
template< typename U = V >
55+
typename type::enable_if< !type::is_same<U,void>::value, U >::type
56+
operator()( const T&... arg ) const { return emit( arg... ); }
4157

42-
V emit( const T&... arg ) const {
43-
if( !has_value() ){ return V(); }
44-
return func_ptr->invoke( arg... );
45-
}
58+
template< typename U = V >
59+
typename type::enable_if< !type::is_same<U,void>::value, U >::type
60+
emit( const T&... arg ) const { U out; if( has_value() ){
61+
func_ptr->invoke ( &out, arg... );
62+
} return out; }
4663

4764
private:
4865

4966
class func_base { public:
50-
virtual ~func_base() { /*-----------------*/ }
51-
virtual V invoke( const T&... arg ) const = 0;
67+
virtual ~func_base () {}
68+
virtual void invoke( V*, const T&... ) const {}
5269
};
5370

5471
/*─······································································─*/
5572

5673
template< class F >
57-
class func_impl : public func_base { private: ptr_t<F> func; public:
58-
func_impl( const F& f ) : func( type::bind( f ) ) { /*-----------*/ }
59-
virtual V invoke( const T&... arg ) const { return (*func)(arg...); }
60-
};
74+
class func_impl : public func_base { private:
75+
76+
template< typename U = V >
77+
typename type::enable_if< type::is_same<U,void>::value, void >::type
78+
invoker_helper( U*, ptr_t<F> cb, const T&... args ) const noexcept {
79+
(*cb)( args... );
80+
}
81+
82+
template< typename U = V >
83+
typename type::enable_if< !type::is_same<U,void>::value, void >::type
84+
invoker_helper( U* dst, ptr_t<F> cb, const T&... args ) const noexcept {
85+
*dst = (*cb)( args... );
86+
}
87+
88+
public:
89+
90+
virtual void invoke( V* out, const T&... arg ) const override {
91+
invoker_helper ( out, func, arg... );
92+
}
93+
94+
func_impl( const F& f ) : func( type::bind(f) ) {}
95+
96+
private: ptr_t<F> func; };
6197

6298
/*─······································································─*/
6399

include/nodepp/path.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ thread_local static map_t<string_t,string_t> out ({
130130

131131
namespace nodepp { namespace path {
132132

133-
inline string_t normalize( string_t path ){
133+
inline string_t normalize( const string_t& path ){
134134
thread_local static regex_t reg0 = regex_t( NODEPP_PATH_SEL );
135135
auto sec = reg0.split( path );
136136
queue_t<string_t> nsec; ulong y=0;
@@ -209,7 +209,7 @@ namespace nodepp { namespace path {
209209

210210
/*─······································································─*/
211211

212-
inline string_t format( path_t& obj ) { string_t _path;
212+
inline string_t format( const path_t& obj ) { string_t _path;
213213

214214
if( !obj.path.empty() ){ return obj.path; }
215215

@@ -302,7 +302,7 @@ namespace nodepp { namespace path {
302302
}
303303

304304
template< class T, ulong N >
305-
string_t join( string_t (& value)[N] ){
305+
string_t join( const string_t (&value)[N] ){
306306
return array_t<string_t>( N, value ).join("/");
307307
}
308308

include/nodepp/promise.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ namespace nodepp { namespace promise {
231231
/*─······································································─*/
232232

233233
template< class V >
234-
promise_t<V,except_t> any( initializer_t<V> prom ) {
234+
promise_t<V,except_t> any( const initializer_t<V>& prom ) {
235235
return promise_t<V,except_t>([=]( res_t<V> res, rej_t<except_t>rej ){
236236

237237
if( prom.empty() ){ rej( "iterator is empty" ); return; }

0 commit comments

Comments
 (0)