1.Improve hal module.

2.Add status code module.
This commit is contained in:
xiaojiazhu 2023-08-18 08:39:03 -07:00
parent e14eeb7ad8
commit 45a7b9e764
17 changed files with 179 additions and 45 deletions

View File

@ -76,7 +76,31 @@ libLogAbstract.a -->> -User:return
``` ```
   构建时把abstract目录和src目录的源码分别编译成库main线程根据实际需要链接并实例化即可。    构建时把abstract目录和src目录的源码分别编译成库main线程根据实际需要链接并实例化即可。
#### 1.4.1.4. 多态单例总结: #### 1.4.1.4. 混合多态单例
   多态单例的分类有C++版本多态单例C语言版本多态单例混合C/C++多态单例。混合多态单例的提出是由于纯C语言多态单例开发难度较高纯C++语言的多态单例不适合底层特指本文的分层结构中的适配层接口为C++时很多C语言代码调用困难。
   混合多态单例内部多态实现使用C++保留C++开发便捷性和易维护性智能指针对外同时提供C语言多态抽象接口和C++多态抽象接口满足C/C++混编时易用性。最大特点是include文件夹里面会包含两个头文件。
##### 1.4.1.4.1. 多态单例目录结构
```
hal
├── abstract // 抽象接口库的基础代码
│   ├── IHal.cpp
│   └── IHalCpp.cpp
├── include
│   ├── IHalCpp.h // C++抽象接口头文件
│   └── IHal.h // C语言抽象接口头文件
└── src // 抽象接口实例库代码
├── Hal.c // C语言接口实例
├── HalCpp.cpp // C++接口实例
├── HalCpp.h
├── Hal.h
├── HalMakePtr.cpp // 负责创建内部实例
└── HalMakePtr.h
```
#### 1.4.1.5. 多态单例总结:
1. 应用代码只使用抽象接口,禁止直接依赖实例代码; 1. 应用代码只使用抽象接口,禁止直接依赖实例代码;
2. 应用代码只有在main线程初始化时实例化实例模块即可 2. 应用代码只有在main线程初始化时实例化实例模块即可

View File

@ -7,7 +7,7 @@ include_directories(
./abstract ./abstract
./src ./src
./include ./include
${UTILS_SOURCE_PATH}/ReturnCode/include ${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/Log/include
) )
#do not rely on any other library #do not rely on any other library
@ -24,6 +24,6 @@ aux_source_directory(./src IMPL_SRC_FILES)
set(ABSTRACT_TARGET HalAbstract) set(ABSTRACT_TARGET HalAbstract)
set(IMPL_TARGET Hal) set(IMPL_TARGET Hal)
add_library(${ABSTRACT_TARGET} STATIC ${ABSTRACT_SRC_FILES}) add_library(${ABSTRACT_TARGET} STATIC ${ABSTRACT_SRC_FILES})
target_link_libraries(${ABSTRACT_TARGET} ReturnCode Log) target_link_libraries(${ABSTRACT_TARGET} StatusCode Log)
add_library(${IMPL_TARGET} STATIC ${IMPL_SRC_FILES}) add_library(${IMPL_TARGET} STATIC ${IMPL_SRC_FILES})
target_link_libraries(${IMPL_TARGET} ${ABSTRACT_TARGET} Log) target_link_libraries(${IMPL_TARGET} ${ABSTRACT_TARGET} Log)

View File

