Skip to content

Commit 491fde4

Browse files
committed
📌 Nodepp | Stable Release | V1.2.0 📌
1 parent dee4319 commit 491fde4

28 files changed

+798
-685
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ void onMain(){
8383
## Compatibility
8484
- 🔗: [NodePP for Window | Linux | Mac | Bsd ](https://github.com/NodeppOfficial/nodepp)
8585
- 🔗: [NodePP for Arduino](https://github.com/NodeppOfficial/nodepp-arduino)
86-
- 🔗: [Nodepp for ESP32](https://github.com/NodeppOfficial/nodepp-ESPXX)
8786
- 🔗: [Nodepp for WASM](https://github.com/NodeppOfficial/nodepp-wasm)
8887

8988
## FAQ

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = nodepp
2-
version = 0.0.33
2+
version = 1.2.0
33
author = EDBCREPO
44
maintainer = EDBCREPO <becerracenmanueld@gmail.com>
55
sentence = nodepp is a C++ library that allow building applications in C++ as if it were being written in NodeJS compatible with Arduino Uno.

src/nodepp/any.h

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,29 @@ public: any_t() noexcept {};
2626

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

29-
uint type_size() const noexcept { return any_sz.null()?0: *any_sz; }
30-
ulong count() const noexcept { return any_ptr.count(); }
31-
bool empty() const noexcept { return any_ptr.null (); }
32-
bool has_value() const noexcept { return !any_ptr.null (); }
29+
ulong type_size() const noexcept { return empty() ?0 : any_ptr->size(); }
30+
ulong count() const noexcept { return any_ptr.count(); } /*--------*/
31+
bool empty() const noexcept { return any_ptr.null (); } /*--------*/
32+
bool has_value() const noexcept { return !any_ptr.null (); } /*--------*/
33+
void free() const noexcept { /*--*/ any_ptr.free (); } /*--------*/
3334

3435
/*─······································································─*/
3536

36-
void free() const noexcept { any_ptr.free(); }
37-
38-
/*─······································································─*/
39-
40-
void operator=( const char* f ) noexcept { set( string::to_string(f) ); }
41-
42-
template< class T >
43-
void operator=( const T& f ) noexcept { set( f ); }
44-
4537
template< class T >
4638
T as() const { return get<T>(); }
4739

4840
template< class T >
49-
void set( const T& f ) noexcept {
50-
any_sz = new uint(sizeof(T));
51-
any_ptr = new any_impl<T>(f);
52-
}
41+
void set( const T& f ) noexcept { any_ptr = new any_impl<T>(f); }
5342

5443
template< class T >
55-
T get() const {
56-
char any [ sizeof(T)/sizeof(char) ]; if( !has_value() )
57-
{ ARDUINO_ERROR("any_t is null"); }
58-
if( *any_sz != sizeof(any)*sizeof(char) )
59-
{ ARDUINO_ERROR("any_t incompatible sizetype"); }
60-
any_ptr->get((void*)&any); return *(T*)(any);
44+
T get() const { const ulong size = sizeof(T) / sizeof( char );
45+
46+
if( !has_value() ) /*----*/ { ARDUINO_ERROR("any_t is null"); } /*---------*/
47+
if( type_size()!=sizeof(T) ){ ARDUINO_ERROR("any_t incompatible sizetype"); }
48+
49+
char any [ size ]; any_ptr->get((void*)&any);
50+
return *(T*)(any); /*----------------------*/
51+
6152
}
6253

6354
/*─······································································─*/
@@ -70,8 +61,9 @@ public: any_t() noexcept {};
7061
class any_base {
7162
public:
7263
virtual ~any_base() noexcept {}
73-
virtual void get( void* /*unused*/ ) const noexcept {}
74-
virtual void set( void* /*unused*/ ) noexcept {}
64+
virtual void get( void* /*unused*/ ) const noexcept {}
65+
virtual void set( void* /*unused*/ ) /*-*/ noexcept {}
66+
virtual ulong size() /*------------*/ const noexcept = 0;
7567
};
7668

7769
/*─······································································─*/
@@ -80,16 +72,16 @@ public: any_t() noexcept {};
8072
class any_impl : public any_base {
8173
public:
8274
any_impl( const T& f ) noexcept : any( f ) {}
83-
virtual void get( void* argc ) const noexcept { memcpy( argc, (void*)&any, sizeof(T) ); }
84-
virtual void set( void* argc ) noexcept { memcpy( (void*)&any, argc, sizeof(T) ); }
75+
virtual ulong size() /*------*/ const noexcept { return sizeof(T); } /*-----------------*/
76+
virtual void get( void* argc ) const noexcept { memcpy( argc, (void*)&any, sizeof(T) ); }
77+
virtual void set( void* argc ) /*-*/ noexcept { memcpy( (void*)&any, argc, sizeof(T) ); }
8578
private:
8679
T any;
8780
};
8881

8982
/*─······································································─*/
9083

9184
ptr_t<any_base> any_ptr;
92-
ptr_t<uint> any_sz;
9385

9486
};}
9587

src/nodepp/array.h

Lines changed: 78 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class array_t {
2020

2121
ptr_t<T> buffer;
2222

23-
ptr_t<ulong> get_slice_range( long x, long y ) const noexcept {
23+
type::optional<ulong[3]> get_slice_range( long x, long y ) const noexcept {
2424

2525
if( empty() || x == y ){ return nullptr; } if( y>0 ){ --y; }
2626

@@ -32,10 +32,13 @@ class array_t {
3232
ulong b = clamp( first() + x, 0UL, a );
3333
ulong c = a - b + 1;
3434

35-
return ptr_t<ulong>({ b, a, c });
35+
ulong arr[3]; /*-----------------------*/
36+
arr[0] = b; arr[1] = a; arr[2] = c;
37+
38+
return arr;
3639
}
3740

38-
ptr_t<ulong> get_splice_range( long x, ulong y ) const noexcept {
41+
type::optional<ulong[3]> get_splice_range( long x, ulong y ) const noexcept {
3942

4043
if( empty() || y == 0 ){ return nullptr; }
4144

@@ -47,7 +50,10 @@ class array_t {
4750
ulong b = clamp( first() + x, 0UL, a );
4851
ulong c = a - b + 1;
4952

50-
return ptr_t<ulong>({ b, a, c });
53+
ulong arr[3]; /*-----------------------*/
54+
arr[0] = b; arr[1] = a; arr[2] = c;
55+
56+
return arr;
5157
}
5258

5359
public:
@@ -88,7 +94,7 @@ class array_t {
8894
/*─······································································─*/
8995

9096
array_t operator+=( const array_t& oth ){
91-
if( oth.empty() ){ return *this; }
97+
if( oth.empty() ){ return *this; } /*------------------*/
9298
auto slf=copy(); buffer.resize( slf.size() +oth.size() );
9399
type::copy( oth.begin(), oth.end(), begin()+slf.size() );
94100
type::copy( slf.begin(), slf.end(), begin() ); return *this;
@@ -158,6 +164,7 @@ class array_t {
158164

159165
ptr_t<int> find( const array_t& data, ulong offset=0 ) const noexcept {
160166
if( data.empty() || empty() ){ return nullptr; } /*------*/
167+
161168
int pos = min( offset, size() ); auto addr = begin() + pos;
162169
ptr_t<int> idx ({ pos, pos }); ulong x=0;
163170

@@ -241,29 +248,26 @@ class array_t {
241248
/*─······································································─*/
242249

243250
void insert( ulong index, const T& value ) noexcept {
244-
index = clamp( index, 0UL, size() );
245-
if( empty() ){ buffer = ptr_t<T> (1); buffer[0] = value; }
251+
index = clamp( index, 0UL, size() ); if( empty() ){ buffer = ptr_t<T> (1); buffer[0] = value; }
246252
else { ulong n=size() + 1; auto n_buffer = ptr_t<T>( n );
247253
type::copy( begin()+index, end() , n_buffer.begin()+index+1 );
248254
type::copy( begin() , begin()+index, n_buffer.begin() );
249255
n_buffer[index] = value; buffer = n_buffer;
250256
}
251257
}
252258

253-
void insert( ulong index, ulong N, T* value ) noexcept {
254-
index = clamp( index, 0UL, size() );
255-
if( empty() ){ buffer = ptr_t<T> ( value, N ); }
256-
else { ulong n=size() + N; auto n_buffer = ptr_t<T>( n );
257-
type::copy( begin()+index, end() , n_buffer.begin()+index+N );
258-
type::copy( value , value+N , n_buffer.begin()+index );
259-
type::copy( begin() , begin()+index, n_buffer.begin() );
259+
void insert( ulong index, ulong N, const T& value ) noexcept {
260+
index = clamp( index, 0UL, size() ); if( empty() ){ buffer = ptr_t<T> ( N, value ); }
261+
else{ ulong n=size() + N; auto n_buffer = ptr_t<T>( n );
262+
type::copy( begin()+index , end() , n_buffer.begin()+index+N );
263+
type::fill( n_buffer.begin()+index, n_buffer.begin()+index+N, value );
264+
type::copy( begin() , begin()+index , n_buffer.begin() );
260265
buffer = n_buffer;
261266
}
262267
}
263268

264269
void insert( ulong index, const array_t& value ) noexcept {
265-
index = clamp( index, 0UL, size() );
266-
if( empty() ){ buffer = ptr_t<T> ( value.size() );
270+
index = clamp( index, 0UL, size() ); if( empty() ){ buffer = ptr_t<T> ( value.size() );
267271
type::copy( value.begin(), value.end(), buffer.begin() );
268272
} else { auto n_buffer = ptr_t<T>( size() + value.size() );
269273
ulong N=value.size();
@@ -274,10 +278,19 @@ class array_t {
274278
}
275279
}
276280

281+
void insert( ulong index, ulong N, T* value ) noexcept {
282+
index = clamp( index, 0UL, size() ); if( empty() ){ buffer = ptr_t<T> ( value, N ); }
283+
else { ulong n=size() + N; auto n_buffer = ptr_t<T>( n );
284+
type::copy( begin()+index, end() , n_buffer.begin()+index+N );
285+
type::copy( value , value+N , n_buffer.begin()+index );
286+
type::copy( begin() , begin()+index, n_buffer.begin() );
287+
buffer = n_buffer;
288+
}
289+
}
290+
277291
template< class V, ulong N >
278292
void insert( ulong index, const V (&value)[N] ) noexcept {
279-
index = clamp( index, 0UL, size() );
280-
if( empty() ){ buffer = ptr_t<T> ( N );
293+
index = clamp( index, 0UL, size() ); if( empty() ){ buffer = ptr_t<T> ( N );
281294
type::copy( value, value + N, buffer.begin() );
282295
} else { ulong n=size() + N; auto n_buffer = ptr_t<T>( n );
283296
type::copy( begin()+index, end() , n_buffer.begin()+index+N );
@@ -287,58 +300,59 @@ class array_t {
287300
}
288301
}
289302

290-
void insert( ulong index, ulong N, const T& value ) noexcept {
291-
index = clamp( index, 0UL, size() );
292-
if( empty() ){ buffer = ptr_t<T> ( N, value ); }
293-
else{ ulong n=size() + N; auto n_buffer = ptr_t<T>( n );
294-
type::copy( begin()+index , end() , n_buffer.begin()+index+N );
295-
type::fill( n_buffer.begin()+index, n_buffer.begin()+index+N, value );
296-
type::copy( begin() , begin()+index , n_buffer.begin() );
297-
buffer = n_buffer;
298-
}
299-
}
300-
301303
/*─······································································─*/
302304

303305
void erase( ulong index ) noexcept {
304-
auto r = get_slice_range( index, size() );
305-
if( r == nullptr ){ return; }
306-
else { auto n_buffer = ptr_t<T>( size() - 1 );
307-
type::copy( begin()+r[0]+1, end() , n_buffer.begin()+r[0] );
308-
type::copy( begin() , begin()+r[0], n_buffer.begin() );
306+
auto r = get_slice_range( index, size() ); if( !r.has_value() ){ return; }
307+
else { auto z = *r.get(); auto n_buffer = ptr_t<T>( size() - 1 );
308+
type::copy( begin()+z[0]+1, end() , n_buffer.begin()+z[0] );
309+
type::copy( begin() , begin()+z[0], n_buffer.begin() );
309310
buffer = n_buffer;
310311
}
311312
}
312313

313314
void erase( ulong start, ulong stop ) noexcept {
314-
auto r = get_slice_range( start, stop );
315-
if( r == nullptr ){ return; }
316-
else { auto n_buffer = ptr_t<T>( size() - r[2] );
317-
type::copy( begin()+r[1]+1, end() , n_buffer.begin()+r[0] );
318-
type::copy( begin() , begin()+r[0], n_buffer.begin() );
315+
auto r = get_slice_range( start, stop ); if( !r.has_value() ){ return; }
316+
else { auto z = *r.get(); auto n_buffer = ptr_t<T>( size() - z[2] );
317+
type::copy( begin()+z[1]+1, end() , n_buffer.begin()+z[0] );
318+
type::copy( begin() , begin()+z[0], n_buffer.begin() );
319319
buffer = n_buffer;
320320
}
321321
}
322322

323323
/*─······································································─*/
324324

325325
string_t join( string_t c=", " ) const noexcept {
326-
if( empty() ){ return nullptr; } string_t out;
326+
if( empty() ){ return nullptr; } queue_t<string_t> borrow;
327+
/*---------------------------------*/ ulong size=0, off=0;
328+
327329
auto addr = begin(); while( addr != end() ){
328-
out += string::to_string( *addr );
329-
out += addr+1 == end() ? nullptr : c;
330-
++addr; } return out;
330+
borrow.push( string::to_string( *addr ) );
331+
size += borrow.last()->data.size();
332+
if( addr+1 != end() ){ size += c.size(); }
333+
++addr; }
334+
335+
string_t out( size, 0x00 ); auto n=borrow.first();
336+
337+
while( n!=nullptr ){
338+
type::copy( n->data.begin(), n->data.end(), out.begin()+off ); off+= n->data.size();
339+
if( off < out.size() )
340+
{ type::copy( c.begin(), c.end(), out.begin()+off ); } /*-----*/ off+= c.size();
341+
n=n->next; }
342+
343+
return out;
331344
}
332345

333346
/*─······································································─*/
334347

335348
array_t slice( long start ) const noexcept {
336349

337350
auto r = get_slice_range( start, size() );
338-
if( r == nullptr ){ return nullptr; }
351+
if( !r.has_value() ){ return nullptr; }
339352

340-
auto n_buffer = ptr_t<T>( r[2] );
341-
type::copy( begin()+r[0], begin()+r[0]+r[2], n_buffer.begin() );
353+
auto z = *r.get();
354+
auto n_buffer = ptr_t<T>( z[2] );
355+
type::copy( begin()+z[0], begin()+z[0]+z[2], n_buffer.begin() );
342356

343357
return n_buffer;
344358
}
@@ -348,10 +362,11 @@ class array_t {
348362
array_t slice( long start, long stop ) const noexcept {
349363

350364
auto r = get_slice_range( start, stop );
351-
if( r == nullptr ){ return nullptr; }
365+
if( !r.has_value() ){ return nullptr; }
352366

353-
auto n_buffer = ptr_t<T>( r[2] );
354-
type::copy( begin()+r[0], begin()+r[0]+r[2], n_buffer.begin() );
367+
auto z = *r.get();
368+
auto n_buffer = ptr_t<T>( z[2] );
369+
type::copy( begin()+z[0], begin()+z[0]+z[2], n_buffer.begin() );
355370

356371
return n_buffer;
357372
}
@@ -361,34 +376,38 @@ class array_t {
361376
array_t splice( long start, ulong stop ) noexcept {
362377

363378
auto r = get_splice_range( start, stop );
364-
if( r == nullptr ){ return nullptr; }
379+
if( !r.has_value() ){ return nullptr; }
365380

366-
auto n_buffer = ptr_t<T>( r[2] );
367-
type::copy( begin()+r[0], begin()+r[0]+r[2], n_buffer.begin() );
381+
auto z = *r.get();
382+
auto n_buffer = ptr_t<T>( z[2] );
383+
type::copy( begin()+z[0], begin()+z[0]+z[2], n_buffer.begin() );
368384

369-
erase( r[0], r[0]+r[2] ); return n_buffer;
385+
erase( z[0], z[0] + z[2] ); return n_buffer;
370386
}
371387

372388
template< class V, ulong N >
373389
array_t splice( long start, ulong stop, const V (&value)[N] ) noexcept {
374390

375391
auto r = get_splice_range( start, stop );
376-
if( r == nullptr ){ return nullptr; }
392+
if( !r.has_value() ){ return nullptr; }
377393

378-
auto n_buffer = ptr_t<T>( r[2] );
379-
type::copy( begin()+r[0], begin()+r[0]+r[2], n_buffer.begin() );
394+
auto z = *r.get();
395+
auto n_buffer = ptr_t<T>( z[2] );
396+
type::copy( begin()+z[0], begin()+z[0]+z[2], n_buffer.begin() );
380397

381-
erase( r[0], r[0]+r[2] ); insert( r[0], value ); return n_buffer;
398+
erase( z[0], z[0]+z[2] ); insert( z[0], value ); return n_buffer;
382399

383400
}
384401

385402
/*─······································································─*/
386403

387404
explicit operator bool(void) const noexcept { return empty(); }
388405
explicit operator T*(void) const noexcept { return &buffer; }
406+
389407
const T* c_arr() const noexcept { return &buffer; }
390408
T* data() const noexcept { return &buffer; }
391409
T* get() const noexcept { return &buffer; }
410+
392411
ptr_t<T>& ptr() noexcept { return buffer; }
393412

394413
};}
@@ -399,7 +418,8 @@ namespace nodepp { namespace string {
399418

400419
template< class T >
401420
array_t<T> operator+( const array_t<T>& A, const array_t<T>& B ){
402-
ptr_t<T> C( A.size() + B.size() );
421+
if( A.empty() ){ return B; } if( B.empty() ){ return A; }
422+
ptr_t<T> C( A.size() + B.size() ); /*--------------*/
403423
type::copy( B.begin(), B.end(), C.begin()+A.size() );
404424
type::copy( A.begin(), A.end(), C.begin() ); return C;
405425
}

0 commit comments

Comments
 (0)