Skip to content

Commit e18372f

Browse files
committed
nodepp V1.4.1_0
1 parent e562d71 commit e18372f

File tree

6 files changed

+96
-110
lines changed

6 files changed

+96
-110
lines changed

examples/0-stl_interop.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,45 @@ void onMain(){
1313
ptr_t<int> ptr ( 0UL, 10 ); // as value
1414
// ptr_t<int> ptr ( 1UL, 10 ); // as buffer
1515

16+
/*-----*/
17+
1618
std::function<void(int)> clb ([=]( int value ){
1719
console::log( value, *ptr );
1820
});
1921

22+
/*-----*/
23+
2024
event_t<int> ev; ev.once( clb );
2125
ev.emit( 100 );
2226

27+
/*-----*/
28+
2329
std::string str = "hello world";
2430
console::log( ">>", str );
2531

32+
/*-----*/
33+
2634
std::vector<int> arr ({ 10, 20, 30, 40, 50 });
2735
console::log( ">>", array_t<int>( arr ).join() );
2836

37+
/*-----*/
38+
2939
std::queue<int> st ({ 10, 20, 30, 40 });
3040
queue_t<int> que ( st );
3141
que.map([=]( int value ){ console::log( "<>", value ); });
3242

43+
/*-----*/
44+
45+
std::string str = "hello world";
46+
ptr_t<std::string> ptr1 ( 0UL );
47+
*ptr1 = type::move( str );
48+
console::log( *ptr1 );
49+
50+
/*----*/
51+
52+
std::vector<int> vec ({ 10, 20, 30 });
53+
ptr_t<int> ptr2 ( 3UL );
54+
type::move( vec.begin(), vec.end(), ptr2.begin() );
55+
console::log( array_t<int>( ptr2 ).join() ); // <- cheap for the CPU
56+
3357
}

examples/2-pointer2.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,13 @@ void onMain(){
2121
console::log( "count->", GC.count() );
2222
return -1; });
2323

