C++ 一个字符串的代理类(String View)

在学习muduo/base源码的时候看到一个StringPiece类,是个string view,记录下来。

关于string view,看http://stackoverflow.com/questions/20803826/what-is-string-view

简单摘抄下来就是

The purpose of any and all kinds of “string reference” and “array reference” proposals is to avoid copying data which is already owned somewhere else and of which only a non-mutating view is required. The string_view in question is one such proposal; there were earlier ones called string_ref and array_ref, too.

The idea is always to store a pair of pointer-to-first-element and size of some existing data array or string.

string view解释过来就是个窗口,你通过这个窗口查看代理的字符串的内容,移动指针和改变长度就可以移动窗口,而且,自身不需要存储字符串,只读,不可修改,生命期应和代理的字符串一致。

muduo的StringPiece代码如下,个人做了一点修改:

class StringPiece {
public:
    StringPiece()
        : _ptr(nullptr),
          _length(0)
    {
    }

    StringPiece(const char* str)
        : _ptr(str),
          _length(strlen(_ptr))
    {
    }

    StringPiece(
请重构这部分代码:#include <array> #include <stdexcept> class SpeedSystem { public: // 默认构造函数 constexpr SpeedSystem() = default; // 带参数的构造函数 constexpr SpeedSystem(float speed, float rate, float accel, float decel, float accelTime, float decelTime, float sramp) : speed(speed) //, rate(rate) , accel(accel) , decel(decel) , accelTime(accelTime) , decelTime(decelTime) , sramp(sramp) {} // 代理类,用于处理赋值操作 class Proxy { public: Proxy(SpeedSystem& ss, const std::string& key) : SS_(ss), key_(key) {} // 重载赋值操作符 Proxy& operator=(float value) { SS_.setValue(key_, value); return *this; } // 重载赋值操作符,接受另一个 Proxy 对象 Proxy& operator=(const Proxy& other) { SS_.setValue(key_, static_cast<float>(other)); return *this; } // 重载类型转换操作符 operator float() const { return SS_.getValue(key_); } private: SpeedSystem& SS_; std::string key_; }; // 通过字符串键访问成员变量 Proxy operator[](const std::string& key) { return Proxy(*this, key); } float operator[](const std::string& key) const { return getValue(key); } private: float speed = 0; // 基础速度 units/s float rate = 1; // 倍率 float realSpeed = 0; // 速度真值 float accel = 0; // 加速度 units/s^2 float decel = 0; // 减速度 units/s^2 float accelTime = 0; // 加速时间 s float decelTime = 0; // 减速时间 s float sramp = 0; // 平滑时间 ms float maxSpeed = 0; // 最高速度 float L0 = 0; // L0 float T0 = 0; // L0 void setValue(const std::string& key, float value) { if (key == "Speed") { speed = value; realSpeed = rate * speed; if ((maxSpeed > 0) && realSpeed > maxSpeed) { realSpeed = maxSpeed; } } else if (key == "Rate") { rate = value; realSpeed = rate * speed; if ((maxSpeed > 0) && realSpeed > maxSpeed) { realSpeed = maxSpeed; } } else if (key == "Accel") accel = value; else if (key == "Decel") decel = value; else if (key == "DecelTime") { decelTime = value; } else if (key == "AccelTime") { accelTime = value; } else if (key == "Sramp") sramp = value; else if (key == "maxSpeed") { maxSpeed = value; if (realSpeed > maxSpeed) { realSpeed = maxSpeed; } } else if (key == "L0") L0 = value; else if (key == "T0") T0 = value; else throw std::invalid_argument("Invalid key: " + key); } float getValue(const std::string& key) const { if (key == "Speed") return speed; else if (key == "Rate") return rate; else if (key == "Accel") return accel; else if (key == "Decel") return decel; else if (key == "DecelTime") return decelTime; else if (key == "AccelTime") return accelTime; else if (key == "Sramp") return sramp; else if (key == "maxSpeed") return maxSpeed; else if (key == "L0") return L0; else if (key == "T0") return T0; else if ("realSpeed" == key) return realSpeed; else throw std::invalid_argument("Invalid key: " + key); } }; #endif
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值