I've been trying to make a work-around for global usage; however, I can't seem to find an alternative.
I'd like to create a C++ suffix function which converts pixels to millimeters. A custom gui library I've been developing separates OS calls and the actual widget system in two separate headers (IoC); therefore, I do not have access to system-level DPI calls when creating widgets (which is when I'd want to use the suffix function). A kludgey work-around I've considered is maintaining a global variable / singleton. For example:
// widgets.h
...
class SystemProperties { // ! << SINGLETON CLASS HERE
std::pair<double, double> dpi = {1, 1};
SystemProperties() {}
static auto self() -> SystemProperties& {
static SystemProperties system_properties;
return system_properties;
}
public:
static auto set_dpi(const decltype(dpi)& dpi) {
SystemProperties::self().dpi = dpi;
}
static auto get_dpi_x() { return SystemProperties::self().dpi.first; }
static auto get_dpi_x() { return SystemProperties::self().dpi.second; }
};
double operator"" _inch_x(long double value) {
return value * SystemProperties::get_dpi_x(); // ! << GLOBAL USAGE HERE
}
double operator"" _inch_y(long double value) {
return value * SystemProperties::get_dpi_y(); // ! << GLOBAL USAGE HERE
}
...
// win32_implementation.h
#include "widgets.h"
...
auto init() {
UINT dpiX, dpiY;
GetDpiForMonitor(
::MonitorFromWindow(::GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY),
MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
SystemProperties::set_dpi({dpiX, dpiY}); // ! << GLOBAL USAGE HERE
}
...
Of course I could implement some kind of additional IoC that does dpi conversion, but that would miss the point of being able to use the suffix operator. Perhaps I am getting too hung up on that function? What are some other work-arounds if possible?
...
the question is likely to be closed as off topic. It is very difficult to review partial code. \$\endgroup\$