nholthaus / units

a compile-time, header-only, dimensional analysis and unit conversion library built on c++14 with no dependencies.
http://nholthaus.github.io/units/
MIT License
934 stars 134 forks source link

Support the MSFS SDK #304

Open nakajimayoshi opened 1 year ago

nakajimayoshi commented 1 year ago

Please include the following information in your issue:

  1. Which version of units you are using 2.3.3

  2. Which compiler exhibited the problem (including compiler version) VSC 2022

My earlier issue led me to investigate more and I found that the issue was related to a self-defined function, not the units library . However, upon further investigation I found that the units library has macro clashes with the target architecture (MSFS).

Background

Microsoft Flight Simulator 2020 is the latest iteration of the decades long Microsoft Flight Simulator series and an extremely diverse program with thousands of different tools and ways to conduct real-life aero simulations. MSFS recently released a variety of tools, namely building Web Assembly modules using C/C++ that can do anything from rendering displays, drawing 2d vector graphics, and calculating system inputs and outputs (e.g. battery voltage and current).

Problem

When writing these modules, we cannot use standard architecture targets like x64 and x86, but rather an Asobo developed toolset which leverages the MSFS SDK. This architecture is specifically designed to compile C/C++ to a .wasm binary rather than a common architecture, ensuring compatibility with a variety of systems as well as Xbox where the simulator is also available. I've explored other C++ unit libraries, but I am a proponent of this one despite the incompatibilities for three reasons:

We'd like to guarantee type safety and strict adherence to dimensional analysis for recreating large and complex aircraft systems, but as of now I'm afraid there are no C++ libraries out there that support the C++ WASM framework published by Microsoft and Asobo.

Solution/info

Fortunately this seems to be an easily fixable problem, mostly related to the min() macro invoked on line 2065 of the header file

    detail::abs(nls::m_value - units::convert<UnitsRhs, Units>(rhs.m_value)) < std::numeric_limits<T>::min()

I've provided a screenshot of the error list below, as well as a link to download the latest version of the MSFS SDK if you'd like to try it. I will provide updates as I attempt to fix the library to support the architecture.

image MRE:

#include "units.h"

class MainBattery {
public:
    auto getPower() -> units::power::watt_t;
};

auto MainBattery::getPower() -> units::power::watt_t {
    return static_cast<units::power::watt_t>(0.);
};

MSFS SDK Installer SDK Documentation

ts826848 commented 1 year ago

This smells like an issue due to <Windows.h> and/or <windef.h> defining min and max macros, which conflict with functions of the same name. If the issue is indeed the same, you'd get similar errors if you include <algorithm> or <limits>. I admittedly have not tried installing the SDK, so I can't say for sure whether that's the actual reason.

Is defining NOMINMAX before including anything that includes possible, or would that break something else?

nakajimayoshi commented 1 year ago

Defining NOMINMAX isn't possible, as it would break the program, but I was able to get around the issue by renaming the min and max macros in Windows.h to minimum and maximum