File tree Expand file tree Collapse file tree 8 files changed +137
-7
lines changed Expand file tree Collapse file tree 8 files changed +137
-7
lines changed Original file line number Diff line number Diff line change @@ -44,3 +44,16 @@ function(list_extract OUTPUT REGEX)
4444 set (${OUTPUT} ${${OUTPUT} } PARENT_SCOPE)
4545
4646endfunction (list_extract)
47+
48+
49+ # Creates an export header similar to generate_export_header, but for templates.
50+ # The main difference is that for MSVC, templates must not get exported.
51+ # When the file ${export_file} is included in source code, the macro ${target_id}_TEMPLATE_API
52+ # may get used to define public visibility for templates on GCC and Clang platforms.
53+ function (generate_template_export_header target target_id export_file)
54+ if ("${CMAKE_CXX_COMPILER_ID} " MATCHES "MSVC" )
55+ configure_file (${PROJECT_SOURCE_DIR} /source /codegeneration/template_msvc_api.h.in ${CMAKE_CURRENT_BINARY_DIR} /${export_file} )
56+ else ()
57+ configure_file (${PROJECT_SOURCE_DIR} /source /codegeneration/template_api.h.in ${CMAKE_CURRENT_BINARY_DIR} /${export_file} )
58+ endif ()
59+ endfunction ()
Original file line number Diff line number Diff line change @@ -19,9 +19,10 @@ message(STATUS "Lib ${target}")
1919# Set API export file and macro
2020string (MAKE_C_IDENTIFIER ${target} target_id)
2121string (TOUPPER ${target_id} target_id)
22- set (feature_file "include/${target} /${target} _features.h" )
23- set (export_file "include/${target} /${target} _api.h" )
24- set (export_macro "${target_id} _API" )
22+ set (feature_file "include/${target} /${target} _features.h" )
23+ set (export_file "include/${target} /${target} _export.h" )
24+ set (template_export_file "include/${target} /${target} _api.h" )
25+ set (export_macro "${target_id} _API" )
2526
2627
2728#
@@ -90,6 +91,10 @@ generate_export_header(${target}
9091 EXPORT_FILE_NAME ${export_file}
9192 EXPORT_MACRO_NAME ${export_macro}
9293)
94+ generate_template_export_header(${target}
95+ ${target_id}
96+ ${template_export_file}
97+ )
9398
9499
95100#
Original file line number Diff line number Diff line change 1+
2+ #ifndef $ {target_id }_TEMPLATE_API_H
3+ #define $ {target_id}_TEMPLATE_API_H
4+
5+ #include <${target}/${target}_export.h>
6+
7+ #ifdef $ {target_id }_STATIC_DEFINE
8+ # define $ {target_id}_TEMPLATE_API
9+ # define $ {target_id}_TEMPLATE_NO_EXPORT
10+ #else
11+ # ifndef $ {target_id }_TEMPLATE_API
12+ # ifdef $ {target }_EXPORTS
13+ /* We are building this library */
14+ # define $ {target_id}_TEMPLATE_API __attribute__((visibility("default")))
15+ # else
16+ /* We are using this library */
17+ # define $ {target_id}_TEMPLATE_API __attribute__((visibility("default")))
18+ # endif
19+ # endif
20+
21+ #endif
22+
23+ #endif
Original file line number Diff line number Diff line change 1+
2+ #ifndef ${target_id}_TEMPLATE_API_H
3+ #define ${target_id}_TEMPLATE_API_H
4+
5+ #include <${target}/${target}_export.h>
6+
7+ #ifdef ${target_id}_STATIC_DEFINE
8+ # define ${target_id}_TEMPLATE_API
9+ # define ${target_id}_TEMPLATE_NO_EXPORT
10+ #else
11+ # ifndef ${target_id}_TEMPLATE_API
12+ # ifdef ${target}_EXPORTS
13+ /* We are building this library */
14+ # define ${target_id}_TEMPLATE_API
15+ # else
16+ /* We are using this library */
17+ # define ${target_id}_TEMPLATE_API
18+ # endif
19+ # endif
20+
21+ #endif
22+
23+ #endif
Original file line number Diff line number Diff line change 33
44#include < baselib/baselib.h>
55
6+ #include < fiblib/CTFibonacci.h>
67#include < fiblib/Fibonacci.h>
78
89
@@ -15,7 +16,8 @@ int main(int /*argc*/, char* /*argv*/[])
1516 // Calculate and print fibonacci number
1617 std::cout << " Fibonacci library" << std::endl;
1718 std::cout << " ========================================" << std::endl;
18- std::cout << " Fibonacci(8) = " << fiblib::Fibonacci ()(8 ) << std::endl;
19+ std::cout << " CTFibonacci(6) = " << fiblib::CTFibonacci<6 >::value << std::endl;
20+ std::cout << " Fibonacci(8) = " << fiblib::Fibonacci ()(8 ) << std::endl;
1921 std::cout << std::endl;
2022
2123 return 0 ;
Original file line number Diff line number Diff line change @@ -19,9 +19,10 @@ message(STATUS "Lib ${target}")
1919# Set API export file and macro
2020string (MAKE_C_IDENTIFIER ${target} target_id)
2121string (TOUPPER ${target_id} target_id)
22- set (feature_file "include/${target} /${target} _features.h" )
23- set (export_file "include/${target} /${target} _api.h" )
24- set (export_macro "${target_id} _API" )
22+ set (feature_file "include/${target} /${target} _features.h" )
23+ set (export_file "include/${target} /${target} _export.h" )
24+ set (template_export_file "include/${target} /${target} _api.h" )
25+ set (export_macro "${target_id} _API" )
2526
2627
2728#
@@ -32,6 +33,8 @@ set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
3233set (source_path "${CMAKE_CURRENT_SOURCE_DIR} /source" )
3334
3435set (headers
36+ ${include_path} /CTFibonacci.h
37+ ${include_path} /CTFibonacci.inl
3538 ${include_path} /Fibonacci.h
3639)
3740
@@ -90,6 +93,10 @@ generate_export_header(${target}
9093 EXPORT_FILE_NAME ${export_file}
9194 EXPORT_MACRO_NAME ${export_macro}
9295)
96+ generate_template_export_header(${target}
97+ ${target_id}
98+ ${template_export_file}
99+ )
93100
94101
95102#
Original file line number Diff line number Diff line change 1+
2+ #pragma once
3+
4+
5+ #include < fiblib/fiblib_api.h>
6+
7+
8+ namespace fiblib
9+ {
10+
11+
12+ /* *
13+ * @brief
14+ * Compile-time computation of fibonacci numbers
15+ */
16+ template <unsigned long long i>
17+ class FIBLIB_TEMPLATE_API CTFibonacci
18+ {
19+ public:
20+ enum {
21+ value = CTFibonacci<i-2 >::value + CTFibonacci<i-1 >::value
22+ };
23+ };
24+
25+
26+ } // namespace fiblib
27+
28+
29+ #include < fiblib/CTFibonacci.inl>
Original file line number Diff line number Diff line change 1+
2+ #pragma once
3+
4+
5+ namespace fiblib
6+ {
7+
8+
9+ template <>
10+ class FIBLIB_TEMPLATE_API CTFibonacci<0 >
11+ {
12+ public:
13+ enum {
14+ value = 0
15+ };
16+ };
17+
18+ template <>
19+ class FIBLIB_TEMPLATE_API CTFibonacci<1 >
20+ {
21+ public:
22+ enum {
23+ value = 1
24+ };
25+ };
26+
27+
28+ } // namespace fiblib
You can’t perform that action at this time.
0 commit comments