Skip to content

Commit

Permalink
Generic API interface
Browse files Browse the repository at this point in the history
  • Loading branch information
roigcarlo committed Nov 13, 2015
1 parent cd7325d commit a4d6503
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 42 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ add_library(Core SHARED ${CORE_SOURCES})
add_library(App1 SHARED ${APP1_SOURCES})
add_library(App2 SHARED ${APP2_SOURCES})

set_target_properties(Core PROPERTIES COMPILE_DEFINITIONS "EXPORT_CORE=1")
set_target_properties(App1 PROPERTIES COMPILE_DEFINITIONS "EXPORT_APP1=1")
set_target_properties(App2 PROPERTIES COMPILE_DEFINITIONS "EXPORT_APP2=1")
set_target_properties(Core PROPERTIES COMPILE_DEFINITIONS "CORE=_,_")
set_target_properties(App1 PROPERTIES COMPILE_DEFINITIONS "APP1=_,_")
set_target_properties(App2 PROPERTIES COMPILE_DEFINITIONS "APP2=_,_")

target_link_libraries(App1 Core)
target_link_libraries(App2 App1 Core)
Expand Down
2 changes: 1 addition & 1 deletion app1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Kratos {

KRATOS_CREATE_VARIABLE(int, APP1_INT, 1)
KRATOS_CREATE_VARIABLE(API(APP1), int, APP1_INT, 1)

int app1_function() {
return APP1_INT;
Expand Down
15 changes: 4 additions & 11 deletions app1.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
#include <iostream>
#include "define.hpp"

#ifdef EXPORT_APP1
#undef API
#define API API_EXPORT
#else
#undef API
#define API API_IMPORT
#endif
#include "core.hpp"

namespace Kratos {

KRATOS_DEFINE_VARIABLE(int, APP1_INT)
KRATOS_DEFINE_VARIABLE(API(APP1), int, APP1_INT)

API int app1_function();
API void test_app1();
API(APP1) int app1_function();
API(APP1) void test_app1();
}
2 changes: 1 addition & 1 deletion app2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kratos {

KRATOS_CREATE_VARIABLE(int, APP2_INT, 2)
KRATOS_CREATE_VARIABLE(API(APP2), int, APP2_INT, 2)

int app2_function() {
return APP2_INT;
Expand Down
14 changes: 3 additions & 11 deletions app2.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#include <iostream>
#include "define.hpp"

#ifdef EXPORT_APP2
#undef API
#define API API_EXPORT
#else
#undef API
#define API API_IMPORT
#endif

namespace Kratos {

KRATOS_DEFINE_VARIABLE(int, APP2_INT)
KRATOS_DEFINE_VARIABLE(API(APP2), int, APP2_INT)

API int app2_function();
API void test_app2();
API(APP2) int app2_function();
API(APP2) void test_app2();
}
3 changes: 1 addition & 2 deletions core.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// This file is the main application that uses DLL
#include "core.hpp"

namespace Kratos {

KRATOS_CREATE_VARIABLE(int, CORE_INT, 0)
KRATOS_CREATE_VARIABLE(API(CORE), int, CORE_INT, 0)

int core_function() {
return CORE_INT;
Expand Down
14 changes: 3 additions & 11 deletions core.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#include <iostream>
#include "define.hpp"

#ifdef EXPORT_CORE
#undef API
#define API API_EXPORT
#else
#undef API
#define API API_IMPORT
#endif

namespace Kratos {

KRATOS_DEFINE_VARIABLE(int, CORE_INT)
KRATOS_DEFINE_VARIABLE(API(CORE), int, CORE_INT)

API int core_function();
API void test_core();
API(CORE) int core_function();
API(CORE) void test_core();
}
11 changes: 9 additions & 2 deletions define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
#define API_IMPORT __attribute__((visibility("default")))
#endif

#define KRATOS_DEFINE_VARIABLE(type, name) API extern type name;
// This fixes MSVC not expanding __VA_ARGS__ as defined in the C99 standard
#define EXPAND(A) A

#define KRATOS_CREATE_VARIABLE(type, name, val) API type name = val;
// Random macro magic
#define API_CALL(x,T1,T2,T3,...) T3
#define API(...) EXPAND(API_CALL(,##__VA_ARGS__,API_EXPORT,API_IMPORT))

#define KRATOS_DEFINE_VARIABLE(api, type, name) api extern type name;

#define KRATOS_CREATE_VARIABLE(api, type, name, val) api type name = val;
#define KRATOS_REGISTER_VARIABLE(var) // NOT USED HERE

#endif

0 comments on commit a4d6503

Please sign in to comment.