-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (124 loc) · 6.49 KB
/
Copy pathmain.cpp
File metadata and controls
164 lines (124 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <algorithm>
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include "../../src/foundation/id_allocator.h"
using namespace nativeapi;
// Window, Menu, MenuItem, TrayIcon and Display come from the IdTypeTag registry
// in id_allocator.h. They are only ever used as template arguments here, so the
// forward declarations that header already provides are enough — no need to pull
// in the full class definitions.
//
// This example used to declare its own placeholder structs with these names.
// That worked back when type tags were assigned by a runtime counter and any
// type at all could be passed. Tags are now compile-time constants from a
// central registry, so a type must be registered to be allocatable.
int main() {
std::cout << "IdAllocator Template Example" << std::endl;
std::cout << "============================" << std::endl;
// Example 1: Basic allocation for different object types
std::cout << "\n1. Basic Allocation:" << std::endl;
auto window_id = IdAllocator::Allocate<Window>();
auto menu_id = IdAllocator::Allocate<Menu>();
auto tray_id = IdAllocator::Allocate<TrayIcon>();
std::cout << "Window ID: 0x" << std::hex << window_id << std::dec
<< " (Type: " << IdAllocator::GetType(window_id)
<< ", Sequence: " << IdAllocator::GetSequence(window_id) << ")" << std::endl;
std::cout << "Menu ID: 0x" << std::hex << menu_id << std::dec
<< " (Type: " << IdAllocator::GetType(menu_id)
<< ", Sequence: " << IdAllocator::GetSequence(menu_id) << ")" << std::endl;
std::cout << "Tray ID: 0x" << std::hex << tray_id << std::dec
<< " (Type: " << IdAllocator::GetType(tray_id)
<< ", Sequence: " << IdAllocator::GetSequence(tray_id) << ")" << std::endl;
// Example 2: TryAllocate with error checking
std::cout << "\n2. TryAllocate (safer allocation):" << std::endl;
auto maybe_id = IdAllocator::TryAllocate<MenuItem>();
if (maybe_id != IdAllocator::kInvalidId) {
std::cout << "MenuItem ID allocated successfully: 0x" << std::hex << maybe_id << std::dec
<< std::endl;
} else {
std::cout << "MenuItem ID allocation failed" << std::endl;
}
// Example 3: ID validation and decomposition
std::cout << "\n3. ID Validation and Decomposition:" << std::endl;
std::cout << "Is window_id valid? " << (IdAllocator::IsValid(window_id) ? "Yes" : "No")
<< std::endl;
auto decomposed = IdAllocator::Decompose(window_id);
std::cout << "Window ID decomposed - Type: " << decomposed.first
<< ", Sequence: " << decomposed.second << std::endl;
// Example 4: Current count query
std::cout << "\n4. Current Counter Query:" << std::endl;
std::cout << "Current Window counter (before allocation): "
<< IdAllocator::GetCurrentCount<Window>() << std::endl;
auto new_window_id = IdAllocator::Allocate<Window>();
std::cout << "New Window ID: 0x" << std::hex << new_window_id << std::dec
<< " (Sequence: " << IdAllocator::GetSequence(new_window_id) << ")" << std::endl;
std::cout << "Current Window counter (after allocation): "
<< IdAllocator::GetCurrentCount<Window>() << std::endl;
// Example 5: Multiple allocations
std::cout << "\n5. Multiple Allocations:" << std::endl;
std::vector<IdAllocator::IdType> window_ids;
for (int i = 0; i < 5; ++i) {
window_ids.push_back(IdAllocator::Allocate<Window>());
}
std::cout << "Allocated " << window_ids.size() << " Window IDs:" << std::endl;
for (size_t i = 0; i < window_ids.size(); ++i) {
std::cout << " ID " << (i + 1) << ": 0x" << std::hex << window_ids[i] << std::dec
<< " (Sequence: " << IdAllocator::GetSequence(window_ids[i]) << ")" << std::endl;
}
// Example 6: Thread safety demonstration
std::cout << "\n6. Thread Safety Demonstration:" << std::endl;
std::vector<std::thread> threads;
std::vector<IdAllocator::IdType> thread_ids;
std::mutex ids_mutex;
const int num_threads = 3;
const int ids_per_thread = 10;
auto start_time = std::chrono::high_resolution_clock::now();
for (int i = 0; i < num_threads; ++i) {
threads.emplace_back([&thread_ids, &ids_mutex, ids_per_thread, i]() {
std::vector<IdAllocator::IdType> local_ids;
for (int j = 0; j < ids_per_thread; ++j) {
auto id = IdAllocator::Allocate<Display>();
local_ids.push_back(id);
}
{
std::lock_guard<std::mutex> lock(ids_mutex);
thread_ids.insert(thread_ids.end(), local_ids.begin(), local_ids.end());
}
});
}
for (auto& thread : threads) {
thread.join();
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
std::cout << "Allocated " << thread_ids.size() << " Display IDs from " << num_threads
<< " threads in " << duration.count() << " microseconds" << std::endl;
// Verify all IDs are unique
std::sort(thread_ids.begin(), thread_ids.end());
auto it = std::unique(thread_ids.begin(), thread_ids.end());
std::cout << "All IDs are unique: " << (it == thread_ids.end() ? "Yes" : "No") << std::endl;
// Example 7: Different object types
std::cout << "\n7. Different Object Types:" << std::endl;
auto display_id = IdAllocator::Allocate<Display>();
std::cout << "Display ID: 0x" << std::hex << display_id << std::dec
<< " (Type: " << IdAllocator::GetType(display_id) << ")" << std::endl;
// Example 8: Reset functionality (for testing)
std::cout << "\n8. Reset Functionality:" << std::endl;
std::cout << "Menu counter before reset: " << IdAllocator::GetCurrentCount<Menu>() << std::endl;
IdAllocator::Reset<Menu>();
std::cout << "Menu counter after reset: " << IdAllocator::GetCurrentCount<Menu>() << std::endl;
auto new_menu_id = IdAllocator::Allocate<Menu>();
std::cout << "New Menu ID after reset: 0x" << std::hex << new_menu_id << std::dec
<< " (Sequence: " << IdAllocator::GetSequence(new_menu_id) << ")" << std::endl;
// Example 9: Independent types after reset
std::cout << "\n9. Independent Types After Reset:" << std::endl;
auto window_after_reset = IdAllocator::Allocate<Window>();
std::cout << "Window ID after Menu reset: 0x" << std::hex << window_after_reset << std::dec
<< " (Sequence: " << IdAllocator::GetSequence(window_after_reset) << ")" << std::endl;
std::cout << "Window counter was not affected by Menu reset" << std::endl;
std::cout << "\nExample completed successfully!" << std::endl;
return 0;
}