1.Improve hal module.
2.Add status code module.
This commit is contained in:
parent
e14eeb7ad8
commit
45a7b9e764
|
@ -76,7 +76,31 @@ libLogAbstract.a -->> -User:return
|
|||
```
|
||||
   构建时把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. 应用代码只使用抽象接口,禁止直接依赖实例代码;
|
||||
2. 应用代码只有在main线程初始化时实例化实例模块即可;
|
||||
|
|
|
@ -7,7 +7,7 @@ include_directories(
|
|||
./abstract
|
||||
./src
|
||||
./include
|
||||
${UTILS_SOURCE_PATH}/ReturnCode/include
|
||||
${UTILS_SOURCE_PATH}/StatusCode/include
|
||||
${UTILS_SOURCE_PATH}/Log/include
|
||||
)
|
||||
#do not rely on any other library
|
||||
|
@ -24,6 +24,6 @@ aux_source_directory(./src IMPL_SRC_FILES)
|
|||
set(ABSTRACT_TARGET HalAbstract)
|
||||
set(IMPL_TARGET Hal)
|
||||
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})
|
||||
target_link_libraries(${IMPL_TARGET} ${ABSTRACT_TARGET} Log)
|
|
@ -2,14 +2,14 @@
|
|||
#include "IHalCpp.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
static RETURN_CODE_C IHalInit(IHal *object)
|
||||
static StatusCode IHalInit(IHal *object)
|
||||
{
|
||||
return IHalCpp::GetInstance()->Init();
|
||||
}
|
||||
static void IHalFree(IHal *object)
|
||||
{
|
||||
}
|
||||
static RETURN_CODE_C IHalUnInit(IHal *object)
|
||||
static StatusCode IHalUnInit(IHal *object)
|
||||
{
|
||||
return IHalCpp::GetInstance()->UnInit();
|
||||
}
|
||||
|
@ -23,14 +23,14 @@ IHal *GetHalIntance(void)
|
|||
{
|
||||
return hal_instance;
|
||||
}
|
||||
RETURN_CODE_C NewIHal(IHal **object)
|
||||
StatusCode NewIHal(IHal **object)
|
||||
{
|
||||
if (!object || !(*object))
|
||||
{
|
||||
return CreateReturnCode(C_RETURN_CODE_NOT_OK);
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
memcpy(*object, &default_hal, sizeof(IHal));
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
void ResetHalImpl(IHal *impl)
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#ifndef I_HAL_H
|
||||
#define I_HAL_H
|
||||
#include "ReturnCode.h"
|
||||
#include "StatusCode.h"
|
||||
/*
|
||||
** Make sure we can call this stuff from C++.
|
||||
*/
|
||||
|
@ -11,23 +11,23 @@ extern "C"
|
|||
typedef struct i_hal IHal;
|
||||
typedef struct i_hal
|
||||
{
|
||||
RETURN_CODE_C (*init)(IHal *);
|
||||
StatusCode (*init)(IHal *);
|
||||
void (*free)(IHal *);
|
||||
RETURN_CODE_C (*un_init)(IHal *);
|
||||
StatusCode (*un_init)(IHal *);
|
||||
} IHal;
|
||||
IHal *GetHalIntance(void);
|
||||
RETURN_CODE_C NewIHal(IHal **object);
|
||||
StatusCode NewIHal(IHal **object);
|
||||
void ResetHalImpl(IHal *impl);
|
||||
static inline RETURN_CODE_C IHalInit(void)
|
||||
static inline StatusCode IHalInit(void)
|
||||
{
|
||||
return GetHalIntance()->init(GetHalIntance());
|
||||
}
|
||||
static inline RETURN_CODE_C IHalUnInit(void)
|
||||
static inline StatusCode IHalUnInit(void)
|
||||
{
|
||||
return GetHalIntance()->un_init(GetHalIntance());
|
||||
}
|
||||
RETURN_CODE_C create_hal_module(void);
|
||||
RETURN_CODE_C destroy_hal_module(void);
|
||||
StatusCode create_hal_module(void);
|
||||
StatusCode destroy_hal_module(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#ifndef IHAL_CPP_H
|
||||
#define IHAL_CPP_H
|
||||
#include "ReturnCode.h"
|
||||
#include "StatusCode.h"
|
||||
#include <memory>
|
||||
class IHalCpp
|
||||
{
|
||||
|
@ -8,7 +8,7 @@ public:
|
|||
IHalCpp() = default;
|
||||
virtual ~IHalCpp() = default;
|
||||
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 RETURN_CODE_C UnInit(void) { return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); }
|
||||
virtual StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
|
||||
virtual StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
|
||||
};
|
||||
#endif
|
|
@ -15,12 +15,12 @@ static void HalImplInit(Hal *hal)
|
|||
LogInfo("HalImplInit\n");
|
||||
((IHal *)hal)->free = HalFree;
|
||||
}
|
||||
RETURN_CODE_C NewHal(Hal **hal)
|
||||
StatusCode NewHal(Hal **hal)
|
||||
{
|
||||
if (!hal)
|
||||
{
|
||||
LogError("C_RETURN_CODE_INVALID_PARAMENTER\n");
|
||||
return CreateReturnCode(C_RETURN_CODE_INVALID_PARAMENTER);
|
||||
LogError("STATUS_CODE_INVALID_PARAMENTER\n");
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
if (!(*hal))
|
||||
{
|
||||
|
@ -30,14 +30,14 @@ RETURN_CODE_C NewHal(Hal **hal)
|
|||
LogInfo("NewHal succeed.\n");
|
||||
NewIHal((IHal **)hal);
|
||||
HalImplInit(*hal);
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
LogError("NewHal failed.\n");
|
||||
return CreateReturnCode(C_RETURN_CODE_NOT_OK);
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
else
|
||||
{
|
||||
HalImplInit(*hal);
|
||||
}
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
#ifndef HAL_H
|
||||
#define HAL_H
|
||||
#include "ReturnCode.h"
|
||||
#include "StatusCode.h"
|
||||
#include "IHal.h"
|
||||
/*
|
||||
** Make sure we can call this stuff from C++.
|
||||
|
@ -14,7 +14,7 @@ extern "C"
|
|||
{
|
||||
IHal base;
|
||||
} Hal;
|
||||
RETURN_CODE_C NewHal(Hal **hal);
|
||||
StatusCode NewHal(Hal **hal);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -6,5 +6,7 @@ class HalCpp : public IHalCpp
|
|||
public:
|
||||
HalCpp() = default;
|
||||
virtual ~HalCpp() = default;
|
||||
StatusCode Init(void);
|
||||
StatusCode UnInit(void);
|
||||
};
|
||||
#endif
|
|
@ -2,22 +2,32 @@
|
|||
#include "Log.h"
|
||||
#include "Hal.h"
|
||||
#include "HalCpp.h"
|
||||
RETURN_CODE_C create_hal_module(void)
|
||||
StatusCode create_hal_module(void)
|
||||
{
|
||||
IHal *hal = NULL;
|
||||
RETURN_CODE_C code = HalMakePtr::GetInstance()->CreateHalPtr(&hal);
|
||||
if (C_RETURN_CODE_OK == code.mCode)
|
||||
StatusCode code = HalMakePtr::GetInstance()->CreateHalPtr(&hal);
|
||||
if (STATUS_CODE_OK == code.mStatusCode)
|
||||
{
|
||||
LogInfo("Create Hal instance ok.\n");
|
||||
ResetHalImpl((IHal *)hal);
|
||||
return code;
|
||||
}
|
||||
return CreateReturnCode(C_RETURN_CODE_NOT_OK);
|
||||
else
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
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;
|
||||
}
|
||||
RETURN_CODE_C destroy_hal_module(void)
|
||||
StatusCode destroy_hal_module(void)
|
||||
{
|
||||
ResetHalImpl(nullptr);
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
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_CODE_C HalMakePtr::CreateHalPtr(IHal **hal)
|
||||
StatusCode HalMakePtr::CreateHalPtr(IHal **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");
|
||||
impl = std::make_shared<HalCpp>();
|
||||
return CreateReturnCode(C_RETURN_CODE_NOT_OK);
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
#define HALMAKEPTR_H
|
||||
#include "IHal.h"
|
||||
#include "IHalCpp.h"
|
||||
#include "ReturnCode.h"
|
||||
#include "StatusCode.h"
|
||||
#include <memory>
|
||||
class HalMakePtr
|
||||
{
|
||||
|
@ -10,9 +10,9 @@ public:
|
|||
HalMakePtr() = default;
|
||||
virtual ~HalMakePtr() = default;
|
||||
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 RETURN_CODE_C UnInit() { return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); }
|
||||
virtual RETURN_CODE_C CreateHalPtr(IHal **hal);
|
||||
virtual RETURN_CODE_C CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl);
|
||||
virtual StatusCode Init() { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
|
||||
virtual StatusCode UnInit() { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
|
||||
virtual StatusCode CreateHalPtr(IHal **hal);
|
||||
virtual StatusCode CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl);
|
||||
};
|
||||
#endif
|
|
@ -2,7 +2,7 @@
|
|||
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
|
||||
|
||||
include_directories(
|
||||
${UTILS_SOURCE_PATH}/ReturnCode/include
|
||||
${UTILS_SOURCE_PATH}/StatusCode/include
|
||||
${UTILS_SOURCE_PATH}/Log/include
|
||||
${HAL_SOURCE_PATH}/include
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
|
||||
|
|
|
@ -10,8 +10,9 @@ namespace IHalTest
|
|||
{
|
||||
InitLog(LOG_EASYLOGGING, nullptr);
|
||||
create_hal_module();
|
||||
i_hal_init();
|
||||
i_hal_un_init();
|
||||
LogInfo("===============\n");
|
||||
IHalInit();
|
||||
IHalUnInit();
|
||||
destroy_hal_module();
|
||||
UnInitLog();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
# cmake_minimum_required(VERSION 2.8.0)
|
||||
add_subdirectory(ReturnCode)
|
||||
add_subdirectory(StatusCode)
|
||||
add_subdirectory(Log)
|
||||
add_subdirectory(LogC)
|
24
utils/StatusCode/CMakeLists.txt
Normal file
24
utils/StatusCode/CMakeLists.txt
Normal 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)
|
||||
|
25
utils/StatusCode/include/StatusCode.h
Normal file
25
utils/StatusCode/include/StatusCode.h
Normal 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
|
35
utils/StatusCode/src/StatusCode.c
Normal file
35
utils/StatusCode/src/StatusCode.c
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user