@ -2,14 +2,14 @@
#include "IHalCpp.h" #include "IHalCpp.h"
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
static RETURN_CODE_C IHalInit(IHal *object) static StatusCode IHalInit(IHal *object)
{ {
return IHalCpp::GetInstance()->Init(); return IHalCpp::GetInstance()->Init();
} }
static void IHalFree(IHal *object) static void IHalFree(IHal *object)
{ {
} }
static RETURN_CODE_C IHalUnInit(IHal *object) static StatusCode IHalUnInit(IHal *object)
{ {
return IHalCpp::GetInstance()->UnInit(); return IHalCpp::GetInstance()->UnInit();
} }
@ -23,14 +23,14 @@ IHal *GetHalIntance(void)
{ {
return hal_instance; return hal_instance;
} }
RETURN_CODE_C NewIHal(IHal **object) StatusCode NewIHal(IHal **object)
{ {
if (!object || !(*object)) if (!object || !(*object))
{ {
return CreateReturnCode(C_RETURN_CODE_NOT_OK); return CreateStatusCode(STATUS_CODE_NOT_OK);
} }
memcpy(*object, &default_hal, sizeof(IHal)); memcpy(*object, &default_hal, sizeof(IHal));
return CreateReturnCode(C_RETURN_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
void ResetHalImpl(IHal *impl) void ResetHalImpl(IHal *impl)
{ {

View File

@ -1,6 +1,6 @@
#ifndef I_HAL_H #ifndef I_HAL_H
#define I_HAL_H #define I_HAL_H
#include "ReturnCode.h" #include "StatusCode.h"
/* /*
** Make sure we can call this stuff from C++. ** Make sure we can call this stuff from C++.
*/ */
@ -11,23 +11,23 @@ extern "C"
typedef struct i_hal IHal; typedef struct i_hal IHal;
typedef struct i_hal typedef struct i_hal
{ {
RETURN_CODE_C (*init)(IHal *); StatusCode (*init)(IHal *);
void (*free)(IHal *); void (*free)(IHal *);
RETURN_CODE_C (*un_init)(IHal *); StatusCode (*un_init)(IHal *);
} IHal; } IHal;
IHal *GetHalIntance(void); IHal *GetHalIntance(void);
RETURN_CODE_C NewIHal(IHal **object); StatusCode NewIHal(IHal **object);
void ResetHalImpl(IHal *impl); void ResetHalImpl(IHal *impl);
static inline RETURN_CODE_C IHalInit(void) static inline StatusCode IHalInit(void)
{ {
return GetHalIntance()->init(GetHalIntance()); return GetHalIntance()->init(GetHalIntance());
} }
static inline RETURN_CODE_C IHalUnInit(void) static inline StatusCode IHalUnInit(void)
{ {
return GetHalIntance()->un_init(GetHalIntance()); return GetHalIntance()->un_init(GetHalIntance());
} }
RETURN_CODE_C create_hal_module(void); StatusCode create_hal_module(void);
RETURN_CODE_C destroy_hal_module(void); StatusCode destroy_hal_module(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -1,6 +1,6 @@
#ifndef IHAL_CPP_H #ifndef IHAL_CPP_H
#define IHAL_CPP_H #define IHAL_CPP_H
#include "ReturnCode.h" #include "StatusCode.h"
#include <memory> #include <memory>
class IHalCpp class IHalCpp
{ {
@ -8,7 +8,7 @@ public:
IHalCpp() = default; IHalCpp() = default;
virtual ~IHalCpp() = default; virtual ~IHalCpp() = default;
static std::shared_ptr<IHalCpp> &GetInstance(std::shared_ptr<IHalCpp> *impl = nullptr); static std::shared_ptr<IHalCpp> &GetInstance(std::shared_ptr<IHalCpp> *impl = nullptr);
virtual RETURN_CODE_C Init(void) { return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); } virtual StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
virtual RETURN_CODE_C UnInit(void) { return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); } virtual StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
}; };
#endif #endif

View File

@ -15,12 +15,12 @@ static void HalImplInit(Hal *hal)
LogInfo("HalImplInit\n"); LogInfo("HalImplInit\n");
((IHal *)hal)->free = HalFree; ((IHal *)hal)->free = HalFree;
} }
RETURN_CODE_C NewHal(Hal **hal) StatusCode NewHal(Hal **hal)
{ {
if (!hal) if (!hal)
{ {
LogError("C_RETURN_CODE_INVALID_PARAMENTER\n"); LogError("STATUS_CODE_INVALID_PARAMENTER\n");
return CreateReturnCode(C_RETURN_CODE_INVALID_PARAMENTER); return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
} }
if (!(*hal)) if (!(*hal))
{ {
@ -30,14 +30,14 @@ RETURN_CODE_C NewHal(Hal **hal)
LogInfo("NewHal succeed.\n"); LogInfo("NewHal succeed.\n");
NewIHal((IHal **)hal); NewIHal((IHal **)hal);
HalImplInit(*hal); HalImplInit(*hal);
return CreateReturnCode(C_RETURN_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
LogError("NewHal failed.\n"); LogError("NewHal failed.\n");
return CreateReturnCode(C_RETURN_CODE_NOT_OK); return CreateStatusCode(STATUS_CODE_NOT_OK);
} }
else else
{ {
HalImplInit(*hal); HalImplInit(*hal);
} }
return CreateReturnCode(C_RETURN_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }

View File

@ -1,6 +1,6 @@
#ifndef HAL_H #ifndef HAL_H
#define HAL_H #define HAL_H
#include "ReturnCode.h" #include "StatusCode.h"
#include "IHal.h" #include "IHal.h"
/* /*
** Make sure we can call this stuff from C++. ** Make sure we can call this stuff from C++.
@ -14,7 +14,7 @@ extern "C"
{ {
IHal base; IHal base;
} Hal; } Hal;
RETURN_CODE_C NewHal(Hal **hal); StatusCode NewHal(Hal **hal);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -0,0 +1,12 @@
#include "HalCpp.h"
#include "Log.h"
StatusCode HalCpp::Init(void)
{
LogInfo("HalCpp::Init\n");
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode HalCpp::UnInit(void)
{
LogInfo("HalCpp::UnInit\n");
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -6,5 +6,7 @@ class HalCpp : public IHalCpp
public: public:
HalCpp() = default; HalCpp() = default;
virtual ~HalCpp() = default; virtual ~HalCpp() = default;
StatusCode Init(void);
StatusCode UnInit(void);
}; };
#endif #endif

View File

@ -2,22 +2,32 @@
#include "Log.h" #include "Log.h"
#include "Hal.h" #include "Hal.h"
#include "HalCpp.h" #include "HalCpp.h"
RETURN_CODE_C create_hal_module(void) StatusCode create_hal_module(void)
{ {
IHal *hal = NULL; IHal *hal = NULL;
RETURN_CODE_C code = HalMakePtr::GetInstance()->CreateHalPtr(&hal); StatusCode code = HalMakePtr::GetInstance()->CreateHalPtr(&hal);
if (C_RETURN_CODE_OK == code.mCode) if (STATUS_CODE_OK == code.mStatusCode)
{ {
LogInfo("Create Hal instance ok.\n"); LogInfo("Create Hal instance ok.\n");
ResetHalImpl((IHal *)hal); ResetHalImpl((IHal *)hal);
return code;
} }
return CreateReturnCode(C_RETURN_CODE_NOT_OK); else
{
return CreateStatusCode(STATUS_CODE_NOT_OK);
} }
RETURN_CODE_C destroy_hal_module(void) auto instance = std::make_shared<IHalCpp>();
StatusCode code2 = HalMakePtr::GetInstance()->CreateHalSharePtr(instance);
if (STATUS_CODE_OK == code2.mStatusCode)
{
LogInfo("IHal manager instance is ok.\n");
IHalCpp::GetInstance(&instance);
}
return code2;
}
StatusCode destroy_hal_module(void)
{ {
ResetHalImpl(nullptr); ResetHalImpl(nullptr);
return CreateReturnCode(C_RETURN_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr> *impl) std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr> *impl)
{ {
@ -28,13 +38,13 @@ std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr>
} }
return instance; return instance;
} }
RETURN_CODE_C HalMakePtr::CreateHalPtr(IHal **hal) StatusCode HalMakePtr::CreateHalPtr(IHal **hal)
{ {
return NewHal((Hal **)hal); return NewHal((Hal **)hal);
} }
RETURN_CODE_C HalMakePtr::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl) StatusCode HalMakePtr::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl)
{ {
LogInfo("HalMakePtr make ptr.\n"); LogInfo("HalMakePtr make ptr.\n");
impl = std::make_shared<HalCpp>(); impl = std::make_shared<HalCpp>();
return CreateReturnCode(C_RETURN_CODE_NOT_OK); return CreateStatusCode(STATUS_CODE_OK);
} }

View File

@ -2,7 +2,7 @@
#define HALMAKEPTR_H #define HALMAKEPTR_H
#include "IHal.h" #include "IHal.h"
#include "IHalCpp.h" #include "IHalCpp.h"
#include "ReturnCode.h" #include "StatusCode.h"
#include <memory> #include <memory>
class HalMakePtr class HalMakePtr
{ {
@ -10,9 +10,9 @@ public:
HalMakePtr() = default; HalMakePtr() = default;
virtual ~HalMakePtr() = default; virtual ~HalMakePtr() = default;
static std::shared_ptr<HalMakePtr> &GetInstance(std::shared_ptr<HalMakePtr> *impl = nullptr); static std::shared_ptr<HalMakePtr> &GetInstance(std::shared_ptr<HalMakePtr> *impl = nullptr);
virtual RETURN_CODE_C Init() { return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); } virtual StatusCode Init() { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
virtual RETURN_CODE_C UnInit() { return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); } virtual StatusCode UnInit() { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
virtual RETURN_CODE_C CreateHalPtr(IHal **hal); virtual StatusCode CreateHalPtr(IHal **hal);
virtual RETURN_CODE_C CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl); virtual StatusCode CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl);
}; };
#endif #endif

View File

@ -2,7 +2,7 @@
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin) set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories( include_directories(
${UTILS_SOURCE_PATH}/ReturnCode/include ${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/Log/include
${HAL_SOURCE_PATH}/include ${HAL_SOURCE_PATH}/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include

View File

@ -10,8 +10,9 @@ namespace IHalTest
{ {
InitLog(LOG_EASYLOGGING, nullptr); InitLog(LOG_EASYLOGGING, nullptr);
create_hal_module(); create_hal_module();
i_hal_init(); LogInfo("===============\n");
i_hal_un_init(); IHalInit();
IHalUnInit();
destroy_hal_module(); destroy_hal_module();
UnInitLog(); UnInitLog();
} }

View File

@ -1,5 +1,6 @@
# cmake_minimum_required(VERSION 2.8.0) # cmake_minimum_required(VERSION 2.8.0)
add_subdirectory(ReturnCode) add_subdirectory(ReturnCode)
add_subdirectory(StatusCode)
add_subdirectory(Log) add_subdirectory(Log)
add_subdirectory(LogC) add_subdirectory(LogC)

View File

@ -0,0 +1,24 @@
include(${CMAKE_SOURCE_DIR}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
include_directories(
./src
./include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
)
#do not rely on any other library
#link_directories(
#)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME StatusCode)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} Log)

View File

@ -0,0 +1,25 @@
#ifndef STATUS_CODE_H
#define STATUS_CODE_H
#ifdef __cplusplus
extern "C"
{
#endif
enum STATUS_CODE
{
STATUS_CODE_OK = 0,
STATUS_CODE_NOT_OK,
STATUS_CODE_VIRTUAL_FUNCTION,
STATUS_CODE_INVALID_PARAMENTER,
STATUS_CODE_END
};
typedef struct status_code StatusCode;
typedef struct status_code
{
const char *(*printStringCode)(const StatusCode);
const long int mStatusCode;
} StatusCode;
const StatusCode CreateStatusCode(const long int code);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,35 @@
#include "StatusCode.h"
#include "Log.h"
#include <stdio.h>
static const char *StatusCodeString[STATUS_CODE_END + 1]={
"STATUS_CODE_OK",
"STATUS_CODE_NOT_OK",
"STATUS_CODE_INVALID_PARAMENTER",
"STATUS_CODE_END"
};
static const char *PrintStringCode(const StatusCode this)
{
if (STATUS_CODE_OK <= this.mStatusCode && this.mStatusCode <= STATUS_CODE_END)
{
LogInfo("Status code = [ %s ]\n", StatusCodeString[this.mStatusCode]);
return StatusCodeString[this.mStatusCode];
}
LogError("Status code = [ %s ]\n", StatusCodeString[STATUS_CODE_INVALID_PARAMENTER]);
return StatusCodeString[STATUS_CODE_INVALID_PARAMENTER];
}
const StatusCode CreateStatusCode(const long int code)
{
if (STATUS_CODE_OK <= code && code <= STATUS_CODE_END)
{
StatusCode result = {NULL, code};
result.printStringCode = PrintStringCode;
return result;
}
else
{
LogError("undefined code.\n");
StatusCode result = {NULL, STATUS_CODE_INVALID_PARAMENTER};
result.printStringCode = PrintStringCode;
return result;
}
}