-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1368 lines (1274 loc) · 56.4 KB
/
main.cpp
File metadata and controls
1368 lines (1274 loc) · 56.4 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <filesystem>
#include <fstream>
#include <string>
#include <stdexcept>
#include <cstdlib>
#include <vector>
#include <map>
#include <cctype>
#include <cstdio>
namespace fs = std::filesystem;
// entry-point
int main(int argc, char* argv[])
{
std::string project_path, project_name;
// Helper method to execute system commands
auto execute_command = [](const std::string& command)
{
std::cout << "Executing: " << command << std::endl;
return std::system(command.c_str());
};
// Write content to file
auto write_file = [](const fs::path& file_path, const std::string& content)
{
std::ofstream file(file_path);
if (!file.is_open())
{
throw std::runtime_error("ERROR! Could not create file: " + file_path.string());
}
file << content;
file.flush();
file.close();
std::cout << "Created: " << file_path << std::endl;
};
// Helper method to sanitize project name for C++ identifiers
auto sanitize_cpp_name = [](const std::string& name)
{
std::string sanitized = name;
// Replace hyphens and other invalid characters with underscores
for (char& c : sanitized)
{
if (!std::isalnum(c) && c != '_')
{
c = '_';
}
}
// Ensure it starts with a letter or underscore
if (!sanitized.empty() && std::isdigit(sanitized[0]))
{
sanitized = "_" + sanitized;
}
return sanitized;
};
// Create directory structure
auto create_directory_structure = [&]()
{
std::cout << "Creating directory structure..." << std::endl;
std::vector<std::string> directories = {
project_path,
project_path + "/include",
project_path + "/include/core",
project_path + "/src",
project_path + "/build",
project_path + "/build/debug",
project_path + "/build/release",
project_path + "/tests"
};
for (const auto& dir : directories)
{
fs::create_directories(dir);
std::cout << "Created directory: " << dir << std::endl;
}
};
// Create template header files
auto copy_template_headers = [&]()
{
std::cout << "Creating template header files..." << std::endl;
// asyncops.hpp content
std::string asyncops_content = R"(
#pragma once
#include <stdexcept>
#include <exception>
#include <iostream>
#include <coroutine>
#include <vector>
#include <forward_list>
#include <thread>
#include <variant>
#include <utility>
#include <semaphore>
#include <memory>
#include <cassert>
/**********************************************************************************************
*
* Asynchronous Operations
* -----------------------
* This header provides utilities for asynchronous programming
* using C++20 coroutines. It includes:
* - A Generator class template for creating coroutine-based generators.
* - A GeneratorFactory class template for managing pools of objects.
* - An awaitable Task class template for defining asynchronous tasks.
* - A SyncWaitTask class template and sync_wait function for synchronously
* waiting on asynchronous tasks to complete.
*
* Developed by: Pooria Yousefi
* Date: 2025-06-26
* License: MIT
*
**********************************************************************************************/
// namespace pooriayousefi::core
namespace pooriayousefi::core
{
template<class T>
struct Generator
{
struct Promise
{
T current_value;
inline decltype(auto) initial_suspend() { return std::suspend_always{}; }
inline decltype(auto) final_suspend() noexcept { return std::suspend_always{}; }
inline decltype(auto) get_return_object() { return Generator{ std::coroutine_handle<Promise>::from_promise(*this) }; }
inline decltype(auto) return_void() { return std::suspend_never{}; }
inline decltype(auto) yield_value(T&& value) noexcept { current_value = value; return std::suspend_always{}; }
inline void unhandled_exception() { std::terminate(); }
};
using promise_type = Promise;
struct Sentinel {};
struct Iterator
{
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = T*;
using reference = T&;
using const_reference = const T&;
std::coroutine_handle<promise_type> handle;
explicit Iterator(std::coroutine_handle<promise_type>& h) :handle{ h } {}
inline Iterator& operator++()
{
handle.resume();
return *this;
}
inline void operator++(int) { (void)operator++(); }
inline reference operator*() { return handle.promise().current_value; }
inline pointer operator->() { return std::addressof(operator*()); }
inline const_reference operator*() const { return handle.promise().current_value; }
inline pointer operator->() const { return std::addressof(operator*()); }
inline bool operator==(Sentinel) { return handle.done(); }
inline bool operator==(Sentinel) const { return handle.done(); }
};
std::coroutine_handle<promise_type> handle;
explicit Generator(std::coroutine_handle<promise_type> h) :handle{ h } {}
~Generator() { if (handle) handle.destroy(); }
Generator(const Generator&) = delete;
Generator(Generator&& other) noexcept :handle(other.handle) { other.handle = nullptr; }
constexpr Generator& operator=(const Generator&) = delete;
constexpr Generator& operator=(Generator&& other) noexcept { handle = other.handle; other.handle = nullptr; return *this; }
inline T get_value() { return handle.promise().current_value; }
inline bool next() { handle.resume(); return !handle.done(); }
inline bool resume() { handle.resume(); return !handle.done(); }
inline decltype(auto) begin()
{
handle.resume();
return Iterator{ handle };
}
inline decltype(auto) end() { return Sentinel{}; }
inline T get_next_value()
{
next();
if (handle.done()) throw std::out_of_range{ "Generator exhausted" };
return get_value();
}
};
template<class T, size_t N = 128>
class GeneratorFactory
{
public:
using Pool = std::vector<T>;
using Pools = std::forward_list<Pool>;
static constexpr inline size_t number_of_objects_in_each_pool = N;
GeneratorFactory():m_pools{}, m_object_counter{ 0 }
{
m_pools.emplace_front(Pool(number_of_objects_in_each_pool, T{}));
}
virtual ~GeneratorFactory()
{
for (auto& pool : m_pools)
{
pool.clear();
}
m_pools.clear();
}
inline Generator<std::shared_ptr<T>> generate()
{
while (true)
{
if (m_object_counter < number_of_objects_in_each_pool)
{
co_yield std::make_shared<T>(m_pools.begin()->data()[m_object_counter++]);
}
else
{
m_pools.emplace_front(Pool(number_of_objects_in_each_pool, T{}));
m_object_counter = 0;
}
}
}
private:
Pools m_pools;
size_t m_object_counter;
};
template<class T>
struct Task
{
struct promise_type
{
std::variant<std::monostate, T, std::exception_ptr> result;
std::coroutine_handle<> continuation;
constexpr decltype(auto) get_return_object() noexcept { return Task{ *this }; }
constexpr void return_value(T value) { result.template emplace<1>(std::move(value)); }
constexpr void unhandled_exception() noexcept { result.template emplace<2>(std::current_exception()); }
constexpr decltype(auto) initial_suspend() { return std::suspend_always{}; }
struct awaitable
{
constexpr bool await_ready() noexcept { return false; }
constexpr decltype(auto) await_suspend(std::coroutine_handle<promise_type> h) noexcept
{
return h.promise().continuation;
}
constexpr void await_resume() noexcept {}
};
constexpr decltype(auto) final_suspend() noexcept { return awaitable{}; }
};
std::coroutine_handle<promise_type> handle;
explicit Task(promise_type& p) noexcept :handle{ std::coroutine_handle<promise_type>::from_promise(p) } {}
Task(Task&& t) noexcept :handle{ t.handle } {}
~Task() { if (handle) handle.destroy(); }
constexpr bool await_ready() { return false; }
constexpr decltype(auto) await_suspend(std::coroutine_handle<> c)
{
handle.promise().continuation = c;
return handle;
}
constexpr T await_resume()
{
auto& result = handle.promise().result;
if (result.index() == 1)
return std::get<1>(std::move(result));
else
std::rethrow_exception(std::get<2>(std::move(result)));
}
};
template<>
struct Task<void>
{
struct promise_type
{
std::exception_ptr e;
std::coroutine_handle<> continuation;
inline decltype(auto) get_return_object() noexcept { return Task{ *this }; }
constexpr void return_void() {}
inline void unhandled_exception() noexcept { e = std::current_exception(); }
constexpr decltype(auto) initial_suspend() { return std::suspend_always{}; }
struct awaitable
{
constexpr bool await_ready() noexcept { return false; }
inline decltype(auto) await_suspend(std::coroutine_handle<promise_type> h) noexcept
{
return h.promise().continuation;
}
constexpr void await_resume() noexcept {}
};
constexpr decltype(auto) final_suspend() noexcept { return awaitable{}; }
};
std::coroutine_handle<promise_type> handle;
explicit Task(promise_type& p) noexcept :handle{ std::coroutine_handle<promise_type>::from_promise(p) } {}
Task(Task&& t) noexcept :handle{ t.handle } {}
~Task() { if (handle) handle.destroy(); }
constexpr bool await_ready() { return false; }
inline decltype(auto) await_suspend(std::coroutine_handle<> c)
{
handle.promise().continuation = c;
return handle;
}
inline void await_resume()
{
if (handle.promise().e)
std::rethrow_exception(handle.promise().e);
}
};
template<class T> using ResultType = decltype(std::declval<T&>().await_resume());
template<class T>
struct SyncWaitTask
{
struct promise_type
{
T* value{ nullptr };
std::exception_ptr error{ nullptr };
std::binary_semaphore sema4{ 0 };
inline SyncWaitTask get_return_object() noexcept { return SyncWaitTask{ *this }; }
constexpr void unhandled_exception() noexcept { error = std::current_exception(); }
constexpr decltype(auto) yield_value(T&& x) noexcept
{
value = std::addressof(x);
return final_suspend();
}
constexpr decltype(auto) initial_suspend() noexcept { return std::suspend_always{}; }
struct awaitable
{
constexpr bool await_ready() noexcept { return false; }
constexpr void await_suspend(std::coroutine_handle<promise_type> h) noexcept { h.promise().sema4.release(); }
constexpr void await_resume() noexcept {}
};
constexpr decltype(auto) final_suspend() noexcept { return awaitable{}; }
constexpr void return_void() noexcept { assert(false); }
};
std::coroutine_handle<promise_type> handle;
explicit SyncWaitTask(promise_type& p) noexcept :handle{ std::coroutine_handle<promise_type>::from_promise(p) } {}
SyncWaitTask(SyncWaitTask&& t) noexcept :handle{ t.handle } {}
~SyncWaitTask() { if (handle) handle.destroy(); }
inline T&& get()
{
auto& p = handle.promise();
handle.resume();
p.sema4.acquire();
if (p.error)
std::rethrow_exception(p.error);
return static_cast<T&&>(*p.value);
}
};
template<class T> ResultType<T> sync_wait(T&& Task)
{
if constexpr (std::is_void_v<ResultType<T>>)
{
struct empty_type {};
auto coro = [&]() -> SyncWaitTask<empty_type>
{
co_await std::forward<T>(Task);
co_yield empty_type{};
assert(false);
};
coro().get();
}
else
{
auto coro = [&]() -> SyncWaitTask<ResultType<T>>
{
co_yield co_await std::forward<T>(Task);
assert(false);
};
return coro().get();
}
}
}
)";
// raiiiofsw.hpp content
std::string raiiiofsw_content = R"(
#pragma once
#include <type_traits>
#include <filesystem>
#include <stdexcept>
#include <typeinfo>
#include <fstream>
#include <string>
/**********************************************************************************************
*
* RAII Input/Output File Stream Wrapper
* -----------------------
* This header provides a RAII wrapper for basic input/output
* file streams. It includes:
* - A BasicInputFileStreamWrapper class template for managing file streams.
* - A BasicOutputFileStreamWrapper class template for managing file streams.
* - Specialization for std::byte for binary file streams.
*
* Developed by: Pooria Yousefi
* Date: 2025-06-26
* License: MIT
*
**********************************************************************************************/
namespace pooriayousefi::core
{
namespace raii
{
template<typename Elem, typename Traits = std::char_traits<Elem>, typename Alloc = std::allocator<Elem>>
struct BasicInputFileStreamWrapper
{
using file_stream_type = std::basic_ifstream<Elem, Traits>;
using type = BasicInputFileStreamWrapper<Elem, Traits, Alloc>;
using string_type = std::basic_string<Elem, Traits, Alloc>;
using stream_buffer_iterator = std::istreambuf_iterator<Elem, Traits>;
file_stream_type file_stream;
BasicInputFileStreamWrapper() :file_stream{} {}
virtual ~BasicInputFileStreamWrapper() { if (is_open()) close(); }
template<typename T> constexpr type& operator>>(T& value) { file_stream >> value; return *this; }
bool is_open() { return file_stream.is_open(); }
void close() { file_stream.close(); }
void open(std::filesystem::path file_path, std::ios_base::openmode open_mode = std::ios_base::in)
{
file_stream.open(file_path, std::ios_base::in | open_mode);
if (!is_open())
throw std::runtime_error(
std::string{
std::string{ "ERROR! Cannot open "} +
file_path.string() +
std::string{ " file in raii::BasicInputFileStreamWrapper<" } +
std::string{ typeid(Elem).name() } +
std::string{ ", " } +
std::string{ typeid(Traits).name() } +
std::string{ ", " } +
std::string{ typeid(Alloc).name() } +
std::string{ ">::open() method." }
}.c_str()
);
}
};
template<>
struct BasicInputFileStreamWrapper<std::byte>
{
using file_stream_type = std::basic_ifstream<std::byte>;
using type = BasicInputFileStreamWrapper<std::byte>;
using string_type = std::basic_string<std::byte>;
using stream_buffer_iterator = std::istreambuf_iterator<std::byte>;
file_stream_type file_stream;
BasicInputFileStreamWrapper() :file_stream{} {}
virtual ~BasicInputFileStreamWrapper() { if (is_open()) close(); }
template<typename T> constexpr type& operator>>(T& value) { file_stream >> value; return *this; }
bool is_open() { return file_stream.is_open(); }
void close() { file_stream.close(); }
void open(std::filesystem::path file_path, std::ios_base::openmode open_mode = std::ios_base::in | std::ios_base::binary)
{
file_stream.open(file_path, std::ios_base::in | std::ios_base::binary | open_mode);
if (!is_open())
throw std::runtime_error(
std::string{
std::string{"ERROR! Cannot open "} +
file_path.string() +
std::string{" file in raii::BasicInputFileStreamWrapper<std::byte>::open() method." }
}.c_str()
);
}
};
template<typename Elem, typename Traits = std::char_traits<Elem>, typename Alloc = std::allocator<Elem>>
struct BasicOutputFileStreamWrapper
{
using file_stream_type = std::basic_ofstream<Elem, Traits>;
using type = BasicOutputFileStreamWrapper<Elem, Traits, Alloc>;
using string_type = std::basic_string<Elem, Traits, Alloc>;
using stream_buffer_iterator = std::ostreambuf_iterator<Elem, Traits>;
file_stream_type file_stream;
BasicOutputFileStreamWrapper() :file_stream{} {}
virtual ~BasicOutputFileStreamWrapper() { if (is_open()) close(); }
template<typename T> constexpr type& operator<<(const T& value) { file_stream << value; return *this; }
template<typename T> constexpr type& operator<<(T&& value) noexcept { file_stream << value; return *this; }
bool is_open() { return file_stream.is_open(); }
void close() { file_stream.flush(); file_stream.close(); }
void open(std::filesystem::path file_path, std::ios_base::openmode open_mode = std::ios_base::out)
{
file_stream.open(file_path, std::ios::out | open_mode);
if (!is_open())
throw std::runtime_error(
std::string{
std::string{"ERROR! Cannot open "} +
file_path.string() +
std::string{" file in raii::BasicOutputFileStreamWrapper<"} +
std::string{typeid(Elem).name()} +
std::string{", "} +
std::string{typeid(Traits).name()} +
std::string{", "} +
std::string{ typeid(Alloc).name() } +
std::string{ "::open() method." }
}.c_str()
);
}
};
template<>
struct BasicOutputFileStreamWrapper<std::byte>
{
using file_stream_type = std::basic_ofstream<std::byte>;
using type = BasicOutputFileStreamWrapper<std::byte>;
using string_type = std::basic_string<std::byte>;
using stream_buffer_iterator = std::ostreambuf_iterator<std::byte>;
file_stream_type file_stream;
BasicOutputFileStreamWrapper() :file_stream{} {}
virtual ~BasicOutputFileStreamWrapper() { if (is_open()) close(); }
template<typename T> constexpr type& operator<<(const T& value) { file_stream << value; return *this; }
template<typename T> constexpr type& operator<<(T&& value) noexcept { file_stream << value; return *this; }
bool is_open() { return file_stream.is_open(); }
void close() { file_stream.flush(); file_stream.close(); }
void open(std::filesystem::path file_path, std::ios_base::openmode open_mode = std::ios_base::out | std::ios_base::binary)
{
file_stream.open(file_path, std::ios::out | std::ios_base::binary | open_mode);
if (!is_open())
throw std::runtime_error(
std::string{
std::string{"ERROR! Cannot open "} +
file_path.string() +
std::string{" file in raii::BasicOutputFileStreamWrapper<std::byte>::open() method." }
}.c_str()
);
}
};
namespace native
{
namespace narrow_encoded
{
using InputFileStreamWrapper = BasicInputFileStreamWrapper<char>;
using OutputFileStreamWrapper = BasicOutputFileStreamWrapper<char>;
}
namespace wide_encoded
{
using InputFileStreamWrapper = BasicInputFileStreamWrapper<wchar_t>;
using OutputFileStreamWrapper = BasicOutputFileStreamWrapper<wchar_t>;
}
}
#if __cplusplus >= 202002L
namespace utf8
{
using InputFileStreamWrapper = BasicInputFileStreamWrapper<char8_t>;
using OutputFileStreamWrapper = BasicOutputFileStreamWrapper<char8_t>;
}
#endif
namespace binary
{
using InputFileStreamWrapper = BasicInputFileStreamWrapper<std::byte>;
using OutputFileStreamWrapper = BasicOutputFileStreamWrapper<std::byte>;
}
}
}
)";
// stringformers.hpp content
std::string stringformers_content = R"(
#pragma once
#include <cctype>
#include <string>
#include <string_view>
#include <ranges>
#include <algorithm>
#include <vector>
#include <unordered_set>
#include <unordered_map>
/**********************************************************************************************
*
* String Transformers
* -------------------
* This header provides utility functions for string manipulation,
* including case conversion and tokenization.
*
* Developed by: Pooria Yousefi
* Date: 2025-06-26
* License: MIT
*
**********************************************************************************************/
namespace pooriayousefi::core
{
template<class Enc, class EncTraits = std::char_traits<Enc>, class EncAlloc = std::allocator<Enc>>
constexpr decltype(auto) to_lowercase(const std::basic_string<Enc, EncTraits, EncAlloc>& word)
{
std::basic_string<Enc, EncTraits, EncAlloc> lowercased_word{};
lowercased_word.resize(std::ranges::size(word));
std::ranges::transform(std::ranges::cbegin(word), std::ranges::cend(word),
std::ranges::begin(lowercased_word), [](const auto& c) { return std::tolower(c); });
return lowercased_word;
}
template<class Enc, class EncTraits = std::char_traits<Enc>, class EncAlloc = std::allocator<Enc>>
constexpr decltype(auto) to_lowercase(std::basic_string_view<Enc, EncTraits> word_view)
{
std::basic_string<Enc, EncTraits, EncAlloc> lowercased_word{};
lowercased_word.resize(std::ranges::size(word_view));
std::ranges::transform(std::ranges::cbegin(word_view), std::ranges::cend(word_view),
std::ranges::begin(lowercased_word), [](const auto& c) { return std::tolower(c); });
return lowercased_word;
}
template<class Enc, class EncTraits = std::char_traits<Enc>, class EncAlloc = std::allocator<Enc>>
constexpr decltype(auto) to_uppercase(const std::basic_string<Enc, EncTraits, EncAlloc>& word)
{
std::basic_string<Enc, EncTraits, EncAlloc> uppercased_word{};
uppercased_word.resize(std::ranges::size(word));
std::ranges::transform(std::ranges::cbegin(word), std::ranges::cend(word),
std::ranges::begin(uppercased_word), [](const auto& c) { return std::toupper(c); });
return uppercased_word;
}
template<class Enc, class EncTraits = std::char_traits<Enc>, class EncAlloc = std::allocator<Enc>>
constexpr decltype(auto) to_uppercase(std::basic_string_view<Enc, EncTraits> word_view)
{
std::basic_string<Enc, EncTraits, EncAlloc> uppercased_word{};
uppercased_word.resize(std::ranges::size(word_view));
std::ranges::transform(std::ranges::cbegin(word_view), std::ranges::cend(word_view),
std::ranges::begin(uppercased_word), [](const auto& c) { return std::toupper(c); });
return uppercased_word;
}
template<class T, class Traits = std::char_traits<T>>
constexpr void tokenize(
std::basic_string_view<T, Traits> src,
std::basic_string_view<T, Traits> delim,
std::vector<std::basic_string_view<T, Traits>>& tokens
)
{
tokens.clear();
tokens.reserve(src.size());
auto last_pos = src.find_first_not_of(delim, 0);
auto pos = src.find_first_of(delim, last_pos);
while (pos != std::basic_string_view<T, Traits>::npos || last_pos != std::basic_string_view<T, Traits>::npos)
{
tokens.emplace_back(src.substr(last_pos, pos - last_pos));
last_pos = src.find_first_not_of(delim, pos);
pos = src.find_first_of(delim, last_pos);
}
}
template<class T, class Traits = std::char_traits<T>>
constexpr void tokenize(
std::basic_string_view<T, Traits> src,
std::basic_string_view<T, Traits> delim,
std::unordered_set<std::basic_string_view<T, Traits>>& tokens
)
{
tokens.clear();
tokens.reserve(src.size());
auto last_pos = src.find_first_not_of(delim, 0);
auto pos = src.find_first_of(delim, last_pos);
while (pos != std::basic_string_view<T, Traits>::npos || last_pos != std::basic_string_view<T, Traits>::npos)
{
tokens.emplace(src.substr(last_pos, pos - last_pos));
last_pos = src.find_first_not_of(delim, pos);
pos = src.find_first_of(delim, last_pos);
}
}
template<class T, class Traits = std::char_traits<T>>
auto tokenize(
std::basic_string_view<T, Traits> src,
std::basic_string_view<T, Traits> delim,
std::unordered_map<std::basic_string_view<T, Traits>, size_t>& tokens
)
{
tokens.clear();
tokens.reserve(src.size());
auto last_pos = src.find_first_not_of(delim, 0);
auto pos = src.find_first_of(delim, last_pos);
while (pos != std::basic_string_view<T, Traits>::npos || last_pos != std::basic_string_view<T, Traits>::npos)
{
tokens[src.substr(last_pos, pos - last_pos)]++;
last_pos = src.find_first_not_of(delim, pos);
pos = src.find_first_of(delim, last_pos);
}
}
}
)";
// utilities.hpp content (truncated for brevity - the full content is very long)
std::string utilities_content = R"(
#pragma once
#include <concepts>
#include <type_traits>
#include <thread>
#include <ratio>
#include <utility>
#include <chrono>
#include <functional>
#include <cstdint>
#include <numbers>
#include <vector>
#include <algorithm>
#include <ranges>
#include <iterator>
#include <unordered_map>
#include <string_view>
#include <random>
#include <variant>
#include <iostream>
/**********************************************************************************************
*
* Utilities Header
* -----------------------
* This header provides general utility functions and classes.
* It includes:
* - A wait_for class template for sleeping for various time durations.
* - A runtime function template for measuring the execution time of a callable.
* - A convert namespace with functions for unit conversions and number base conversions.
* - A countdown function template for displaying a countdown in seconds.
* - An iterate function template for iterating over a range with a specified step size
* - Specializations of standard functors for std::byte and std::reference_wrapper.
* - A histogram function template for counting occurrences of elements in a range.
* - A frequencies function template for counting word frequencies in a string view.
* - A do_n_times_shuffle_and_sample function template for shuffling and sampling a range.
* - A Result struct template for encapsulating expected values or exceptions.
*
* Developed by: Pooria Yousefi
* Date: 2025-06-26
* License: MIT
*
**********************************************************************************************/
namespace pooriayousefi::core
{
template<typename T> concept Arithmetic = std::floating_point<T> || std::integral<T>;
template<Arithmetic T>
class wait_for
{
public:
wait_for() = delete;
wait_for(T value) :m_value{ value } {}
inline void nanoseconds() { std::this_thread::sleep_for(std::chrono::duration<double, std::ratio<1, 1'000'000'000>>(m_value)); }
inline void microseconds() { std::this_thread::sleep_for(std::chrono::duration<double, std::ratio<1, 1'000'000>>(m_value)); }
inline void milliseconds() { std::this_thread::sleep_for(std::chrono::duration<double, std::ratio<1, 1'000>>(m_value)); }
inline void seconds() { std::this_thread::sleep_for(std::chrono::duration<double, std::ratio<1>>(m_value)); }
inline void minutes() { std::this_thread::sleep_for(std::chrono::duration<double, std::ratio<60>>(m_value)); }
inline void hours() { std::this_thread::sleep_for(std::chrono::duration<double, std::ratio<3'600>>(m_value)); }
inline void days() { std::this_thread::sleep_for(std::chrono::duration<double, std::ratio<86'400>>(m_value)); }
private:
T m_value;
};
template<typename F, typename... Args>
constexpr decltype(auto) runtime(F&& f, Args&&... args)
{
if constexpr (std::is_void_v<std::invoke_result_t<F, Args...>>)
{
auto ti{ std::chrono::high_resolution_clock::now() };
std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
auto tf{ std::chrono::high_resolution_clock::now() };
return std::chrono::duration<double>(tf - ti).count();
}
else if constexpr (!std::is_void_v<std::invoke_result_t<F, Args...>>)
{
auto ti{ std::chrono::high_resolution_clock::now() };
auto retval{ std::invoke(std::forward<F>(f), std::forward<Args>(args)...) };
auto tf{ std::chrono::high_resolution_clock::now() };
return std::make_pair(std::move(retval), std::chrono::duration<double>(tf - ti).count());
}
}
namespace convert
{
template<std::floating_point T> constexpr T degrees_to_radians(T x) { return x * std::numbers::pi_v<T> / (T)180; }
template<std::floating_point T> constexpr T radians_to_degrees(T x) { return x * (T)180 / std::numbers::pi_v<T>; }
template<std::floating_point T> constexpr T Celsius_to_Fahrenheit(T x) { return (x * (T)9 / (T)5) + (T)32; }
template<std::floating_point T> constexpr T Fahrenheit_to_Celsius(T x) { return (x - (T)32) * (T)5 / (T)9; }
}
template<std::integral I>
constexpr void countdown(I nsec)
{
std::cout << "\nT-" << nsec << ' ';
std::this_thread::sleep_for(std::chrono::seconds(1));
for (auto i{ static_cast<int64_t>(nsec) - static_cast<int64_t>(1) }; i >= static_cast<int64_t>(0); --i)
{
std::cout << i << ' ';
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
template<std::input_or_output_iterator It, std::invocable<std::iter_value_t<It>&> F>
constexpr void iterate(It begin, size_t n, size_t step_size, F&& f)
{
size_t c(0);
auto it = begin;
do
{
std::invoke(std::forward<F>(f), *it);
c++;
} while (c < n && [&]() { it = std::ranges::next(it, step_size); return true; }());
}
}
namespace std
{
template<> struct hash<byte>
{
constexpr const size_t operator()(const byte& b) const
{
hash<size_t> hasher{};
return hasher(to_integer<size_t>(b));
}
};
template<> struct equal_to<byte>
{
constexpr const bool operator()(const byte& lb, const byte& rb) const
{
return to_integer<size_t>(lb) == to_integer<size_t>(rb);
}
};
template<class T> struct hash<reference_wrapper<const T>>
{
constexpr const size_t operator()(const reference_wrapper<const T>& ref) const
{
hash<T> hasher{};
return hasher(ref.get());
}
};
template<class T> struct equal_to<reference_wrapper<const T>>
{
constexpr const bool operator()(const reference_wrapper<const T>& lhs, const reference_wrapper<const T>& rhs) const
{
return lhs.get() == rhs.get();
}
};
}
)";
// Write all header files
write_file(project_path + "/include/core/asyncops.hpp", asyncops_content);
write_file(project_path + "/include/core/raiiiofsw.hpp", raiiiofsw_content);
write_file(project_path + "/include/core/stringformers.hpp", stringformers_content);
write_file(project_path + "/include/core/utilities.hpp", utilities_content);
std::cout << "Template header files created!" << std::endl;
};
// Create source file
auto create_source_files = [&]()
{
std::cout << "Creating source files..." << std::endl;
// Main source file template
std::string main_cpp_template = R"(
#include "asyncops.hpp"
#include "raiiiofsw.hpp"
#include "stringformers.hpp"
#include "utilities.hpp"
// entry-point
int main()
{
try
{
// start here ...
return EXIT_SUCCESS;
}
catch (const std::exception& xxx)
{
std::cerr << "Error: " << xxx.what() << std::endl;
return EXIT_FAILURE;
}
}
)";
write_file(project_path + "/src/main.cpp", main_cpp_template);
};
// Create build system
auto create_build_system = [&]()
{
std::cout << "Creating build system..." << std::endl;
// Create C++ build system executable
std::string build_cpp = "#include <iostream>\n";
build_cpp += "#include <string>\n";
build_cpp += "#include <vector>\n";
build_cpp += "#include <cstdlib>\n";
build_cpp += "#include <filesystem>\n\n";
build_cpp += "namespace fs = std::filesystem;\n\n";
build_cpp += "class BuildSystem\n";
build_cpp += "{\n";
build_cpp += "private:\n";
build_cpp += " std::string build_type_;\n";
build_cpp += " std::string output_type_;\n";
build_cpp += " \n";
build_cpp += " int execute_command(const std::string& command) const\n";
build_cpp += " {\n";
build_cpp += " std::cout << \"Executing: \" << command << std::endl;\n";
build_cpp += " return std::system(command.c_str());\n";
build_cpp += " }\n";
build_cpp += " \n";
build_cpp += "public:\n";
build_cpp += " BuildSystem() : build_type_(\"debug\"), output_type_(\"executable\")\n";
build_cpp += " {\n";
build_cpp += " }\n";
build_cpp += " \n";
build_cpp += " void set_build_type(const std::string& type)\n";
build_cpp += " {\n";
build_cpp += " build_type_ = type;\n";
build_cpp += " }\n";
build_cpp += " \n";
build_cpp += " void set_output_type(const std::string& type)\n";
build_cpp += " {\n";
build_cpp += " output_type_ = type;\n";
build_cpp += " }\n";
build_cpp += " \n";
build_cpp += " int build()\n";
build_cpp += " {\n";
build_cpp += " std::string build_dir = \"build/\" + build_type_;\n";
build_cpp += " fs::create_directories(build_dir);\n";
build_cpp += " \n";
build_cpp += " std::vector<std::string> source_files;\n";
build_cpp += " \n";
build_cpp += " // Collect all source files\n";
build_cpp += " for (const auto& entry : fs::recursive_directory_iterator(\"src\"))\n";
build_cpp += " {\n";
build_cpp += " if (entry.is_regular_file() && entry.path().extension() == \".cpp\")\n";
build_cpp += " {\n";
build_cpp += " source_files.push_back(entry.path().string());\n";
build_cpp += " }\n";
build_cpp += " }\n";
build_cpp += " \n";
build_cpp += " std::string compile_flags;\n";
build_cpp += " std::string link_flags;\n";
build_cpp += " std::string output_name;\n";
build_cpp += " \n";
build_cpp += " if (build_type_ == \"debug\")\n";
build_cpp += " {\n";
build_cpp += " compile_flags = \"-g -O0 -DDEBUG\";\n";
build_cpp += " }\n";
build_cpp += " else if (build_type_ == \"release\")\n";
build_cpp += " {\n";
build_cpp += " compile_flags = \"-O3 -DNDEBUG\";\n";
build_cpp += " }\n";
build_cpp += " \n";
build_cpp += " // Common flags\n";
build_cpp += " compile_flags += \" -std=c++23 -Wall -Wextra -Wpedantic -Iinclude -Iinclude/core\";\n";
build_cpp += " \n";
build_cpp += " if (output_type_ == \"executable\")\n";
build_cpp += " {\n";
build_cpp += " output_name = build_dir + \"/\" + \"" + project_name + "\";\n";
build_cpp += " }\n";
build_cpp += " else if (output_type_ == \"static\")\n";
build_cpp += " {\n";
build_cpp += " output_name = build_dir + \"/lib\" + \"" + project_name + "\" + \".a\";\n";
build_cpp += " compile_flags += \" -c\";\n";
build_cpp += " }\n";
build_cpp += " else if (output_type_ == \"dynamic\")\n";
build_cpp += " {\n";
build_cpp += " output_name = build_dir + \"/lib\" + \"" + project_name + "\" + \".so\";\n";
build_cpp += " compile_flags += \" -fPIC\";\n";
build_cpp += " link_flags += \" -shared\";\n";
build_cpp += " }\n";
build_cpp += " \n";
build_cpp += " std::cout << \"Building " + project_name + " (\" << build_type_ << \", \" << output_type_ << \")...\" << std::endl;\n";
build_cpp += " \n";
build_cpp += " if (output_type_ == \"static\")\n";
build_cpp += " {\n";
build_cpp += " // Compile to object files first\n";
build_cpp += " std::vector<std::string> object_files;\n";
build_cpp += " for (const auto& source : source_files)\n";
build_cpp += " {\n";
build_cpp += " std::string obj_file = build_dir + \"/\" + fs::path(source).stem().string() + \".o\";\n";
build_cpp += " object_files.push_back(obj_file);\n";
build_cpp += " \n";
build_cpp += " std::string compile_cmd = \"g++ \" + compile_flags + \" \" + source + \" -o \" + obj_file;\n";
build_cpp += " if (execute_command(compile_cmd) != 0)\n";
build_cpp += " {\n";
build_cpp += " return 1;\n";