本例和动态的策略模式一样。也是高层依赖于统一的策略接口,底层可以有不同的实现。但是使用模板选择不同的策略实现。[就是编译期静态选择实现] 程序代码结构如下, 代码如下, test/CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
if(APPLE)
message(STATUS "This is Apple, do nothing.")
set(CMAKE_MACOSX_RPATH 1)
set(CMAKE_PREFIX_PATH /Users/aabjfzhu/software/vcpkg/ports/cppwork/vcpkg_installed/x64-osx/share )
elseif(UNIX)
message(STATUS "This is linux, set CMAKE_PREFIX_PATH.")
set(CMAKE_PREFIX_PATH /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/share)
endif(APPLE)
project(static_strategy)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-narrowing")
add_definitions(-g)
find_package(ZLIB)
find_package(OpenCV REQUIRED )
find_package(Arrow CONFIG REQUIRED)
find_package(unofficial-brotli REQUIRED)
find_package(unofficial-utf8proc CONFIG REQUIRED)
find_package(Thrift CONFIG REQUIRED)
find_package(glog REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost REQUIRED COMPONENTS
system
filesystem
serialization
program_options
thread
)
find_package(DataFrame REQUIRED)
if(APPLE)
MESSAGE(STATUS "This is APPLE, set INCLUDE_DIRS")
set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include /usr/local/iODBC/include /opt/snowflake/snowflakeodbc/include/ ${CMAKE_CURRENT_SOURCE_DIR}/../include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../../include)
elseif(UNIX)
MESSAGE(STATUS "This is linux, set INCLUDE_DIRS")
set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include ${CMAKE_CURRENT_SOURCE_DIR}/../include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/)
endif(APPLE)
if(APPLE)
MESSAGE(STATUS "This is APPLE, set LINK_DIRS")
set(LINK_DIRS /usr/local/lib /usr/local/iODBC/lib /opt/snowflake/snowflakeodbc/lib/universal)
elseif(UNIX)
MESSAGE(STATUS "This is linux, set LINK_DIRS")
set(LINK_DIRS ${Boost_INCLUDE_DIRS} /usr/local/lib /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/lib)
endif(APPLE)
if(APPLE)
MESSAGE(STATUS "This is APPLE, set ODBC_LIBS")
set(ODBC_LIBS iodbc iodbcinst)
elseif(UNIX)
MESSAGE(STATUS "This is linux, set LINK_DIRS")
set(ODBC_LIBS odbc odbcinst ltdl)
endif(APPLE)
include_directories(${INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRS})
file( GLOB test_file_list ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
file( GLOB APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../include/*.h ${CMAKE_CURRENT_SOURCE_DIR}/../include/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/arr_/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/http/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/yaml/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/df/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/death_handler/impl/*.cpp)
add_library(${PROJECT_NAME}_lib SHARED ${APP_SOURCES} ${test_file})
target_link_libraries(${PROJECT_NAME}_lib ${Boost_LIBRARIES} ZLIB::ZLIB glog::glog DataFrame::DataFrame ${OpenCV_LIBS})
target_link_libraries(${PROJECT_NAME}_lib OpenSSL::SSL OpenSSL::Crypto libgtest.a pystring libyaml-cpp.a libgmock.a ${ODBC_LIBS} libnanodbc.a pthread dl backtrace libzstd.a libbz2.a libsnappy.a re2::re2 parquet lz4 unofficial::brotli::brotlidec-static unofficial::brotli::brotlienc-static unofficial::brotli::brotlicommon-static utf8proc thrift::thrift arrow arrow_dataset)
foreach( test_file ${test_file_list} )
file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${test_file})
string(REPLACE ".cpp" "" file ${filename})
add_executable(${file} ${test_file})
target_link_libraries(${file} ${PROJECT_NAME}_lib)
endforeach( test_file ${test_file_list})
test/static_strategy_test.cpp
#include "static_strategy.hpp"
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <fstream>
#include <memory>
#include <algorithm>
#include "death_handler/death_handler.h"
int main(int argc, char** argv) {
FLAGS_log_dir = "./";
FLAGS_alsologtostderr = true;
// 日志级别 INFO, WARNING, ERROR, FATAL 的值分别为0、1、2、3
FLAGS_minloglevel = 0;
Debug::DeathHandler dh;
google::InitGoogleLogging("./logs.log");
testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
return ret;
}
GTEST_TEST(StaticStrategyTests, StaticStrategy) {
// markdown
TextProcessor<MarkdownListStrategy> tp;
tp.append_list({"foo", "bar", "baz"});
std::cout << tp.str() << std::endl;
// html
TextProcessor<HtmlListStrategy> hp;
hp.append_list({"foo", "bar", "baz"});
std::cout << hp.str() << std::endl;
}
include/static_strategy.hpp
#ifndef _FREDRIC_STATIC_STRATEGY_HPP_
#define _FREDRIC_STATIC_STRATEGY_HPP_
#include <iostream>
#include <string>
#include <sstream>
#include <memory>
#include <vector>
#include <concepts>
enum class OutputFormat {
Markdown,
Html
};
template <typename T>
concept ListStrategyType = requires (T value) {
{value.add_list_item(std::declval<std::ostringstream&>(), std::declval<std::string const&>())};
{value.start(std::declval<std::ostringstream&>())};
{value.end(std::declval<std::ostringstream&>())};
};
struct MarkdownListStrategy {
void start(std::ostringstream& oss) {
}
void end(std::ostringstream& oss) {
}
void add_list_item(std::ostringstream& oss, std::string const& item) {
oss << " * " << item << std::endl;
}
};
struct HtmlListStrategy {
void start(std::ostringstream& oss) {
oss << "<ul>" << std::endl;
}
void end(std::ostringstream& oss) {
oss << "</ul>" << std::endl;
}
void add_list_item(std::ostringstream& oss, std::string const& item) {
oss << "<li>" << item << "</li>" << std::endl;
}
};
template <ListStrategyType LS>
struct TextProcessor {
void clear() {
oss.str("");
oss.clear();
}
void append_list(std::vector<std::string> const& items) {
list_strategy.start(oss);
for(auto& item: items) {
list_strategy.add_list_item(oss, item);
}
list_strategy.end(oss);
}
std::string str() const {
return oss.str();
}
private:
std::ostringstream oss;
LS list_strategy;
};
#endif
程序输出如下,
|