06_C++精灵库之dynamicproperty.h头文件源代码(2026年1月15日版)

#ifndef DYNAMICPROPERTY_H
#define DYNAMICPROPERTY_H

#include <any>
#include <string>
#include <stdexcept>
#include <typeinfo>
#include <iostream>
#include <sstream>
#include <utility> // for std::pair

class DynamicProperty {
private:
    std::any value_;
    
public:
    // 构造函数
    DynamicProperty() = default;
    
    template<typename T>
    DynamicProperty(T value) : value_(std::move(value)) {}
       // 允许从 C 风格字符串隐式构造 DynamicProperty
	DynamicProperty(const char* str) {
	    if (str) {
	        value_ = std::string(str);
	    } else {
	        value_ = std::string(); // 或者 value_ = 0; 但建议统一为 string
	    }
	}    
    // 拷贝构造函数
    DynamicProperty(const DynamicProperty&) = default;
    
    // 移动构造函数
    DynamicProperty(DynamicProperty&&) = default;
    
    // 拷贝赋值运算符
    DynamicProperty& operator=(const DynamicProperty&) = default;
    
    // 移动赋值运算符
    DynamicProperty& operator=(DynamicProperty&&) = default;
    
    ~DynamicProperty() = default;
    
    DynamicProperty& operator=(const char* str) {
      if (str == nullptr) {
        value_ = 0; 
        // 或者更安全:throw std::invalid_argument("null pointer assigned to DynamicProperty");
      }else {
        value_ = std::string(str);
      }
    return *this;
   }
   
	// 类型转换运算符
	operator int() const {
	    if (auto* i = std::any_cast<int>(&value_)) {
	        return *i;
	    } else if (auto* d = std::any_cast<double>(&value_)) {
	        return static_cast<int>(*d);
	    } else if (auto* f = std::any_cast<float>(&value_)) {
	        return static_cast<int>(*f);
	    } else {
	        return 0; // 或者其他默认值,或者抛出异常
	    }
	}
	
	operator double() const {
	    if (auto* i = std::any_cast<int>(&value_)) {
	        return static_cast<double>(*i);
	    } else if (auto* d = std::any_cast<double>(&value_)) {
	        return *d;
	    } else if (auto* f = std::any_cast<float>(&value_)) {
	        return static_cast<double>(*f);
	    } else {
	        return 0.0;
	    }
	}
	
	operator float() const {
	    if (auto* i = std::any_cast<int>(&value_)) {
	        return static_cast<float>(*i);
	    } else if (auto* d = std::any_cast<double>(&value_)) {
	        return static_cast<float>(*d);
	    } else if (auto* f = std::any_cast<float>(&value_)) {
	        return *f;
	    } else {
	        return 0.0f;
	    }
	}
	
	operator bool() const {
	    if (auto* b = std::any_cast<bool>(&value_)) {
	        return *b;
	    } else if (auto* i = std::any_cast<int>(&value_)) {
	        return *i != 0;
	    } else if (auto* d = std::any_cast<double>(&value_)) {
	        return *d != 0.0;
	    } else if (auto* f = std::any_cast<float>(&value_)) {
	        return *f != 0.0f;
	    } else if (auto* s = std::any_cast<std::string>(&value_)) {
	        return !s->empty();
	    } else {
	        return false;
	    }
	}
	
	operator std::pair<float, float>() const {
    if (auto* p = std::any_cast<std::pair<float, float>>(&value_)) {
        return *p;
    } else {
        // 可选:抛出异常,或返回默认值
        throw std::runtime_error("DynamicProperty is not a std::pair<float, float>");
     }
   }

	
	operator std::string() const {
	    return to_string();
	}
	    
    
    // 获取值
    template<typename T>
    T get() const {
        try {
            return std::any_cast<T>(value_);
        } catch (const std::bad_any_cast& e) {
            throw std::runtime_error("DynamicProperty type mismatch: expected " + 
                                   std::string(typeid(T).name()) + 
                                   ", but got different type");
        }
    }
    
    // 设置值
    template<typename T>
    void set(T value) {
        value_ = std::move(value);
    }
    
    // 检查是否包含值
    bool has_value() const {
        return value_.has_value();
    }
    
    // 获取类型信息
    const std::type_info& type() const {
        return value_.type();
    }
    
    // 转换为字符串
    std::string to_string() const;
    
    // 算术运算符
    // 自增运算符
    DynamicProperty& operator++();        // 前缀 ++
    DynamicProperty operator++(int);      // 后缀 ++
    
    // 自减运算符
    DynamicProperty& operator--();        // 前缀 --
    DynamicProperty operator--(int);      // 后缀 --
    
    // 一元正号运算符
	DynamicProperty operator+() const {
	    return *this; // 一元正号通常返回对象本身
	}

