How to handle units in c++ interface

前端 未结 9 2122
独厮守ぢ
独厮守ぢ 2021-02-07 02:56

I am currently designing an API where I want that the user to be able to write code like this:

PowerMeter.forceVoltage(1 mV);
PowerMeter.settlingTime(1 ms);
         


        
9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-07 03:37

    Here's what I came up with... pretty much the same idea as Anders K, but since I wrote the code, I'll post it:

    #include 
    
    using namespace std;
    
    class MilliVoltsValue;
    class VoltsValue;
    
    class VoltsValue
    {
    public:
       explicit VoltsValue(float v = 0.0f) : _volts(v) {/* empty */}
       VoltsValue(const MilliVoltsValue & mV);
    
       operator float() const {return _volts;}
    
    private:
       float _volts;
    };
    
    class MilliVoltsValue
    {
    public:
       explicit MilliVoltsValue(float mV = 0.0f) : _milliVolts(mV) {/* empty */}
       MilliVoltsValue(const VoltsValue & v) : _milliVolts(v*1000.0f) {/* empty */}
    
       operator float() const {return _milliVolts;}
    
    private:
       float _milliVolts;
    };
    
    VoltsValue :: VoltsValue(const MilliVoltsValue & mV) : _volts(mV/1000.0f) {/* empty */}
    
    class PowerMeter
    {
    public:
       PowerMeter() {/* empty */}
    
       void forceVoltage(const VoltsValue & v) {_voltsValue = v;}
       VoltsValue getVoltage() const {return _voltsValue;}
    
    private:
       VoltsValue _voltsValue;
    };
    
    int main(int argc, char ** argv)
    {
       PowerMeter meter;
    
       meter.forceVoltage(VoltsValue(5.0f));
       cout << "Current PowerMeter voltage is " << meter.getVoltage() << " volts!" << endl;
    
       meter.forceVoltage(MilliVoltsValue(2500.0f));
       cout << "Now PowerMeter voltage is " << meter.getVoltage() << " volts!" << endl;
    
       // The line below will give a compile error, because units aren't specified
       meter.forceVoltage(3.0f);   // error!
    
       return 0;
    }
    

提交回复
热议问题