Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
utility_containers_example.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
15
18
19#include <iostream>
20#include <string>
21
22using namespace kcenon::common;
23using namespace kcenon::common::utils;
24
25// A sample resource for the object pool
27{
28 int id;
29 std::string endpoint;
30
31 Connection() : id(0), endpoint("default") {}
32 Connection(int i, const std::string& ep) : id(i), endpoint(ep) {}
33};
34
35int main()
36{
37 std::cout << "=== Utility Containers Example ===\n\n";
38
39 // --- Circular Buffer ---
40 std::cout << "1. CircularBuffer (mutex-protected, capacity=5):\n";
41 {
43
44 // Push elements
45 for (int i = 1; i <= 5; ++i)
46 {
47 buffer.push(i);
48 }
49 std::cout << " Pushed 1-5. Size=" << buffer.size() << ", Full=" << buffer.full() << "\n";
50
51 // Overwrite oldest when full
52 buffer.push(99, true); // overwrite=true
53 std::cout << " Pushed 99 with overwrite. Size=" << buffer.size() << "\n";
54
55 // Pop elements
56 std::cout << " Popping: ";
57 while (auto val = buffer.pop())
58 {
59 std::cout << *val << " ";
60 }
61 std::cout << "\n Empty=" << buffer.empty() << "\n";
62 }
63
64 // --- SPSC Lock-Free Buffer ---
65 std::cout << "\n2. SPSCCircularBuffer (lock-free, capacity=4):\n";
66 {
68
69 spsc.push("alpha");
70 spsc.push("beta");
71 spsc.push("gamma");
72 std::cout << " Pushed 3 strings. Size=" << spsc.size() << "\n";
73
74 // Pop one
75 if (auto val = spsc.pop())
76 {
77 std::cout << " Popped: '" << *val << "'\n";
78 }
79 std::cout << " Remaining size=" << spsc.size() << "\n";
80 }
81
82 // --- Object Pool ---
83 std::cout << "\n3. ObjectPool (growth=4):\n";
84 {
86
87 // Pre-allocate
88 pool.reserve(8);
89 std::cout << " Reserved 8 slots. Available=" << pool.available() << "\n";
90
91 // Acquire objects
92 bool reused = false;
93 auto conn1 = pool.acquire(&reused, 1, "db-primary");
94 std::cout << " Acquired conn1: id=" << conn1->id << ", endpoint=" << conn1->endpoint
95 << ", reused=" << (reused ? "true" : "false") << "\n";
96
97 auto conn2 = pool.acquire(&reused, 2, "db-replica");
98 std::cout << " Acquired conn2: id=" << conn2->id
99 << ", available=" << pool.available() << "\n";
100
101 // Release conn1 back to pool (via unique_ptr destructor)
102 std::cout << " Releasing conn1...\n";
103 conn1.reset();
104 std::cout << " Available after release=" << pool.available() << "\n";
105
106 // Re-acquire — should reuse
107 auto conn3 = pool.acquire(&reused, 3, "db-analytics");
108 std::cout << " Acquired conn3: reused=" << (reused ? "true" : "false") << "\n";
109
110 // Clear the pool
111 conn2.reset();
112 conn3.reset();
113 pool.clear();
114 std::cout << " Pool cleared. Available=" << pool.available() << "\n";
115 }
116
117 std::cout << "\nDone.\n";
118 return 0;
119}
Thread-safe fixed-capacity circular buffer (SPSC-friendly).
Thread-safe fixed-size circular buffer.
Definition utils.cppm:100
bool push(const T &value, bool overwrite=false)
Thread-safe object pool that reuses raw storage for expensive objects.
Definition utils.cppm:218
pointer_type acquire(bool *reused, Args &&... args)
Acquire an object constructed with the provided arguments.
Definition object_pool.h:82
void reserve(std::size_t count)
Add count additional blocks to the pool.
void clear()
Destroy all cached instances and release memory.
Lock-free circular buffer for single-producer single-consumer (SPSC).
bool push(const T &value)
Push a value (producer thread only).
std::optional< T > pop()
Pop a value (consumer thread only).
Core interfaces.
Definition adapter.h:21
Thread-safe object pool reusing raw storage for expensive objects.
Connection(int i, const std::string &ep)