	// 一元负号运算符
	DynamicProperty operator-() const {
	    if (auto* i = std::any_cast<int>(&value_)) {
	        return DynamicProperty(-(*i));
	    } else if (auto* d = std::any_cast<double>(&value_)) {
	        return DynamicProperty(-(*d));
	    } else if (auto* f = std::any_cast<float>(&value_)) {
	        return DynamicProperty(-(*f));
	    } else {
	        throw std::runtime_error("DynamicProperty does not support unary - for current type");
	    }
	}
    
    
	// 加法赋值运算符
	template<typename T>
	DynamicProperty& operator+=(const T& value) {
	    if (auto* i = std::any_cast<int>(&value_)) {
	        if constexpr (std::is_arithmetic_v<T>) {
	            *i += static_cast<int>(value);
	        } else {
	            throw std::runtime_error("DynamicProperty does not support += with non-arithmetic type for int");
	        }
	    } else if (auto* d = std::any_cast<double>(&value_)) {
	        if constexpr (std::is_arithmetic_v<T>) {
	            *d += static_cast<double>(value);
	        } else {
	            throw std::runtime_error("DynamicProperty does not support += with non-arithmetic type for double");
	        }
	    } else if (auto* f = std::any_cast<float>(&value_)) {
	        if constexpr (std::is_arithmetic_v<T>) {
	            *f += static_cast<float>(value);
	        } else {
	            throw std::runtime_error("DynamicProperty does not support += with non-arithmetic type for float");
	        }
	    } else if (auto* s = std::any_cast<std::string>(&value_)) {
	        // 使用字符串流来处理所有类型的转换
	        std::ostringstream oss;
	        oss << value;
	        *s += oss.str();
	    } else {
	        throw std::runtime_error("DynamicProperty does not support += for current type");
	    }
	    return *this;
	}
    
    // DynamicProperty 之间的加法运算符
	friend DynamicProperty operator+(const DynamicProperty& lhs, const DynamicProperty& rhs) {
	    if (lhs.is_int() && rhs.is_int()) {
	        return DynamicProperty(lhs.get<int>() + rhs.get<int>());
	    } else if (lhs.is_double() && rhs.is_double()) {
	        return DynamicProperty(lhs.get<double>() + rhs.get<double>());
	    } else if (lhs.is_float() && rhs.is_float()) {
	        return DynamicProperty(lhs.get<float>() + rhs.get<float>());
	    } else if (lhs.is_string() && rhs.is_string()) {
	        return DynamicProperty(lhs.get<std::string>() + rhs.get<std::string>());
	    } else {
	        // 尝试数值类型转换
	        if (lhs.is_int() || lhs.is_double() || lhs.is_float()) {
	            if (rhs.is_int() || rhs.is_double() || rhs.is_float()) {
	                // 数值类型相加
	                if (lhs.is_double() || rhs.is_double()) {
	                    return DynamicProperty(static_cast<double>(lhs) + static_cast<double>(rhs));
	                } else if (lhs.is_float() || rhs.is_float()) {
	                    return DynamicProperty(static_cast<float>(lhs) + static_cast<float>(rhs));
	                } else {
	                    return DynamicProperty(static_cast<int>(lhs) + static_cast<int>(rhs));
	                }
	            }
	        }
	        // 如果无法进行数值运算,转换为字符串相加
	        return DynamicProperty(lhs.to_string() + rhs.to_string());
	    }
	}
	
	// DynamicProperty 之间的减法运算符
	friend DynamicProperty operator-(const DynamicProperty& lhs, const DynamicProperty& rhs) {
	    // 只支持数值类型的减法
	    if (lhs.is_int() && rhs.is_int()) {
	        return DynamicProperty(lhs.get<int>() - rhs.get<int>());
	    } else if (lhs.is_double() && rhs.is_double()) {
	        return DynamicProperty(lhs.get<double>() - rhs.get<double>());
	    } else if (lhs.is_float() && rhs.is_float()) {
	        return DynamicProperty(lhs.get<float>() - rhs.get<float>());
	    } else {
	        // 尝试数值类型转换
	        if (lhs.is_int() || lhs.is_double() || lhs.is_float()) {
	            if (rhs.is_int() || rhs.is_double() || rhs.is_float()) {
	                // 数值类型相减
	                if (lhs.is_double() || rhs.is_double()) {
	                    return DynamicProperty(static_cast<double>(lhs) - static_cast<double>(rhs));
	                } else if (lhs.is_float() || rhs.is_float()) {
	                    return DynamicProperty(static_cast<float>(lhs) - static_cast<float>(rhs));
	                } else {
	                    return DynamicProperty(static_cast<int>(lhs) - static_cast<int>(rhs));
	                }
	            }
	        }
	        throw std::runtime_error("DynamicProperty does not support - between these types (only numeric types)");
	    }
	}
	
