使用标准异常处理
This commit is contained in:
@@ -10,6 +10,7 @@ add_library(order
|
||||
src/Options.cpp
|
||||
src/Order.cpp
|
||||
include/Order.h
|
||||
src/OptionsException.cpp
|
||||
)
|
||||
|
||||
if (DEFINED ENABLE_TEST)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// 版权所有 (c) ling 保留所有权利。
|
||||
// 除非另行说明,否则仅允许在parameter中使用此文件中的代码。
|
||||
//
|
||||
// Created by ling on 24-2-4.
|
||||
// 由 ling 创建于 24-2-4.
|
||||
//
|
||||
|
||||
#ifndef OPTIONS_H
|
||||
|
||||
21
include/OptionsException.h
Normal file
21
include/OptionsException.h
Normal file
@@ -0,0 +1,21 @@
|
||||
// 版权所有 (c) ling 保留所有权利。
|
||||
// 除非另行说明,否则仅允许在parameter中使用此文件中的代码。
|
||||
//
|
||||
// 由 ling 创建于 24-2-7.
|
||||
//
|
||||
|
||||
#ifndef OPTIONSEXCEPTION_H
|
||||
#define OPTIONSEXCEPTION_H
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
namespace ling {
|
||||
class OptionsException : public std::runtime_error {
|
||||
public:
|
||||
explicit OptionsException(const std::string &err);
|
||||
|
||||
explicit OptionsException(const char *err);
|
||||
};
|
||||
} // ling
|
||||
|
||||
#endif //OPTIONSEXCEPTION_H
|
||||
@@ -1,5 +1,7 @@
|
||||
// 版权所有 (c) ling 保留所有权利。
|
||||
// 除非另行说明,否则仅允许在parameter中使用此文件中的代码。
|
||||
//
|
||||
// Created by ling on 24-2-4.
|
||||
// 由 ling 创建于 24-2-4.
|
||||
//
|
||||
|
||||
#ifndef ORDER_H
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// 版权所有 (c) ling 保留所有权利。
|
||||
// 除非另行说明,否则仅允许在parameter中使用此文件中的代码。
|
||||
//
|
||||
// Created by ling on 24-2-4.
|
||||
// 由 ling 创建于 24-2-4.
|
||||
//
|
||||
|
||||
#include "Options.h"
|
||||
|
||||
15
src/OptionsException.cpp
Normal file
15
src/OptionsException.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
// 版权所有 (c) ling 保留所有权利。
|
||||
// 除非另行说明,否则仅允许在parameter中使用此文件中的代码。
|
||||
//
|
||||
// 由 ling 创建于 24-2-7.
|
||||
//
|
||||
|
||||
#include "../include/OptionsException.h"
|
||||
|
||||
namespace ling {
|
||||
OptionsException::OptionsException(const std::string &err) : std::runtime_error(err) {
|
||||
}
|
||||
|
||||
OptionsException::OptionsException(const char *err) : std::runtime_error(err) {
|
||||
}
|
||||
} // ling
|
||||
@@ -1,5 +1,7 @@
|
||||
// 版权所有 (c) ling 保留所有权利。
|
||||
// 除非另行说明,否则仅允许在parameter中使用此文件中的代码。
|
||||
//
|
||||
// Created by ling on 24-2-4.
|
||||
// 由 ling 创建于 24-2-4.
|
||||
//
|
||||
|
||||
#include "../include/Order.h"
|
||||
@@ -7,9 +9,7 @@
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#define THROW(errStr) do{ this->err = errStr; return false; } while(false)
|
||||
#include <OptionsException.h>
|
||||
|
||||
namespace ling {
|
||||
Order::DataStruct::DataStruct(const std::vector<std::string> &option, const int id, const Type type, const std::string &memage) {
|
||||
@@ -48,7 +48,7 @@ namespace ling {
|
||||
bool Order::getOption(const int id) {
|
||||
const auto it = options.find(id);
|
||||
if (it == options.end())
|
||||
THROW("无效键");
|
||||
throw OptionsException("无效键:" + std::to_string(id));
|
||||
return it->second;
|
||||
}
|
||||
|
||||
@@ -97,45 +97,45 @@ namespace ling {
|
||||
if (rules == data.end()) {
|
||||
//检查匿名参数数量
|
||||
if (anonymity.size() >= anonymityNumber)
|
||||
THROW("无法解析的参数:" + *it);
|
||||
throw OptionsException("无法解析的参数:" + *it);
|
||||
anonymity.push_back(*it);
|
||||
continue;
|
||||
}
|
||||
if (it->at(0) != '-')
|
||||
THROW("无法解析的参数:" + *it);
|
||||
throw OptionsException("无法解析的参数:" + *it);
|
||||
switch (rules->second.type) {
|
||||
case OPT:
|
||||
if (options[rules->second.id])
|
||||
THROW("重复提供参数:" + *it);
|
||||
throw OptionsException("重复提供参数:" + *it);
|
||||
options[rules->second.id] = true;
|
||||
break;
|
||||
case STRING:
|
||||
if (opt_str.find(rules->second.id) != opt_str.end())
|
||||
THROW("重复提供参数:" + *it);
|
||||
throw OptionsException("重复提供参数:" + *it);
|
||||
if (std::next(it) == cmd.end())
|
||||
THROW("参数不足!" + *it + " 需要提供参数");
|
||||
throw OptionsException("参数不足!" + *it + " 需要提供参数");
|
||||
if (std::next(it)->at(0) == '-')
|
||||
THROW("参数不足!" + *it + " 需要提供参数");
|
||||
throw OptionsException("参数不足!" + *it + " 需要提供参数");
|
||||
++it;
|
||||
opt_str[rules->second.id] = *it;
|
||||
break;
|
||||
case INT:
|
||||
if (opt_int.find(rules->second.id) != opt_int.end())
|
||||
THROW("重复提供参数:" + *it);
|
||||
throw OptionsException("重复提供参数:" + *it);
|
||||
if (std::next(it) == cmd.end())
|
||||
THROW("参数不足!" + *it + " 需要提供参数");
|
||||
throw OptionsException("参数不足!" + *it + " 需要提供参数");
|
||||
if (std::next(it)->at(0) == '-')
|
||||
THROW("参数不足!" + *it + " 需要提供参数");
|
||||
throw OptionsException("参数不足!" + *it + " 需要提供参数");
|
||||
++it;
|
||||
opt_int[rules->second.id] = std::stoll(*it);
|
||||
break;
|
||||
case DOUBLE:
|
||||
if (opt_double.find(rules->second.id) != opt_double.end())
|
||||
THROW("重复提供参数:" + *it);
|
||||
throw OptionsException("重复提供参数:" + *it);
|
||||
if (std::next(it) == cmd.end())
|
||||
THROW("参数不足!" + *it + " 需要提供参数");
|
||||
throw OptionsException("参数不足!" + *it + " 需要提供参数");
|
||||
if (std::next(it)->at(0) == '-')
|
||||
THROW("参数不足!" + *it + " 需要提供参数");
|
||||
throw OptionsException("参数不足!" + *it + " 需要提供参数");
|
||||
++it;
|
||||
opt_double[rules->second.id] = std::stod(*it);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user