Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
concepts_example.cpp File Reference

Demonstrates usage of C++20 concepts in network_system. More...

#include <kcenon/network/concepts/concepts.h>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <system_error>
#include <vector>
Include dependency graph for concepts_example.cpp:

Go to the source code of this file.

Classes

class  ConceptClient
 A client class demonstrating concept-constrained callbacks. More...
 
class  MockNetworkClient
 A mock client satisfying NetworkClient concept. More...
 
class  XorTransformer
 A simple XOR "encryption" transformer. More...
 

Functions

template<ByteBuffer Buffer>
void send_buffer (const Buffer &buffer)
 Send data from any buffer type satisfying ByteBuffer concept.
 
void demonstrate_byte_buffer ()
 
template<MutableByteBuffer Buffer>
void receive_into (Buffer &buffer, std::size_t expected_size)
 Receive data into a resizable buffer.
 
void demonstrate_mutable_buffer ()
 
void demonstrate_callbacks ()
 
template<NetworkClient Client>
void use_client (Client &client, const std::string &message)
 Generic function that works with any NetworkClient.
 
void demonstrate_network_client ()
 
template<ReversibleDataTransformer T>
void process_bidirectional (T &transformer, std::vector< uint8_t > &data)
 Apply a reversible transformer to data.
 
void demonstrate_transformer ()
 
template<Duration D>
void set_timeout (D duration)
 Set a timeout using any duration type.
 
void demonstrate_duration ()
 
int main ()
 

Detailed Description

Demonstrates usage of C++20 concepts in network_system.

BSD 3-Clause License Copyright (c) 2024, Network System Project

This example shows how to use network_system concepts for:

  • Compile-time type validation
  • Better error messages
  • Self-documenting template interfaces

Requirements:

  • C++20 compiler (GCC 10+, Clang 10+, MSVC 2022+)

Definition in file concepts_example.cpp.

Function Documentation

◆ demonstrate_byte_buffer()

void demonstrate_byte_buffer ( )

Definition at line 44 of file concepts_example.cpp.

44 {
45 std::cout << "=== ByteBuffer Concept ===\n";
46
47 // Works with std::vector<uint8_t>
48 std::vector<uint8_t> vec_buffer = {1, 2, 3, 4, 5};
49 send_buffer(vec_buffer);
50
51 // Works with std::string
52 std::string str_buffer = "Hello, Network!";
53 send_buffer(str_buffer);
54
55 // Works with std::array
56 std::array<uint8_t, 4> arr_buffer = {0xDE, 0xAD, 0xBE, 0xEF};
57 send_buffer(arr_buffer);
58
59 // The following would NOT compile (good!):
60 // int x = 42;
61 // send_buffer(x); // Error: 'int' does not satisfy 'ByteBuffer'
62
63 std::cout << "\n";
64}
void send_buffer(const Buffer &buffer)
Send data from any buffer type satisfying ByteBuffer concept.

References send_buffer().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demonstrate_callbacks()

void demonstrate_callbacks ( )

Definition at line 180 of file concepts_example.cpp.

180 {
181 std::cout << "=== Callback Concepts ===\n";
182
184
185 // Lambda callbacks - all types are validated at compile time
186 client.on_data([](const std::vector<uint8_t>& data) {
187 std::cout << "Received " << data.size() << " bytes\n";
188 });
189
190 client.on_error([](std::error_code ec) {
191 std::cout << "Error: " << ec.message() << "\n";
192 });
193
194 client.on_connect([]() {
195 std::cout << "Connected!\n";
196 });
197
198 client.on_retry([](std::size_t attempt) {
199 std::cout << "Retry attempt #" << attempt << "\n";
200 });
201
202 client.simulate_events();
203
204 // Invalid callbacks would fail at compile time:
205 // client.on_data([](int x) {}); // Wrong parameter type
206 // client.on_error([](std::string s) {}); // Wrong parameter type
207 // client.on_connect([](int x) {}); // Handler should take no args
208
209 std::cout << "\n";
210}
A client class demonstrating concept-constrained callbacks.

Referenced by main().

Here is the caller graph for this function:

◆ demonstrate_duration()

void demonstrate_duration ( )

Definition at line 347 of file concepts_example.cpp.

347 {
348 std::cout << "=== Duration Concept ===\n";
349
350 set_timeout(std::chrono::seconds(30));
351 set_timeout(std::chrono::milliseconds(500));
352 set_timeout(std::chrono::microseconds(100000));
353
354 // Invalid: raw integers don't satisfy Duration
355 // set_timeout(5); // Error: 'int' does not satisfy 'Duration'
356
357 std::cout << "\n";
358}
void set_timeout(D duration)
Set a timeout using any duration type.

References set_timeout().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demonstrate_mutable_buffer()

void demonstrate_mutable_buffer ( )

Definition at line 84 of file concepts_example.cpp.

84 {
85 std::cout << "=== MutableByteBuffer Concept ===\n";
86
87 std::vector<uint8_t> buffer;
88 receive_into(buffer, 100);
89 std::cout << "Received data, first byte: " << static_cast<int>(buffer[0]) << "\n";
90
91 // std::array does NOT satisfy MutableByteBuffer (no resize)
92 // std::array<uint8_t, 10> arr;
93 // receive_into(arr, 5); // Error: no resize() method
94
95 std::cout << "\n";
96}
void receive_into(Buffer &buffer, std::size_t expected_size)
Receive data into a resizable buffer.

