如何实现一个具备基本功能函数的C++的自定义String字符串类

在这篇文章,我们创造一个自定义的C++字符串类,它具备基本的string操作功能。

为什么要创建自己的字符串类呢?其中的一个原因是,当你想要让自己的字符串类型区别于一般的string时,你就需要自定义字符串类。例如,在证券期货的交易策略服务系统中,策略一般具有“策略ID”和“策略实例ID”的属性,他们都是字符串类型。如果你想要在C++代码中严格的限定某些函数调用参数,如果应该输入“策略实例ID”,程序员不会由于粗心而错误的传入“策略ID”。你就需要实现自己的策略实例ID和策略ID。

废话不多说,我先上参考代码。下面的代码段就是策略ID和策略实例ID的代码。两者几乎一模一样,但是在C++编译过程中可以严格的区分两种类型,这就可以让编译器解决很多编码问题。


namespace framework
{

class StrategyId
{
public:
    StrategyId() {}
    StrategyId(const std::string& value) : value_(value) {}
    StrategyId(const char* value) : value_(value) {}
    operator std::string() const { return value_; }
    std::string str_value() const { return value_; }
    void str_value(const std::string& value) { value_ = value; }
    bool empty() const { return value_.empty(); }
    const char* c_str() const { return value_.c_str(); }
    bool operator==(const std::string& other) const { return other == value_; }
    bool operator==(const char* other) const { return other == value_; }
    size_t length() const { return value_.length(); }

private:
    std::string value_;
};

class StrategyInstId
{
public:
    StrategyInstId() {}
    StrategyInstId(const std::string& value) : value_(value) {}
    StrategyInstId(const char* value) : value_(value) {}
    operator std::string() const { return value_; }
    std::string str_value() const { return value_; }
    void str_value(const std::string& value) { value_ = value; }
    bool empty() const { return value_.empty(); }
    const char* c_str() const { return value_.c_str(); }
    bool operator==(const std::string& other) const { return other == value_; }
    bool operator==(const char* other) const { return other == value_; }
    size_t length() const { return value_.length(); }

private:
    std::string value_;
};

在本实例代码中,因为使用了fmtlib/fmt类库和daniele77/cli,还需要做相应的函数重载。另外,为了支持两种ID作为std::unordered_map的哈希键值(hash key),也需要自定义Key类型重载。我把这些代码放在了额外的"instance-inl.h"中。

#pragma once

namespace fmt
{
namespace v6
{

template <>
struct formatter<framework::StrategyId> : fmt::formatter<std::string>
{
    auto format(const framework::StrategyId& s, fmt::format_context& ctx)
    {
        return fmt::formatter<std::string>::format(s.str_value(), ctx);
    }
};

template <>
struct formatter<framework::StrategyInstId> : fmt::formatter<std::string>
{
    auto format(const framework::StrategyInstId& s, fmt::format_context& ctx)
    {
        return fmt::formatter<std::string>::format(s.str_value(), ctx);
    }
};

} // namespace v6
} // namespace fmt

namespace std
{
extern GWSDK_API ostream& operator<<(ostream& os, const framework::StrategyId& v);
extern GWSDK_API ostream& operator<<(ostream& os, const framework::StrategyInstId& v);
extern GWSDK_API istream& operator>>(istream& is, framework::StrategyId& v);
extern GWSDK_API istream& operator>>(istream& is, framework::StrategyInstId& v);

template <> struct hash<framework::StrategyId>
{
    std::size_t operator()(const framework::StrategyId& k) const
    {
        using std::hash;
        using std::size_t;
        using std::string;

        // Compute individual hash values for first,
        // second and third and combine them using XOR
        // and bit shifting:

        return hash<string>()(k.str_value());
    }
};
template <> struct hash<framework::StrategyInstId>
{
    std::size_t operator()(const framework::StrategyInstId& k) const
    {
        using std::hash;
        using std::size_t;
        using std::string;

        // Compute individual hash values for first,
        // second and third and combine them using XOR
        // and bit shifting:

        return hash<string>()(k.str_value());
    }
};

} // namespace std

#include "cli/cli.h"

namespace cli
{
template <> struct TypeDesc<framework::StrategyId>
{
    static const char* Name() { return "<framework::StrategyId>"; }
};
template <> struct TypeDesc<framework::StrategyInstId>
{
    static const char* Name() { return "<framework::StrategyInstId>"; }
};

}

我在实现OpenOrderCTP的时候使用了这些代码技术。OpenOrderCTP是上海期货交易所CTP API接口的二次封装,在普通机器上内部时延<30微妙,它实现委托状态和成交的严格排序与缓存、重演,实现了事务级别的原子交易功能,实现了复杂订单、多个委托打包自动重发的功能,还可以省心支持对接CTP之外的其他证券交易柜台,如LTS、QMT、UFT、TradeX等。如需技术支持和咨询,请联系微信号: funrun2019(下方扫码)。
在这里插入图片描述

下面的文章摘录自国外友人的网页,可做参考。

 

The string class has the following basic functionalities:

  1. Constructor with no arguments: This allocates the storage for the string object in the heap and assign the value as a NULL character.
  2. Constructor with only one argument : It accepts a pointer to a character or we can say if we pass an array of characters, accepts the pointer to the first character in the array then the constructor of the String class allocates the storage on the heap memory of the same size as of the passed array and copies the contents of the array to that allocated memory in heap. It copies the contents using the strcpy() function declared in cstring library.
    Befor
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值