Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
System.hpp
1#pragma once
2
3#include "system/Config.hpp"
4#include "Common.hpp"
5
6#include <thread>
7
12class System {
13public:
15 enum class Picker {
16 Load,
17 Directory,
18 Save
19 };
20
28 static bool showPicker(Picker mode, const std::string & startDir, std::string & outPath, const std::string & extensions = "");
29
36 static bool createDirectory(const std::string & directory);
37
39 static void ping();
40
44 static double time();
45
49 static std::string timestamp();
50
56 static uint64_t hash64(const void* data, size_t size);
57
63 static uint32_t hash32(const void* data, size_t size);
64
72 template<typename ThreadFunc>
73 static void forParallel(size_t low, size_t high, ThreadFunc func) {
74 // Make sure the loop is increasing.
75 if(high < low) {
76 const size_t temp = low;
77 low = high;
78 high = temp;
79 }
80 // Prepare the threads pool.
81 // Always leave one thread free.
82 const size_t availableThreadsCount = std::max(int(std::thread::hardware_concurrency())-1, 1);
83 const size_t count = std::min(size_t(availableThreadsCount), high - low);
84 std::vector<std::thread> threads;
85 threads.reserve(count);
86
87 // Compute the span of each thread.
88 const size_t span = std::max(size_t(1), (high - low) / count);
89 // Helper to execute the function passed on a subset of the total interval.
90 auto launchThread = [&func](size_t a, size_t b) {
91 for(size_t i = a; i < b; ++i) {
92 func(i);
93 }
94 };
95
96 for(size_t tid = 0; tid < count; ++tid) {
97 // For each thread, call the same lambda with different bounds as arguments.
98 const size_t threadLow = tid * span;
99 const size_t threadHigh = tid == (count-1) ? high : ((tid + 1) * span);
100 threads.emplace_back(launchThread, threadLow, threadHigh);
101 }
102 // Wait for all threads to finish.
103 std::for_each(threads.begin(), threads.end(), [](std::thread & x) { x.join(); });
104 }
105
106 #ifdef _WIN32
107
112 static wchar_t * widen(const std::string & str);
113
118 static std::string narrow(wchar_t * str);
119
120 #else
121
126 static const char * widen(const std::string & str);
127
132 static std::string narrow(char * str);
133
134 #endif
135
136};
Performs system basic operations such as directory creation, timing, threading, file picking.
Definition: System.hpp:12
static void forParallel(size_t low, size_t high, ThreadFunc func)
Definition: System.hpp:73
static void ping()
Definition: System.cpp:75
static const char * widen(const std::string &str)
Definition: System.cpp:124
static uint32_t hash32(const void *data, size_t size)
Definition: System.cpp:101
static bool showPicker(Picker mode, const std::string &startDir, std::string &outPath, const std::string &extensions="")
Definition: System.cpp:27
static bool createDirectory(const std::string &directory)
Definition: System.cpp:69
Picker
Definition: System.hpp:15
@ Save
Save to a new or existing file.
@ Directory
open or create a directory.
@ Load
Load an existing file.
static uint64_t hash64(const void *data, size_t size)
Definition: System.cpp:97
static double time()
Definition: System.cpp:79
static std::string narrow(char *str)
Definition: System.cpp:127
static std::string timestamp()
Definition: System.cpp:83