1
\$\begingroup\$

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?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Just a warning about code review, any time the code contains ... the question is likely to be closed as off topic. It is very difficult to review partial code. \$\endgroup\$
    – pacmaninbw
    Aug 28, 2019 at 13:00
  • \$\begingroup\$ This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does and the code should be complete enough to be reviewable. This is currently missing too much context I think, although clarification of the post may alleviate that. \$\endgroup\$
    – Mast
    Aug 28, 2019 at 13:00
  • \$\begingroup\$ If you're using classes, why don't you instantiate the class and only access the member variables? What made you pick this solution instead? If you need to set global settings that you can't workaround because they're external, well, tough luck, there is no alternative for global usage. But that would be obvious, so I suppose you got another problem somewhere. \$\endgroup\$
    – Mast
    Aug 28, 2019 at 13:02
  • \$\begingroup\$ @pacmaninbw I see I'll be working on an edit later tonight, appreciate the advice. \$\endgroup\$ Aug 28, 2019 at 17:31

0

Browse other questions tagged or ask your own question.