References receive_into().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demonstrate_network_client()

void demonstrate_network_client ( )

Definition at line 256 of file concepts_example.cpp.

256 {
257 std::cout << "=== NetworkClient Concept ===\n";
258
260
261 // Try to send before connecting
262 use_client(client, "Hello");
263
264 // Connect and send
265 client.connect();
266 use_client(client, "Hello, World!");
267
268 // Stop client
269 client.stop_client();
270
271 std::cout << "\n";
272}
A mock client satisfying NetworkClient concept.
void use_client(Client &client, const std::string &message)
Generic function that works with any NetworkClient.

References use_client().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ demonstrate_transformer()

void demonstrate_transformer ( )

Definition at line 322 of file concepts_example.cpp.

322 {
323 std::cout << "=== DataTransformer Concept ===\n";
324
325 XorTransformer xor_transform(0x42);
326 std::vector<uint8_t> data = {'H', 'e', 'l', 'l', 'o'};
327
328 process_bidirectional(xor_transform, data);
329
330 std::cout << "\n";
331}
A simple XOR "encryption" transformer.
void process_bidirectional(T &transformer, std::vector< uint8_t > &data)
Apply a reversible transformer to data.

References process_bidirectional().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 392 of file concepts_example.cpp.

392 {
393 std::cout << "==============================================\n";
394 std::cout << "Network System - C++20 Concepts Example\n";
395 std::cout << "==============================================\n\n";
396
403
404 std::cout << "==============================================\n";
405 std::cout << "All examples completed successfully!\n";
406 std::cout << "==============================================\n";
407
408 return 0;
409}
void demonstrate_network_client()
void demonstrate_mutable_buffer()
void demonstrate_transformer()
void demonstrate_callbacks()
void demonstrate_duration()
void demonstrate_byte_buffer()

References demonstrate_byte_buffer(), demonstrate_callbacks(), demonstrate_duration(), demonstrate_mutable_buffer(), demonstrate_network_client(), and demonstrate_transformer().

Here is the call graph for this function:

◆ process_bidirectional()

template<ReversibleDataTransformer T>
void process_bidirectional ( T & transformer,
std::vector< uint8_t > & data )

Apply a reversible transformer to data.

Template Parameters
TMust satisfy ReversibleDataTransformer

Definition at line 306 of file concepts_example.cpp.

306 {
307 std::cout << "Original: ";
308 for (auto b : data) std::cout << static_cast<char>(b);
309 std::cout << "\n";
310
311 transformer.transform(data);
312 std::cout << "Transformed: ";
313 for (auto b : data) std::cout << std::hex << static_cast<int>(b) << " ";
314 std::cout << std::dec << "\n";
315
316 transformer.reverse_transform(data);
317 std::cout << "Restored: ";
318 for (auto b : data) std::cout << static_cast<char>(b);
319 std::cout << "\n";
320}

Referenced by demonstrate_transformer().

Here is the caller graph for this function:

◆ receive_into()

template<MutableByteBuffer Buffer>
void receive_into ( Buffer & buffer,
std::size_t expected_size )

Receive data into a resizable buffer.

Template Parameters
BufferMust satisfy MutableByteBuffer (ByteBuffer + resize)

Definition at line 75 of file concepts_example.cpp.

75 {
76 buffer.resize(expected_size);
77 std::cout << "Buffer resized to " << buffer.size() << " bytes\n";
78 // Fill with dummy data
79 for (std::size_t i = 0; i < buffer.size(); ++i) {
80 static_cast<uint8_t*>(buffer.data())[i] = static_cast<uint8_t>(i % 256);
81 }
82}

Referenced by demonstrate_mutable_buffer().

Here is the caller graph for this function:

◆ send_buffer()

template<ByteBuffer Buffer>
void send_buffer ( const Buffer & buffer)

Send data from any buffer type satisfying ByteBuffer concept.

Template Parameters
BufferMust satisfy ByteBuffer (has data() and size())

Definition at line 37 of file concepts_example.cpp.

37 {
38 std::cout << "Sending " << buffer.size() << " bytes from buffer\n";
39 // Access is compile-time guaranteed
40 const void* ptr = buffer.data();
41 (void)ptr; // Suppress unused warning in example
42}

Referenced by demonstrate_byte_buffer().

Here is the caller graph for this function:

◆ set_timeout()

template<Duration D>
void set_timeout ( D duration)

Set a timeout using any duration type.

Template Parameters
DMust satisfy Duration concept

Definition at line 342 of file concepts_example.cpp.

342 {
343 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
344 std::cout << "Timeout set to " << ms.count() << " milliseconds\n";
345}

Referenced by demonstrate_duration().

Here is the caller graph for this function:

◆ use_client()

template<NetworkClient Client>
void use_client ( Client & client,
const std::string & message )

Generic function that works with any NetworkClient.

Template Parameters
ClientMust satisfy NetworkClient concept

Definition at line 246 of file concepts_example.cpp.

246 {
247 if (!client.is_connected()) {
248 std::cout << "Client not connected, cannot send\n";
249 return;
250 }
251
252 std::vector<uint8_t> data(message.begin(), message.end());
253 client.send_packet(std::move(data));
254}

Referenced by demonstrate_network_client().

Here is the caller graph for this function: