Skip to content

Commit

Permalink
feat: 更新执行策略
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Oct 3, 2023
1 parent fff91e1 commit 4c9a7f1
Show file tree
Hide file tree
Showing 29 changed files with 754 additions and 322 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ message("* * * * * * * * * * * * * * * * *")

cmake_minimum_required(VERSION 3.2.5)

project(CThreadPool VERSION 1.2.0)
project(CThreadPool VERSION 1.2.1)

set(CMAKE_CXX_STANDARD 11)

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ int main() {
[2023.03.07 - v1.2.0 - Chunel]
* 优化windows版本功能
[2023.10.07 - v1.2.1 - Chunel]
* 更新执行策略,优化整体性能
------------
#### 附录-2. 联系方式
* 微信: ChunelFeng
Expand Down
2 changes: 2 additions & 0 deletions src/CBasic/CBasicInclude.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
#include "CBasicDefine.h"
#include "CStrDefine.h"
#include "CStdEx.h"
#include "CDescInfo.h"
#include "CStruct.h"

#endif //CGRAPH_CBASICINCLUDE_H
76 changes: 76 additions & 0 deletions src/CBasic/CDescInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: CDescInfo.h
@Time: 2023/2/19 15:56
@Desc: 通用描述信息
***************************/

#ifndef CGRAPH_CDESCINFO_H
#define CGRAPH_CDESCINFO_H

#include <string>

#include "CBasicDefine.h"

CGRAPH_NAMESPACE_BEGIN

class CDescInfo {
public:
/**
* 获取名称信息
* @return
*/
const std::string& getName() const {
return name_;
}

/**
* 获取唯一id信息
* @return
*/
const std::string& getSession() const {
return session_;
}

/**
* 获取描述信息
* @return
*/
const std::string& getDescription() const {
return description_;
}

/**
* 设置名称信息
* @param name
* @return
*/
virtual auto setName(const std::string& name)
-> decltype(this) {
name_ = name;
return this;
}

/**
* 设置描述信息
* @param description
* @return
*/
virtual auto setDescription(const std::string& description)
-> decltype(this) {
description_ = description;
return this;
}

virtual ~CDescInfo() = default;

protected:
std::string name_; // 名字
std::string session_; // 唯一id信息
std::string description_; // 描述信息
};

CGRAPH_NAMESPACE_END

#endif //CGRAPH_CDESCINFO_H
15 changes: 10 additions & 5 deletions src/CBasic/CException.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ CGRAPH_NAMESPACE_BEGIN

class CEXCEPTION : public std::exception {
public:
explicit CEXCEPTION(const std::string& info = CGRAPH_EMPTY) {
info_ = info.empty() ? CGRAPH_BASIC_EXCEPTION : info;
explicit CEXCEPTION(const std::string& info,
const std::string& locate = CGRAPH_EMPTY) {
/**
* 这里的设计,和CStatus有一个联动
* 如果不了解具体情况,不建议做任何修改
*/
exception_info_ = locate + " | " + info;
}

/**
* 获取异常信息
* @return
*/
[[nodiscard]] const char* what() const noexcept override {
return info_.c_str();
const char* what() const noexcept override {
return exception_info_.c_str();
}

private:
std::string info_; // 异常状态信息
std::string exception_info_; // 异常状态信息
};

CGRAPH_NAMESPACE_END
Expand Down
38 changes: 33 additions & 5 deletions src/CBasic/CFuncType.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,30 @@ enum class CFunctionType {
#define CGRAPH_EMPTY_FUNCTION \
return CStatus(); \

/** 不支持当前功能 */
#define CGRAPH_NO_SUPPORT \
return CStatus(CGRAPH_FUNCTION_NO_SUPPORT); \

/** 获取当前代码所在的位置信息 */
#define CGRAPH_GET_LOCATE \
(std::string(__FILE__) + " | " + std::string(__FUNCTION__) \
+ " | line = [" + ::std::to_string( __LINE__) + "]")


/** 生成一个包含异常位置的 CStatus
* 这里这样实现,是为了符合 CStatus 类似写法
* */
#define CErrStatus(info) \
CStatus(info, CGRAPH_GET_LOCATE) \

/** 返回异常信息和状态 */
#define CGRAPH_RETURN_ERROR_STATUS(info) \
return CStatus(info); \
return CErrStatus(info); \

/** 根据条件判断是否返回错误状态 */
#define CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(cond, info) \
if (unlikely(cond)) { CGRAPH_RETURN_ERROR_STATUS(info); } \

/** 不支持当前功能 */
#define CGRAPH_NO_SUPPORT \
return CErrStatus(CGRAPH_FUNCTION_NO_SUPPORT); \

/** 定义为不能赋值和拷贝的对象类型 */
#define CGRAPH_NO_ALLOWED_COPY(CType) \
Expand All @@ -60,7 +77,18 @@ enum class CFunctionType {

/** 抛出异常 */
#define CGRAPH_THROW_EXCEPTION(info) \
throw CException(info); \
throw CException(info, CGRAPH_GET_LOCATE); \

/** 在异常状态的情况下,抛出异常 */
#define CGRAPH_THROW_EXCEPTION_BY_STATUS(status) \
if (unlikely((status).isErr())) { \
CGRAPH_THROW_EXCEPTION((status).getInfo()); \
} \

/** 根据条件判断是否抛出异常 */
#define CGRAPH_THROW_EXCEPTION_BY_CONDITION(cond, info) \
if (unlikely(cond)) { CGRAPH_THROW_EXCEPTION(info); } \


CGRAPH_NAMESPACE_END

Expand Down
80 changes: 38 additions & 42 deletions src/CBasic/CStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,116 +19,112 @@ CGRAPH_NAMESPACE_BEGIN
/**
* 说明:
* 返回值为0,表示正常逻辑
* 返回值为正整数,表示warning逻辑,程序仍会继续执行
* 返回值为负整数,表示error逻辑,程序终止执行
* 自定义返回值,请务必遵守以上约定
*/
static const int STATUS_OK = 0; /** 正常流程返回值 */
static const int STATUS_ERR = -1; /** 异常流程返回值 */
static const int STATUS_CRASH = -996; /** 异常流程返回值 */
static const char* STATUS_ERROR_INFO_CONNECTOR = " && "; /** 多异常信息连接符号 */

class CSTATUS {
public:
explicit CSTATUS() = default;

explicit CSTATUS(const std::string &errorInfo) {
explicit CSTATUS(const std::string &errorInfo,
const std::string &locateInfo = CGRAPH_EMPTY) {
this->error_code_ = STATUS_ERR; // 默认的error code信息
this->error_info_ = errorInfo;
this->error_locate_ = locateInfo;
}

explicit CSTATUS(int errorCode, const std::string &errorInfo) {
explicit CSTATUS(int errorCode, const std::string &errorInfo,
const std::string &locateInfo = CGRAPH_EMPTY) {
this->error_code_ = errorCode;
this->error_info_ = errorInfo;
this->error_locate_ = locateInfo;
}

CSTATUS(const CSTATUS &status) {
this->error_code_ = status.error_code_;
this->error_info_ = status.error_info_;
this->error_locate_ = status.error_locate_;
}

CSTATUS(const CSTATUS &&status) noexcept {
this->error_code_ = status.error_code_;
this->error_info_ = status.error_info_;
this->error_locate_ = status.error_locate_;
}

CSTATUS& operator=(const CSTATUS& status) = default;

CSTATUS& operator+=(const CSTATUS& cur) {
if (this->isOK() && cur.isOK()) {
return (*this);
/**
* 如果当前状态已经异常,则不做改动
* 如果当前状态正常,并且传入的状态是异常的话,则返回异常
*/
if (!this->isErr() && cur.isErr()) {
this->error_code_ = cur.error_code_;
this->error_info_ = cur.error_info_;
this->error_locate_ = cur.error_locate_;
}

error_info_ = this->isOK()
? cur.error_info_
: (cur.isOK()
? error_info_
: (error_info_ + STATUS_ERROR_INFO_CONNECTOR + cur.error_info_));
error_code_ = STATUS_ERR;

return (*this);
}

void setStatus(const std::string& info) {
error_code_ = STATUS_ERR;
error_info_ = info;
}

void setStatus(int code, const std::string& info) {
error_code_ = code;
error_info_ = info;
}

[[nodiscard]] int getCode() const {
/**
* 获取异常值信息
* @return
*/
int getCode() const {
return this->error_code_;
}

[[nodiscard]] const std::string& getInfo() const {
/**
* 获取异常信息
* @return
*/
const std::string& getInfo() const {
return this->error_info_;
}

/**
* 恢复数据
* 获取报错位置
* @return
*/
void reset() {
error_code_ = STATUS_OK;
error_info_ = CGRAPH_EMPTY;
const std::string& getLocate() const {
return this->error_locate_;
}

/**
* 判断当前状态是否可行
* @return
*/
[[nodiscard]] bool isOK() const {
bool isOK() const {
return STATUS_OK == error_code_;
}

/**
* 判断当前状态是否可行
* @return
*/
[[nodiscard]] bool isErr() const {
bool isErr() const {
return error_code_ < STATUS_OK; // 约定异常信息,均为负值
}

/**
* 判断当前状态是否有异常
* @return
*/
[[nodiscard]] bool isNotErr() const {
return error_code_ >= STATUS_OK;
}

/**
* 判断当前状态,不是ok的(包含error 和 warning)
* 判断当前状态是否是崩溃了
* @return
*/
[[nodiscard]] bool isNotOK() const {
return error_code_ != STATUS_OK;
bool isCrash() const {
return STATUS_CRASH == error_code_;
}

private:
int error_code_ { STATUS_OK }; // 错误码信息
int error_code_ = STATUS_OK; // 错误码信息
std::string error_info_; // 错误信息描述
std::string error_locate_; // 错误发生的具体位置,形如:file|function|line
};

CGRAPH_NAMESPACE_END
Expand Down
13 changes: 8 additions & 5 deletions src/CBasic/CStrDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
#ifndef CGRAPH_CSTRDEFINE_H
#define CGRAPH_CSTRDEFINE_H

#include <string>

#include "CBasicDefine.h"

CGRAPH_NAMESPACE_BEGIN

static const char* CGRAPH_EMPTY = "";
static const char* CGRAPH_DEFAULT = "default";
static const char* CGRAPH_UNKNOWN = "unknown";
static const char* CGRAPH_BASIC_EXCEPTION = "CGraph default exception";
static const char* CGRAPH_FUNCTION_NO_SUPPORT = "CGraph function no support";
static const std::string& CGRAPH_EMPTY = "";
static const std::string& CGRAPH_DEFAULT = "default";
static const std::string& CGRAPH_UNKNOWN = "unknown";
static const std::string& CGRAPH_BASIC_EXCEPTION = "CGraph default exception";
static const std::string& CGRAPH_FUNCTION_NO_SUPPORT = "CGraph function no support";
static const std::string& CGRAPH_INPUT_IS_NULL = "input is nullptr";

CGRAPH_NAMESPACE_END

Expand Down
25 changes: 25 additions & 0 deletions src/CBasic/CStruct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: CStruct.h
@Time: 2023/7/16 11:36
@Desc:
***************************/

#ifndef CGRAPH_CSTRUCT_H
#define CGRAPH_CSTRUCT_H

#include "CBasicDefine.h"

CGRAPH_NAMESPACE_BEGIN

/**
* 所有框架内部结构体定义的基类
* 仅针对类似 bean 数据类型的定义
*/
class CStruct {
};

CGRAPH_NAMESPACE_END

#endif //CGRAPH_CSTRUCT_H
Loading

0 comments on commit 4c9a7f1

Please sign in to comment.