	// DynamicProperty 之间的乘法运算符
	friend DynamicProperty operator*(const DynamicProperty& lhs, const DynamicProperty& rhs) {
	    // 只支持数值类型的乘法
	    if (lhs.is_int() && rhs.is_int()) {
	        return DynamicProperty(lhs.get<int>() * rhs.get<int>());
	    } else if (lhs.is_double() && rhs.is_double()) {
	        return DynamicProperty(lhs.get<double>() * rhs.get<double>());
	    } else if (lhs.is_float() && rhs.is_float()) {
	        return DynamicProperty(lhs.get<float>() * rhs.get<float>());
	    } else {
	        // 尝试数值类型转换
	        if (lhs.is_int() || lhs.is_double() || lhs.is_float()) {
	            if (rhs.is_int() || rhs.is_double() || rhs.is_float()) {
	                // 数值类型相乘
	                if (lhs.is_double() || rhs.is_double()) {
	                    return DynamicProperty(static_cast<double>(lhs) * static_cast<double>(rhs));
	                } else if (lhs.is_float() || rhs.is_float()) {
	                    return DynamicProperty(static_cast<float>(lhs) * static_cast<float>(rhs));
	                } else {
	                    return DynamicProperty(static_cast<int>(lhs) * static_cast<int>(rhs));
	                }
	            }
	        }
	        throw std::runtime_error("DynamicProperty does not support * between these types (only numeric types)");
	    }
	}
	
	// DynamicProperty 之间的除法运算符
	friend DynamicProperty operator/(const DynamicProperty& lhs, const DynamicProperty& rhs) {
	    // 检查除零
	    if ((rhs.is_int() && rhs.get<int>() == 0) ||
	        (rhs.is_double() && rhs.get<double>() == 0.0) ||
	        (rhs.is_float() && rhs.get<float>() == 0.0f)) {
	        throw std::runtime_error("Division by zero");
	    }
	    
	    // 只支持数值类型的除法
	    if (lhs.is_int() && rhs.is_int()) {
	        return DynamicProperty(lhs.get<int>() / rhs.get<int>());
	    } else if (lhs.is_double() && rhs.is_double()) {
	        return DynamicProperty(lhs.get<double>() / rhs.get<double>());
	    } else if (lhs.is_float() && rhs.is_float()) {
	        return DynamicProperty(lhs.get<float>() / rhs.get<float>());
	    } else {
	        // 尝试数值类型转换
	        if (lhs.is_int() || lhs.is_double() || lhs.is_float()) {
	            if (rhs.is_int() || rhs.is_double() || rhs.is_float()) {
	                // 数值类型相除
	                if (lhs.is_double() || rhs.is_double()) {
	                    return DynamicProperty(static_cast<double>(lhs) / static_cast<double>(rhs));
	                } else if (lhs.is_float() || rhs.is_float()) {
	                    return DynamicProperty(static_cast<float>(lhs) / static_cast<float>(rhs));
	                } else {
	                    return DynamicProperty(static_cast<int>(lhs) / static_cast<int>(rhs));
	                }
	            }
	        }
	        throw std::runtime_error("DynamicProperty does not support / between these types (only numeric types)");
	    }
	}
	
	// DynamicProperty 之间的取模运算符(仅整数)
	friend DynamicProperty operator%(const DynamicProperty& lhs, const DynamicProperty& rhs) {
	    // 检查模零
	    if ((rhs.is_int() && rhs.get<int>() == 0)) {
	        throw std::runtime_error("Modulo by zero");
	    }
	    
	    if (lhs.is_int() && rhs.is_int()) {
	        return DynamicProperty(lhs.get<int>() % rhs.get<int>());
	    } else {
	        throw std::runtime_error("DynamicProperty does not support % between these types (only int)");
	    }
	}


    
    // 减法赋值运算符
    template<typename T>
    DynamicProperty& operator-=(const T& value) {
        if (auto* i = std::any_cast<int>(&value_)) {
            *i -= static_cast<int>(value);
        } else if (auto* d = std::any_cast<double>(&value_)) {
            *d -= static_cast<double>(value);
        } else if (auto* f = std::any_cast<float>(&value_)) {
            *f -= static_cast<float>(value);
        } else {
            throw std::runtime_error("DynamicProperty does not support -= for current type");
        }
        return *this;
    }
    
    // 乘法赋值运算符
    template<typename T>
    DynamicProperty& operator*=(const T& value) {
        if (auto* i = std::any_cast<int>(&value_)) {
            *i *= static_cast<int>(value);
        } else if (auto* d = std::any_cast<double>(&value_)) {
            *d *= static_cast<double>(value);
        } else if (auto* f = std::any_cast<float>(&value_)) {
            *f *= static_cast<float>(value);
        } else {
            throw std::runtime_error("DynamicProperty does not support *= for current type");
        }
        return *this;
    }
    
