stmac.h

来自「一个语言识别引擎」· C头文件 代码 · 共 48 行

H
48
字号
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#ifndef stmac_h
#define stmac_h

#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <stdexcept>


#define DEBUG(x) cout << #x << ": " << x << endl
#define ensure(expression) if (!(expression)) \
          throw runtime_error(string(__FILE__) + ":" + rtoolbox::stringify(__LINE__) + \
                              ": failed to ensure `" + #expression + "'")

class BadConversion : public std::runtime_error {
public:
    BadConversion(const std::string& s) : std::runtime_error(s) { }
};

template<typename T>
inline std::string stringify(const T& x)
{
    std::ostringstream o;
    if (!(o << x))
        throw BadConversion(std::string("stringify(")
                            + typeid(x).name() + ")");
    return o.str();
}

template<typename T>
inline T numify(const std::string& s,
                bool failIfLeftoverChars = true)
{
    T x;
    std::istringstream i(s);
    char c;
    if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
        throw BadConversion(s);
    return x;
}



#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?