C ++中的测量单位 [英] Units of measurement in C++

查看:98
本文介绍了C ++中的测量单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个游戏引擎,目前我正在设计IO系统。我已经这么做,引擎本身不处理任何文件格式,而是让用户实现他想要的任何东西,创建一个 *。dll 文件中带有appriopriatly命名的函数。虽然这本身并不是一个问题,我的主要担心是可能会在引擎使用期间可见的影响。

I'm working on a game engine, and currently I'm stuck designing the IO system. I've made it so, the engine itself doesn't handle any file formats, but rather lets the user implement anything he wants by creating a *.dll file with appriopriatly named functions inside. While that itself wasn't much of a problem, my main concern are the implications that'll probably be visible during the usage of the engine.

我设计了一个简单的 resource 接口作为一个基类,用于所有的事情,用户可以想起来,我试图通过使简单的子类专用于通用的数据类型,用户不必自己实现基础(目前我在想音频 image data mesh )。从 audio 类开始,我对一个特殊问题进行了汇总,同时尝试决定应以何种类型存储有关采样率的信息。通常单位是赫兹,因此我决定将其设为 unsigned int

I designed a simple resource interface as a base class for all the things the user can think off, and I'm trying to extend it by making simple child classes dedicated to the common data types, so the user doesn't have to implement the basics by himself (currently I'm thinking of audio, image, data and mesh). Starting with the audio class, I've sumbled on a peculiar problem, while trying to decide in what type should I store information about sampling rate. The usual unit are hertz, so I decided to make it an unsigned int.

但是这里有一个小问题 - 如果用户尝试在千赫中设置它?让我们假设一些抽象文件格式可以存储在两个单位的片刻。我已经做了一个简单的包装类来命名单位类型:

However there's a little problem here - what if the user tries to set it in kilohertz? Lets assume some abstract file format can store it in both units for a moment. I've made a simple wrapper class to name the unit type:

class hertz{
private:
    unsigned int value;
    hertz(){};
public:
    operator unsigned int();
    hertz(unsigned int value);
};

并决定让用户也使用kHz:

and decided to let the user also use kHz:

class kilohertz{
private:
    float value;
    kilohertz(){};
public:
    operator hertz();
    kilohertz(float value);
};

audio 这使得用户设置采样率被声明为 track& samplingRate(units :: hertz rate); 。用户必须通过明确说明他使用的是什么样的数量级来调用它:

While the function inside the audio class, which lets the user set the sampling rate is declared as track& samplingRate(units::hertz rate);. The user has to call it by explicitly saying what order of magnitude he's using:

someAudioFile.samplingRate(hertz(44100));
someAudioFile.samplingRate(kilohertz(44.1));

我的问题是:


有没有更好的方法强迫用户以简单优雅的方式使用测量单位?一个设计模式可能是或者某些聪明的typedef使用?

Is there a better way to force the user to use a measurement unit in a simple and elegant way? A design pattern maybe, or some clever use of typedefs?

请注意,在创建引擎的过程中,更多的单位,将与赫兹不兼容。从我的头顶 - 我可能希望用户能够通过 units :: rgb(123,42,120)和<$ c $来设置像素颜色c> units :: hsl(10,30,240)。

Please also note that in the process of creating the engine, I may require more units which will be incompatible with Hertz. From the top of my head - I may want the user to be able to set an pixel color both by doing units::rgb(123,42,120) and units::hsl(10,30,240).

我试图搜索一个可行的答案, http://stackoverflow.com/questions/11027587/how-to-handle-units-in-c-interface\">这个问题,但是OP只需要数量级,而不确保单位不兼容

I've tried searching for a viable answer and only found this question, but the OP only wanted orders of magnitude without ensuring the units are not compatible with other ones.

另外请注意,我使用旧的 C ++ 版本,而不是 C ++ 11 。发布一个有效的解决方案在任何版本是伟大的,这将是很好,如果我也可以使用它:)

Also please note I'm using the old C++ version, not C++11. While posting a solution valid in any version is great, it would be nice if I could also use it :)

推荐答案

你提到你没有使用C ++ 11,但其他人正在看这个问题,所以这里是使用用户定义的字面量的C ++ 11解决方案:

I know you mentioned you aren't using C++11 but others looking at this question may be, so here's the C++11 solution using user defined literals:

http://ideone.com/UzeafE

#include <iostream>
using namespace std;

class Frequency
{
public:
    void Print() const { cout << hertz << "Hz\n"; }

    explicit constexpr Frequency(unsigned int h) : hertz(h) {}
private:
    unsigned int hertz;
};
constexpr Frequency operator"" _Hz(unsigned long long hz)
{
    return Frequency{hz};
}
constexpr Frequency operator"" _kHz(long double khz)
{
    return Frequency{khz * 1000};
}

int main()
{
    Frequency(44100_Hz).Print();
    Frequency(44.1_kHz).Print();
    return 0;
}

输出:

44100Hz
44100Hz

这篇关于C ++中的测量单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