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