Skip to content

Commit c33241d

Browse files
committed
ps_ptr pt 27
1 parent 56184c3 commit c33241d

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/psram_unique_ptr.hpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,17 @@ class ps_ptr {
653653
T* get() const { return static_cast<T*>(mem.get()); }
654654
// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
655655
// 📌📌📌 S E T 📌📌📌
656+
657+
// ps_ptr<uint32_t> p;
658+
// uint32_t* raw = (uint32_t*)malloc(100 * sizeof(uint32_t)); // create memory manually
659+
660+
// for (int i = 0; i < 100; ++i) {// write data
661+
// raw[i] = i * i;
662+
// }
663+
// p.set(raw, 100 * sizeof(uint32_t)); // ps_ptr takes over the pointer and remembers the size
664+
// for (int i = 0; i < 5; ++i) {// access with ps_ptr
665+
// printf("%u ", p[i]);
666+
// } // later at p.reset () or automatically in the destructor `free(raw)`
656667
void set(T* ptr, std::size_t size = 0) {
657668
if (mem.get() != ptr) {
658669
mem.reset(ptr);
@@ -661,13 +672,27 @@ class ps_ptr {
661672
}
662673
// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
663674
// 📌📌📌 O P E R A T O R 📌📌📌
675+
676+
// struct MyStruct {
677+
// int x;
678+
// void hello() const {log_w("Hello, x = %i", x);}
679+
// };
680+
// ps_ptr<MyStruct> ptr;
681+
// ptr.alloc(sizeof(MyStruct));
682+
// ptr.clear(); // optional, setzt x auf 0
683+
// ptr->x = 123; // 👈 uses operator->()
684+
// ptr->hello(); // 👈 uses operator->()
685+
// (*ptr).x = 456; // 👈 uses operator*()
686+
// std::cout << (*ptr).x << "\n";
664687
T* operator->() const { return get(); }
665688
T& operator*() const { return *get(); }
666-
T& operator[](size_t index) {return get()[index];}
667-
// T& operator[](size_t index) {return static_cast<T*>(get())[index];}
668689

690+
//ps_ptr<int32_t> p;
691+
//int32_t x = p[5];
692+
T& operator[](size_t index) {return get()[index];}
669693

670-
// Zugriff auf ps_ptr<T>[], wenn T selbst ein ps_ptr<U> ist
694+
// ps_ptr<ps_ptr<int32_t>> array_of_ptrs;
695+
// array_of_ptrs[0].alloc(...);
671696
template <typename U = T>
672697
typename std::enable_if<
673698
std::is_class<U>::value &&
@@ -677,6 +702,16 @@ class ps_ptr {
677702
return get()[index];
678703
}
679704

705+
706+
// struct MyStruct {
707+
// int value;
708+
// };
709+
// ps_ptr<MyStruct> smart_ptr;
710+
//
711+
// MyStruct* raw = (MyStruct*)malloc(sizeof(MyStruct));// Manually allocated raw memory
712+
// raw->value = 123;
713+
// smart_ptr = raw; // Smart_PTR is now taking over the property
714+
// std::cout << smart_ptr->value << std::endl; // access as usual gives: 123
680715
ps_ptr<T>& operator=(T* raw_ptr) {
681716
if (mem.get() != raw_ptr) {
682717
mem.reset(raw_ptr);
@@ -695,6 +730,13 @@ class ps_ptr {
695730
// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
696731
// 📌📌📌 M O V E 📌📌📌
697732

733+
// ps_ptr<int32_t> a;
734+
// ps_ptr<int32_t> b;
735+
//
736+
// b.alloc(128);// allocate mem
737+
//
738+
// // Move semantics (no copy of the content!)
739+
// a = std::move(b); // 👉 Call your Move Assignment operator
698740
ps_ptr& operator=(ps_ptr&& other) noexcept {
699741
if (this != &other) {
700742
mem = std::move(other.mem);

0 commit comments

Comments
 (0)