diff --git a/CMakeLists.txt b/CMakeLists.txt index ea2c15c..7e8b844 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ add_library(order src/Options.cpp src/Order.cpp include/Order.h + src/OptionsException.cpp ) if (DEFINED ENABLE_TEST) diff --git a/include/Options.h b/include/Options.h index 5b30fab..29806c2 100644 --- a/include/Options.h +++ b/include/Options.h @@ -1,5 +1,7 @@ +// 版权所有 (c) ling 保留所有权利。 +// 除非另行说明,否则仅允许在parameter中使用此文件中的代码。 // -// Created by ling on 24-2-4. +// 由 ling 创建于 24-2-4. // #ifndef OPTIONS_H diff --git a/include/OptionsException.h b/include/OptionsException.h new file mode 100644 index 0000000..6cb9437 --- /dev/null +++ b/include/OptionsException.h @@ -0,0 +1,21 @@ +// 版权所有 (c) ling 保留所有权利。 +// 除非另行说明,否则仅允许在parameter中使用此文件中的代码。 +// +// 由 ling 创建于 24-2-7. +// + +#ifndef OPTIONSEXCEPTION_H +#define OPTIONSEXCEPTION_H +#include + + +namespace ling { + class OptionsException : public std::runtime_error { + public: + explicit OptionsException(const std::string &err); + + explicit OptionsException(const char *err); + }; +} // ling + +#endif //OPTIONSEXCEPTION_H diff --git a/include/Order.h b/include/Order.h index 7181bd0..2e817a2 100644 --- a/include/Order.h +++ b/include/Order.h @@ -1,5 +1,7 @@ +// 版权所有 (c) ling 保留所有权利。 +// 除非另行说明,否则仅允许在parameter中使用此文件中的代码。 // -// Created by ling on 24-2-4. +// 由 ling 创建于 24-2-4. // #ifndef ORDER_H diff --git a/src/Options.cpp b/src/Options.cpp index 43f867d..3b5e997 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -1,5 +1,7 @@ +// 版权所有 (c) ling 保留所有权利。 +// 除非另行说明,否则仅允许在parameter中使用此文件中的代码。 // -// Created by ling on 24-2-4. +// 由 ling 创建于 24-2-4. // #include "Options.h" diff --git a/src/OptionsException.cpp b/src/OptionsException.cpp new file mode 100644 index 0000000..c87389f --- /dev/null +++ b/src/OptionsException.cpp @@ -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 diff --git a/src/Order.cpp b/src/Order.cpp index 33ce466..a18cf53 100644 --- a/src/Order.cpp +++ b/src/Order.cpp @@ -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 #include #include -#include - -#define THROW(errStr) do{ this->err = errStr; return false; } while(false) +#include namespace ling { Order::DataStruct::DataStruct(const std::vector &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;