24-
process::await([&](){
24+
process::await([&](){ // <- here & is safe because await block the main thread but not the EvLoop.
2525
console::log("-- 2 --");
2626
console::log( "addr ->", GC );
2727
console::log( "value->", *GC );
2828
console::log( "count->", GC.count() );
2929
return -1; });
3030

31-
process::onSIGCLOSE.once([=](){
32-
console::log("-- 3 --");
33-
console::log( "addr ->", GC );
34-
console::log( "value->", *GC );
35-
console::log( "count->", GC.count() );
36-
});
37-
3831
console::log("-- 4 --");
3932
console::log( "addr ->", GC );
4033
console::log( "value->", *GC );

examples/2-pointer3.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <nodepp/nodepp.h>
2+
3+
using namespace nodepp;
4+
5+
class my_class { public:
6+
7+
int variable = 10;
8+
9+
my_class() {
10+
auto self = type::bind( this ); // ptr_t<my_class> ( 0UL, *this );
11+
process::add( fn() -> int {
12+
console::log( self->variable ); // 10
13+
self->variable = 20;
14+
console::log( self->variable ); // 20
15+
return -1; }); }
16+
17+
};
18+
19+
void onMain() {
20+
21+
my_class obj;
22+
console::log( obj.variable );
23+
24+
}

examples/3-Regex-2.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ void onMain(){
1212
![Imagen4](URL5)
1313
)";
1414

15+
// nodepp compile regex but if you need to be faster use static to pre-compile regex and keep
16+
// as long as the function lives, but take care with () memory copy, because can lead
17+
// to race conditions if shared with multiple threads
18+
// thread_local static regex_t( "(pattern)" ); <- multithread safe
19+
// /*--------*/ static regex_t( "(pattern)" ); <- multithread unsafe
20+
1521
regex_t reg ("!\\[([^\\]]+)\\]\\(([^\\)]+)\\)");
1622

1723
console::log( "-- --" );

include/nodepp/macros.h

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#if defined(_POSIX_THREADS) && (_POSIX_THREADS > 0)
1818
#define NODEPP_THREAD_SUPPORTED
1919
#else
20-
#define thread_local
20+
#define thread_local /*unused*/
2121
#endif
2222

2323
/*────────────────────────────────────────────────────────────────────────────*/
@@ -28,24 +28,24 @@ template< class T > T clamp( const T& val, const T& _min, const T& _max ){ retur
2828

2929
/*────────────────────────────────────────────────────────────────────────────*/
3030

31-
#define coDelay(VALUE) do { _time_=process::millis()+VALUE; while( process::millis()<_time_ ){ coErrno(VALUE,_LINE_,1); }} while(0);
32-
#define coUDelay(VALUE) do { _time_=process::micros()+VALUE; while( process::micros()<_time_ ){ coNext; }} while(0);
33-
#define coErrno(DELAY,STATE,OUT) do { coSet(STATE); coroutine::getno( OUT,coGet,DELAY ); return OUT; case STATE:; } while(0);
31+
#define coDelay(VALUE) do { _time_=nodepp::process::millis()+VALUE; while( nodepp::process::millis()<_time_ ){ coErrno(VALUE,_LINE_,1); }} while(0);
32+
#define coUDelay(VALUE) do { _time_=nodepp::process::micros()+VALUE; while( nodepp::process::micros()<_time_ ){ /*------------*/ coNext; }} while(0);
33+
#define coErrno(DELAY,STATE,OUT) do { coSet(STATE); nodepp ::coroutine::getno( OUT,coGet,DELAY ); return OUT; case STATE:; } while(0);
3434

3535
/*────────────────────────────────────────────────────────────────────────────*/
3636

37-
#define coGoto(VALUE) do { coSet( VALUE ); coroutine::getno(1,coGet); return 1; } while(0);
38-
#define coStay(VALUE) do { coSet( VALUE ); coroutine::getno(0,coGet); return 0; } while(0);
39-
#define coNext do { coErrno(0UL,_LINE_,1); /*-------------------------*/ } while(0);
40-
#define coYield(VALUE) do { coErrno(0UL, VALUE,1); /*-------------------------*/ } while(0);
41-
#define coWait(VALUE) do { while( VALUE ){ /*------------------------*/ coNext;}} while(0);
42-
#define coEnd do { _time_=0; _state_=_time_; /**/ coroutine::getno(-1); } while(0); return -1;
43-
#define coStop } _time_=0; _state_=_time_; /**/ coroutine::getno(-1); } while(0); return -1;
37+
#define coGoto(VALUE) do { coSet( VALUE ); nodepp::coroutine::getno(1,coGet); return 1; } while(0);
38+
#define coStay(VALUE) do { coSet( VALUE ); nodepp::coroutine::getno(0,coGet); return 0; } while(0);
39+
#define coNext do { coErrno(0UL,_LINE_,1); /*---------------------------------*/ } while(0);
40+
#define coYield(VALUE) do { coErrno(0UL, VALUE,1); /*---------------------------------*/ } while(0);
41+
#define coWait(VALUE) do { while( VALUE ){ /*-------------------------------*/ coNext; }} while(0);
42+
#define coEnd do { _time_=0; _state_=_time_; /**/ nodepp::coroutine::getno(-1); } while(0); return -1;
43+
#define coStop } _time_=0; _state_=_time_; /**/ nodepp::coroutine::getno(-1); } while(0); return -1;
4444

4545
/*────────────────────────────────────────────────────────────────────────────*/
4646

4747
#define coStart thread_local static int _state_=0; thread_local static ulong _time_=0; coBegin
48-
#define coBegin do { switch(_state_) { case 0:; coroutine::getno(-2);
48+
#define coBegin do { switch(_state_) { case 0:; nodepp::coroutine::getno(-2);
4949
#define coEmit int operator()
5050

5151
#define coSet(VALUE) _state_ = VALUE
@@ -54,15 +54,16 @@ template< class T > T clamp( const T& val, const T& _min, const T& _max ){ retur
5454

5555
/*────────────────────────────────────────────────────────────────────────────*/
5656

57-
#define onMain INIT(); int main( int argc, char** args ) { \
58-
process::start( argc,args ); INIT(); \
59-
process::wait (); return 0; \
60-
} void INIT
57+
#define onMain NODEPP_BEGIN(); int main( int argc, char** args ) { \
58+
nodepp::process::start( argc,args ); NODEPP_BEGIN(); \
59+
nodepp::process::wait (); return 0 ; \
60+
} void NODEPP_BEGIN
6161

6262
/*────────────────────────────────────────────────────────────────────────────*/
6363

64+
#define GENERATOR(NAME) struct NAME : public nodepp::generator_t
6465
#define COROUTINE() [=]( int& _state_, ulong& _time_ )
65-
#define GENERATOR(NAME) struct NAME : public generator_t
66+
#define fn(...) [=]( __VA_ARGS__ )
6667

6768
/*────────────────────────────────────────────────────────────────────────────*/
6869

@@ -81,7 +82,6 @@ template< class T > T clamp( const T& val, const T& _min, const T& _max ){ retur
8182

8283
/*────────────────────────────────────────────────────────────────────────────*/
8384

84-
#define _JSON_(...) json::parse(_STRING_(__VA_ARGS__))
8585
#define _FUNC_ __PRETTY_FUNCTION__
8686
#define _STRING_(...) #__VA_ARGS__
8787
#define _NAME_ __FUNCTION__
@@ -98,12 +98,7 @@ template< class T > T clamp( const T& val, const T& _min, const T& _max ){ retur
9898

9999
/*────────────────────────────────────────────────────────────────────────────*/
100100

101-
static bool& NODEPP_SHTDWN(){ static bool out=false; return out; }
102-
103-
/*────────────────────────────────────────────────────────────────────────────*/
104-
105-
#define MAX_SOCKET limit::get_soft_fileno()
106-
101+
#define MAX_SOCKET nodepp::limit::get_soft_fileno()
107102
#define HASH_TABLE_SIZE 16
108103
#define MAX_BATCH 16
109104
#define MAX_SSO 16
@@ -247,7 +242,7 @@ static bool& NODEPP_SHTDWN(){ static bool out=false; return out; }
247242

248243
/*────────────────────────────────────────────────────────────────────────────*/
249244

250-
#define typeof(DATA) string_t( typeid(DATA).name() )
245+
#define typeof(DATA) nodepp::string_t( typeid(DATA).name() )
251246

252247
#define ullong unsigned long long int
253248
#define ulong unsigned long int
@@ -281,7 +276,10 @@ static bool& NODEPP_SHTDWN(){ static bool out=false; return out; }
281276

282277
/*────────────────────────────────────────────────────────────────────────────*/
283278

284-
using null_t = decltype( nullptr );
279+
namespace nodepp {
280+
static bool& NODEPP_SHTDWN(){ static bool out=false; return out; }
281+
/*--*/ using null_t = decltype( nullptr );
282+
}
285283

286284
/*────────────────────────────────────────────────────────────────────────────*/
287285

main.cpp

Lines changed: 17 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,26 @@
11
#include <nodepp/nodepp.h>
2-
#include <nodepp/worker.h>
3-
#include <nodepp/timer.h>
4-
#include <nodepp/http.h>
5-
#include <nodepp/path.h>
6-
#include <nodepp/ws.h>
7-
#include <nodepp/fs.h>
2+
#include <nodepp/event.h>
83

9-
using namespace nodepp;
10-
11-
void server(){
12-
13-
auto server = http::server([=]( http_t cli ){
14-
15-
cli.write_header( 200, header_t({
16-
{ "Content-Security-Policy", "*" }
17-
}) );
18-
19-
cli.write("Hello World!");
20-
21-
}); ws::server( server );
22-
23-
server.onConnect([=]( ws_t cli ){
24-
25-
console::log("connected");
26-
27-
cli.onData([=]( string_t data ){
28-
cli.write( "<: received" );
29-
console::log( data );
30-
});
31-
32-
cli.onClose([=](){
33-
console::log("closed");
34-
});
35-
36-
});
4+
#include <string>
5+
#include <queue>
6+
#include <vector>
7+
#include <functional>
378

38-
server.onError([=]( except_t err ){
39-
console::log( ">>", err.what() );
40-
});
41-
42-
server.listen( "localhost", 8000, [=]( socket_t server ){
43-
console::log("server started at http://localhost:8000");
44-
});
45-
46-
}
47-
48-
void client() {
49-
50-
auto client = ws::client( "ws://localhost:8000/" );
51-
52-
client.onConnect([=]( ws_t cli ){
53-
54-
auto cin = fs::std_input();
55-
console::log("connected");
56-
57-
cli.onData([]( string_t data ){
58-
console::log( data );
59-
});
60-
61-
cin.onData([=]( string_t data ){
62-
cli.write( data );
63-
});
64-
65-
cli.onClose([=](){
66-
console::log("closed");
67-
cin.close();
68-
});
69-
70-
stream::pipe( cin );
71-
72-
});
9+
using namespace nodepp;
7310

74-
client.onError([=]( except_t err ){
75-
console::log( "<>", err.data() );
76-
});
11+
void onMain(){
7712

78-
}
13+
std::string str = "hello world";
14+
ptr_t<std::string> ptr1 ( 0UL );
15+
*ptr1 = type::move( str );
16+
console::log( *ptr1 );
17+
console::log( str.length() ); // <- str is empty
7918

80-
void onMain() {
19+
/*----*/
8120

82-
if( process::env::get( "MODE" ) == "client" )
83-
{ client(); } else { server(); }
21+
std::vector<int> vec ({ 10, 20, 30 });
22+
ptr_t<int> ptr2 ( 3UL );
23+
type::move( vec.begin(), vec.end(), ptr2.begin() );
24+
console::log( array_t<int>( ptr2 ).join() ); // <- cheap for the CPU
8425

8526
}

0 commit comments

Comments
 (0)