    // 除法赋值运算符
    template<typename T>
    DynamicProperty& operator/=(const T& value) {
        if (value == 0) {
            throw std::runtime_error("Division by zero");
        }
        if (auto* i = std::any_cast<int>(&value_)) {
            *i /= static_cast<int>(value);
        } else if (auto* d = std::any_cast<double>(&value_)) {
            *d /= static_cast<double>(value);
        } else if (auto* f = std::any_cast<float>(&value_)) {
            *f /= static_cast<float>(value);
        } else {
            throw std::runtime_error("DynamicProperty does not support /= for current type");
        }
        return *this;
    }
    
    // 模赋值运算符(仅整数)
    template<typename T>
    DynamicProperty& operator%=(const T& value) {
        if (value == 0) {
            throw std::runtime_error("Modulo by zero");
        }
        if (auto* i = std::any_cast<int>(&value_)) {
            *i %= static_cast<int>(value);
        } else {
            throw std::runtime_error("DynamicProperty does not support %= for current type (only int)");
        }
        return *this;
    }
    // 取模运算符
	template<typename T>
	DynamicProperty operator%(const T& value) const {
	    if (auto* i = std::any_cast<int>(&value_)) {
	        return DynamicProperty(*i % static_cast<int>(value));
	    } else {
	        throw std::runtime_error("DynamicProperty does not support % for current type (only int)");
	    }
	}

    
    // 比较运算符 - 针对 DynamicProperty 之间的比较
    bool operator==(const DynamicProperty& other) const;
    bool operator!=(const DynamicProperty& other) const;
    bool operator<(const DynamicProperty& other) const;
    bool operator>(const DynamicProperty& other) const;
    bool operator<=(const DynamicProperty& other) const;
    bool operator>=(const DynamicProperty& other) const;
    
    // 与具体类型的比较 - 简化版本,只支持相同类型比较
    template<typename T>
    bool operator==(const T& other) const {
        if (auto* i = std::any_cast<int>(&value_)) {
            return *i == other;
        } else if (auto* d = std::any_cast<double>(&value_)) {
            return *d == other;
        } else if (auto* f = std::any_cast<float>(&value_)) {
            return *f == other;
        } else if (auto* s = std::any_cast<std::string>(&value_)) {
            if constexpr (std::is_same_v<T, std::string>) {
                return *s == other;
            } else {
                // 对于非字符串类型,转换为字符串比较
                return *s == std::to_string(other);
            }
        } else if (auto* b = std::any_cast<bool>(&value_)) {
            return *b == other;
        }
        return false;
    }
    
    template<typename T>
    bool operator!=(const T& other) const {
        return !(*this == other);
    }
    
    template<typename T>
    bool operator<(const T& other) const {
        if (auto* i = std::any_cast<int>(&value_)) {
            return *i < other;
        } else if (auto* d = std::any_cast<double>(&value_)) {
            return *d < other;
        } else if (auto* f = std::any_cast<float>(&value_)) {
            return *f < other;
        } else if (auto* s = std::any_cast<std::string>(&value_)) {
            if constexpr (std::is_same_v<T, std::string>) {
                return *s < other;
            } else {
                // 对于非字符串类型,转换为字符串比较
                return *s < std::to_string(other);
            }
        }
        return false;
    }
    
    template<typename T>
    bool operator>(const T& other) const {
        if (auto* i = std::any_cast<int>(&value_)) {
            return *i > other;
        } else if (auto* d = std::any_cast<double>(&value_)) {
            return *d > other;
        } else if (auto* f = std::any_cast<float>(&value_)) {
            return *f > other;
        } else if (auto* s = std::any_cast<std::string>(&value_)) {
            if constexpr (std::is_same_v<T, std::string>) {
                return *s > other;
            } else {
                // 对于非字符串类型,转换为字符串比较
                return *s > std::to_string(other);
            }
        }
        return false;
    }
    
    template<typename T>
    bool operator<=(const T& other) const {
        return !(*this > other);
    }
    
    template<typename T>
    bool operator>=(const T& other) const {
        return !(*this < other);
    }
    
    // 流输出运算符
    friend std::ostream& operator<<(std::ostream& os, const DynamicProperty& prop);
    
    // 类型检查方法
    bool is_int() const { return value_.type() == typeid(int); }
    bool is_double() const { return value_.type() == typeid(double); }
    bool is_float() const { return value_.type() == typeid(float); }
    bool is_string() const { return value_.type() == typeid(std::string); }
    bool is_bool() const { return value_.type() == typeid(bool); }
    bool is_pair_float() const {    return value_.type() == typeid(std::pair<float, float>); }
    
};

#endif // DYNAMICPROPERTY_H

关于李兴球

李兴球的博客是Python创意编程原创博客
此条目发表在C++分类目录。将固定链接加入收藏夹。

发表回复