Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cmake/Custom.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ function(list_extract OUTPUT REGEX)
set(${OUTPUT} ${${OUTPUT}} PARENT_SCOPE)

endfunction(list_extract)


# Creates an export header similar to generate_export_header, but for templates.
# The main difference is that for MSVC, templates must not get exported.
# When the file ${export_file} is included in source code, the macro ${target_id}_TEMPLATE_API
# may get used to define public visibility for templates on GCC and Clang platforms.
function(generate_template_export_header target target_id export_file)
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
configure_file(${PROJECT_SOURCE_DIR}/source/codegeneration/template_msvc_api.h.in ${CMAKE_CURRENT_BINARY_DIR}/${export_file})
else()
configure_file(${PROJECT_SOURCE_DIR}/source/codegeneration/template_api.h.in ${CMAKE_CURRENT_BINARY_DIR}/${export_file})
endif()
endfunction()
11 changes: 8 additions & 3 deletions source/baselib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ message(STATUS "Lib ${target}")
# Set API export file and macro
string(MAKE_C_IDENTIFIER ${target} target_id)
string(TOUPPER ${target_id} target_id)
set(feature_file "include/${target}/${target}_features.h")
set(export_file "include/${target}/${target}_api.h")
set(export_macro "${target_id}_API")
set(feature_file "include/${target}/${target}_features.h")
set(export_file "include/${target}/${target}_export.h")
set(template_export_file "include/${target}/${target}_api.h")
set(export_macro "${target_id}_API")


#
Expand Down Expand Up @@ -90,6 +91,10 @@ generate_export_header(${target}
EXPORT_FILE_NAME ${export_file}
EXPORT_MACRO_NAME ${export_macro}
)
generate_template_export_header(${target}
${target_id}
${template_export_file}
)


#
Expand Down
22 changes: 22 additions & 0 deletions source/codegeneration/template_api.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#ifndef ${target_id}_TEMPLATE_API_H
#define ${target_id}_TEMPLATE_API_H

#include <${target}/${target}_export.h>

#ifdef ${target_id}_STATIC_DEFINE
# define ${target_id}_TEMPLATE_API
#else
# ifndef ${target_id}_TEMPLATE_API
# ifdef ${target}_EXPORTS
/* We are building this library */
# define ${target_id}_TEMPLATE_API __attribute__((visibility("default")))
# else
/* We are using this library */
# define ${target_id}_TEMPLATE_API __attribute__((visibility("default")))
# endif
# endif

#endif

#endif
22 changes: 22 additions & 0 deletions source/codegeneration/template_msvc_api.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#ifndef ${target_id}_TEMPLATE_API_H
#define ${target_id}_TEMPLATE_API_H

#include <${target}/${target}_export.h>

#ifdef ${target_id}_STATIC_DEFINE
# define ${target_id}_TEMPLATE_API
#else
# ifndef ${target_id}_TEMPLATE_API
# ifdef ${target}_EXPORTS
/* We are building this library */
# define ${target_id}_TEMPLATE_API
# else
/* We are using this library */
# define ${target_id}_TEMPLATE_API
# endif
# endif

#endif

#endif
4 changes: 3 additions & 1 deletion source/examples/fibcmd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <baselib/baselib.h>

#include <fiblib/CTFibonacci.h>
#include <fiblib/Fibonacci.h>


Expand All @@ -15,7 +16,8 @@ int main(int /*argc*/, char* /*argv*/[])
// Calculate and print fibonacci number
std::cout << "Fibonacci library" << std::endl;
std::cout << "========================================" << std::endl;
std::cout << "Fibonacci(8) = " << fiblib::Fibonacci()(8) << std::endl;
std::cout << "CTFibonacci(6) = " << fiblib::CTFibonacci<6>::value << std::endl;
std::cout << "Fibonacci(8) = " << fiblib::Fibonacci()(8) << std::endl;
std::cout << std::endl;

return 0;
Expand Down
13 changes: 10 additions & 3 deletions source/fiblib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ message(STATUS "Lib ${target}")
# Set API export file and macro
string(MAKE_C_IDENTIFIER ${target} target_id)
string(TOUPPER ${target_id} target_id)
set(feature_file "include/${target}/${target}_features.h")
set(export_file "include/${target}/${target}_api.h")
set(export_macro "${target_id}_API")
set(feature_file "include/${target}/${target}_features.h")
set(export_file "include/${target}/${target}_export.h")
set(template_export_file "include/${target}/${target}_api.h")
set(export_macro "${target_id}_API")


#
Expand All @@ -32,6 +33,8 @@ set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")

set(headers
${include_path}/CTFibonacci.h
${include_path}/CTFibonacci.inl
${include_path}/Fibonacci.h
)

Expand Down Expand Up @@ -90,6 +93,10 @@ generate_export_header(${target}
EXPORT_FILE_NAME ${export_file}
EXPORT_MACRO_NAME ${export_macro}
)
generate_template_export_header(${target}
${target_id}
${template_export_file}
)


#
Expand Down
29 changes: 29 additions & 0 deletions source/fiblib/include/fiblib/CTFibonacci.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

#pragma once


#include <fiblib/fiblib_api.h>


namespace fiblib
{


/**
* @brief
* Compile-time computation of fibonacci numbers
*/
template <unsigned long long i>
class FIBLIB_TEMPLATE_API CTFibonacci
{
public:
enum {
value = CTFibonacci<i-2>::value + CTFibonacci<i-1>::value
};
};


} // namespace fiblib


#include <fiblib/CTFibonacci.inl>
28 changes: 28 additions & 0 deletions source/fiblib/include/fiblib/CTFibonacci.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#pragma once


namespace fiblib
{


template <>
class FIBLIB_TEMPLATE_API CTFibonacci<0>
{
public:
enum {
value = 0
};
};

template <>
class FIBLIB_TEMPLATE_API CTFibonacci<1>
{
public:
enum {
value = 1
};
};


} // namespace fiblib