美文网首页
构建类似std::pair的数据结构

构建类似std::pair的数据结构

作者: FredricZhu | 来源:发表于2022-02-07 16:32 被阅读0次

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

if(APPLE)
    message(STATUS "This is Apple, do nothing.")
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(duo1)

add_definitions(-std=c++17)

add_definitions(-g)

find_package(ZLIB)

find_package(glog REQUIRED)

find_package(OpenCV 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}/../)
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}/../)
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 main_file_list ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) 

file( GLOB APP_SOURCES  ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp ${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})
target_link_libraries(${PROJECT_NAME}_lib ${Boost_LIBRARIES} ZLIB::ZLIB glog::glog DataFrame::DataFrame ${OpenCV_LIBS})
target_link_libraries(${PROJECT_NAME}_lib  ssl crypto libgtest.a pystring libyaml-cpp.a libgmock.a ${ODBC_LIBS} libnanodbc.a pthread dl backtrace)

foreach( main_file ${main_file_list} )
    file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${main_file})
    string(REPLACE ".cpp" "" file ${filename})
    add_executable(${file}  ${main_file})
    target_link_libraries(${file} ${PROJECT_NAME}_lib)
endforeach( main_file ${main_file_list})

duo1.hpp

#ifndef _FREDRIC_DUO1_HPP_
#define _FREDRIC_DUO1_HPP_

template <typename T1, typename T2>
class Duo {
    public:
        using Type1 = T1;
        using Type2 = T2;
        enum { N = 2 };
    
    private:
        T1 value1;
        T2 value2;

    public:
        Duo(): value1(), value2() {}

        Duo(T1 const& a, T2 const& b): value1{a}, value2{b} {}
        
        template <typename U1, typename U2>
        Duo(Duo<U1, U2> const& d): value1{d.v1()}, value2{d.v2()} {}

        template <typename U1, typename U2>
        Duo<T1, T2>& operator=(Duo<U1, U2> const& d) {
            value1 = d.v1();
            value2 = d.v2();
            return *this;
        }

        T1& v1() {
            return value1;
        }

        T1 const& v1() const {
            return value1;
        }

        T2& v2() {
            return value2;
        }

        T2 const& v2() const {
            return value2;
        }
};

template <typename T1, typename T2, 
    typename U1, typename U2>
inline bool operator==(Duo<T1, T2> const& d1, Duo<U1, U2> const& d2) {
    return d1.v1() == d2.v1() && d1.v2() == d2.v2();
}

template <typename T1, typename T2, 
    typename U1, typename U2>
inline bool operator!=(Duo<T1, T2> const& d1, Duo<U1, U2> const& d2) {
    return !(d1==d2);
}

template <typename T1, typename T2>
inline Duo<T1, T2> make_duo(T1 const& a, T2 const& b) {
    return Duo<T1, T2>(a, b);
}

#endif

main.cpp

#include "duo1/duo1.hpp"

#include <iostream>

Duo<float, int> foo() {
    return make_duo(42.0f, 42);
}

int main(int argc, char* argv[]) {
    if(foo() == make_duo(42, 42.0f)) {
        std::cout << "Two duos are equal..." << std::endl;
    } else {
        std::cout << "Two duos are not equal..." << std::endl;
    }
    
    return EXIT_SUCCESS;
}

程序输出如下,


image.png

相关文章

网友评论

      本文标题:构建类似std::pair的数据结构

      本文链接:https://www.haomeiwen.com/subject/wrbmkrtx.html