Skip to content

Commit 08e7d58

Browse files
committed
add cpp generator for xthreejs autogenerates classes
1 parent 4901f85 commit 08e7d58

8 files changed

+958
-9
lines changed

js/scripts/generate-wrappers.js

+415-6
Large diffs are not rendered by default.

js/scripts/prop-types.js

+193-3
Large diffs are not rendered by default.
+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
project(xthreejs)
3+
4+
message(STATUS "Forcing tests build type to Release")
5+
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
6+
7+
set(XTHREEJS_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
8+
set(XTHREEJS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
9+
10+
# Configuration
11+
# =============
12+
13+
include(GNUInstallDirs)
14+
include(CMakePackageConfigHelpers)
15+
16+
set(XTHREEJS_INSTALL_LIBRARY_DIR "\"${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}\"")
17+
18+
configure_file (
19+
"${XTHREEJS_INCLUDE_DIR}/xthreejs/xthreejs_config.hpp.in"
20+
"${XTHREEJS_INCLUDE_DIR}/xthreejs/xthreejs_config.hpp"
21+
)
22+
23+
# Versionning
24+
# ===========
25+
26+
file(STRINGS "${XTHREEJS_INCLUDE_DIR}/xthreejs/xthreejs_config.hpp" xthreejs_version_defines
27+
REGEX "#define XTHREEJS_VERSION_(MAJOR|MINOR|PATCH)")
28+
foreach(ver ${xthreejs_version_defines})
29+
if(ver MATCHES "#define XTHREEJS_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$")
30+
set(XTHREEJS_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
31+
endif()
32+
endforeach()
33+
set(XTHREEJS_VERSION
34+
${XTHREEJS_VERSION_MAJOR}.${XTHREEJS_VERSION_MINOR}.${XTHREEJS_VERSION_PATCH})
35+
message(STATUS "xthreejs version: v${XTHREEJS_VERSION}")
36+
37+
# Binary version
38+
# See the following URL for explanations about the binary versionning
39+
# https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html#Updating-version-info
40+
file(STRINGS "${XTHREEJS_INCLUDE_DIR}/xthreejs/xthreejs_config.hpp" xthreejs_version_defines
41+
REGEX "#define XTHREEJS_BINARY_(CURRENT|REVISION|AGE)")
42+
foreach(ver ${xthreejs_version_defines})
43+
if(ver MATCHES "#define XTHREEJS_BINARY_(CURRENT|REVISION|AGE) +([^ ]+)$")
44+
set(XTHREEJS_BINARY_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
45+
endif()
46+
endforeach()
47+
set(XTHREEJS_BINARY_VERSION
48+
${XTHREEJS_BINARY_CURRENT}.${XTHREEJS_BINARY_REVISION}.${XTHREEJS_BINARY_AGE})
49+
message(STATUS "xthreejs binary version: v${XTHREEJS_BINARY_VERSION}")
50+
51+
# Dependencies
52+
# ============
53+
54+
find_package(cppzmq 4.2.3 REQUIRED)
55+
find_package(xtl 0.4 REQUIRED)
56+
find_package(xeus 0.10.1 REQUIRED)
57+
find_package(xwidgets 0.7 REQUIRED)
58+
find_package(xproperty 0.7 REQUIRED)
59+
60+
# Source files
61+
# ============
62+
63+
set(XTHREEJS_HEADERS
64+
{{#each hppfiles as |file|}}
65+
${XTHREEJS_INCLUDE_DIR}/xthreejs/{{{ file }}}
66+
{{/each}}
67+
${XTHREEJS_INCLUDE_DIR}/xthreejs/xthreejs_config.hpp
68+
)
69+
70+
set(XTHREEJS_SOURCES
71+
{{#each cppfiles as |file|}}
72+
${XTHREEJS_SOURCE_DIR}/{{{ file }}}
73+
{{/each}}
74+
)
75+
76+
# Output
77+
# ======
78+
79+
OPTION(XTHREEJS_PRECOMPILED "precompile xthreejs instances" ON)
80+
if(XTHREEJS_PRECOMPILED)
81+
add_definitions(-DXTHREEJS_PRECOMPILED)
82+
endif()
83+
84+
add_library(xthreejs SHARED ${XTHREEJS_SOURCES} ${XTHREEJS_HEADERS})
85+
86+
target_include_directories(xthreejs PUBLIC $<BUILD_INTERFACE:${XTHREEJS_INCLUDE_DIR}>
87+
$<INSTALL_INTERFACE:include>)
88+
89+
target_link_libraries(xthreejs
90+
PUBLIC xtl
91+
PUBLIC xwidgets
92+
PRIVATE xeus)
93+
94+
set_target_properties(xthreejs PROPERTIES
95+
PUBLIC_HEADER "${XTHREEJS_HEADERS}"
96+
COMPILE_DEFINITIONS "XTHREEJS_EXPORTS"
97+
PREFIX ""
98+
VERSION ${XTHREEJS_BINARY_VERSION}
99+
SOVERSION ${XTHREEJS_BINARY_CURRENT}
100+
OUTPUT_NAME "libxthreejs")
101+
102+
# Compilation flags
103+
# =================
104+
105+
include(CheckCXXCompilerFlag)
106+
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
107+
OPTION(DISABLE_ARCH_NATIVE "disable -march=native flag" OFF)
108+
109+
set_target_properties(xthreejs PROPERTIES
110+
CXX_EXTENSIONS OFF
111+
CXX_STANDARD_REQUIRED 14)
112+
113+
target_compile_features(xthreejs PRIVATE cxx_std_14)
114+
115+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
116+
target_compile_options(xthreejs PUBLIC -Wunused-parameter -Wextra -Wreorder)
117+
if (DISABLE_ARCH_NATIVE)
118+
target_compile_options(xthreejs PUBLIC -mtune=generic)
119+
else()
120+
target_compile_options(xthreejs PUBLIC -march=native)
121+
endif()
122+
123+
# Enable link time optimization and set the default symbol
124+
# visibility to hidden (very important to obtain small binaries)
125+
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
126+
# Check for Link Time Optimization support
127+
# (GCC/Clang)
128+
CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
129+
if (HAS_LTO_FLAG)
130+
target_compile_options(xthreejs PUBLIC -flto)
131+
endif()
132+
133+
# Intel equivalent to LTO is called IPO
134+
if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
135+
CHECK_CXX_COMPILER_FLAG("-ipo" HAS_IPO_FLAG)
136+
if (HAS_IPO_FLAG)
137+
target_compile_options(xthreejs PUBLIC -ipo)
138+
endif()
139+
endif()
140+
endif()
141+
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
142+
endif()
143+
144+
# Installation
145+
# ============
146+
147+
set(XTHREEJS_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" CACHE STRING "install path for xthreejsConfig.cmake")
148+
149+
install(DIRECTORY ${XTHREEJS_INCLUDE_DIR}/xthreejs DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
150+
151+
install(TARGETS xthreejs
152+
EXPORT ${PROJECT_NAME}-targets
153+
#PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xthreejs
154+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
155+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
156+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
157+
158+
# Makes the project importable from the build directory
159+
export(EXPORT ${PROJECT_NAME}-targets
160+
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
161+
162+
configure_package_config_file(${PROJECT_NAME}Config.cmake.in
163+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
164+
INSTALL_DESTINATION ${XTHREEJS_CMAKECONFIG_INSTALL_DIR})
165+
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
166+
VERSION ${XTHREEJS_VERSION}
167+
COMPATIBILITY AnyNewerVersion)
168+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
169+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
170+
DESTINATION ${XTHREEJS_CMAKECONFIG_INSTALL_DIR})
171+
install(EXPORT ${PROJECT_NAME}-targets
172+
FILE ${PROJECT_NAME}Targets.cmake
173+
DESTINATION ${XTHREEJS_CMAKECONFIG_INSTALL_DIR})
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#ifndef {{ header }}
2+
#define {{ header }}
3+
4+
#include "xtl/xoptional.hpp"
5+
#include "xwidgets/xeither.hpp"
6+
#include "xwidgets/xwidget.hpp"
7+
8+
#include "{{ cpp_base_relative_path }}/base/xenums.hpp"
9+
#include "{{ cpp_base_relative_path }}/base/xthree_types.hpp"
10+
#include "{{ superClass.cppRelativePath }}.hpp"
11+
#include "{{ cpp_base_relative_path }}/base/xrender.hpp"
12+
13+
namespace xthree
14+
{
15+
//
16+
// {{ className }} declaration
17+
//
18+
19+
template<class D>
20+
class {{ xclassName }} : public {{ superClass.xclassName }}<D>
21+
{
22+
public:
23+
24+
using base_type = {{ superClass.xclassName }}<D>;
25+
using derived_type = D;
26+
27+
void serialize_state(xeus::xjson&, xeus::buffer_sequence&) const;
28+
void apply_patch(const xeus::xjson&, const xeus::buffer_sequence&);
29+
30+
{{#each properties as |prop propName|}}
31+
{{{ prop.xproperty }}}
32+
{{/each}}
33+
34+
{{#if hasBuffer}}
35+
const std::vector<xw::xjson_path_type>& buffer_paths() const;
36+
{{/if}}
37+
38+
std::shared_ptr<xw::xmaterialize<xpreview>> pre = nullptr;
39+
40+
protected:
41+
42+
{{ xclassName }}();
43+
using base_type::base_type;
44+
45+
private:
46+
47+
void set_defaults();
48+
};
49+
50+
using {{ className }} = xw::xmaterialize<{{ xclassName }}>;
51+
52+
using {{ className }}_generator = xw::xgenerator<{{ xclassName }}>;
53+
54+
//
55+
// {{ className }} implementation
56+
//
57+
58+
{{#if hasBuffer}}
59+
template <class D>
60+
inline const std::vector<xw::xjson_path_type>& {{ xclassName }}<D>::buffer_paths() const
61+
{
62+
static const std::vector<xw::xjson_path_type> default_buffer_paths = {
63+
{{#each properties as |prop propName|}}
64+
{{#if prop.isBinaryBuffer}}
65+
{ "{{ propName }}", "buffer" },
66+
{{/if}}
67+
{{/each}}
68+
};
69+
return default_buffer_paths;
70+
}
71+
{{/if}}
72+
73+
template <class D>
74+
inline void {{ xclassName }}<D>::serialize_state(xeus::xjson& state, xeus::buffer_sequence& buffers) const
75+
{
76+
base_type::serialize_state(state, buffers);
77+
78+
{{#each properties as |prop propName|}}
79+
{{#if prop.xproperty}}
80+
xw::set_patch_from_property({{ propName }}, state, buffers);
81+
{{/if}}
82+
{{/each}}
83+
}
84+
85+
template <class D>
86+
inline void {{ xclassName }}<D>::apply_patch(const xeus::xjson& patch, const xeus::buffer_sequence& buffers)
87+
{
88+
base_type::apply_patch(patch, buffers);
89+
90+
{{#each properties as |prop propName|}}
91+
{{#if prop.xproperty}}
92+
xw::set_property_from_patch({{ propName }}, patch, buffers);
93+
{{/if}}
94+
{{/each}}
95+
}
96+
97+
template <class D>
98+
inline {{ xclassName }}<D>::{{ xclassName }}()
99+
: base_type()
100+
{
101+
set_defaults();
102+
}
103+
104+
template <class D>
105+
inline void {{ xclassName }}<D>::set_defaults()
106+
{
107+
this->_model_name() = "{{ modelName }}";
108+
this->_view_name() = "";
109+
}
110+
}
111+
112+
{{#unless hasOverride}}
113+
xeus::xjson mime_bundle_repr(xw::xmaterialize<xthree::{{ xclassName }}>& widget);
114+
{{/unless}}
115+
116+
/*********************
117+
* precompiled types *
118+
*********************/
119+
120+
#ifdef XTHREEJS_PRECOMPILED
121+
#ifndef _WIN32
122+
extern template class xw::xmaterialize<xthree::{{ xclassName }}>;
123+
extern template xw::xmaterialize<xthree::{{ xclassName }}>::xmaterialize();
124+
extern template class xw::xtransport<xw::xmaterialize<xthree::{{ xclassName }}>>;
125+
extern template class xw::xgenerator<xthree::{{ xclassName }}>;
126+
extern template xw::xgenerator<xthree::{{ xclassName }}>::xgenerator();
127+
extern template class xw::xtransport<xw::xgenerator<xthree::{{ xclassName }}>>;
128+
#endif
129+
#endif
130+
131+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef XTHREE_{{ header }}_DIR_HPP
2+
#define XTHREE_{{ header }}_DIR_HPP
3+
4+
{{#each hppfiles as |hppfile|}}
5+
#include "{{ hppfile }}"
6+
{{/each}}
7+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{{#unless hasOverride}}
2+
#include "xthreejs/{{ hppfile }}"
3+
4+
xeus::xjson mime_bundle_repr(xw::xmaterialize<xthree::{{ xclassName }}>& widget)
5+
{
6+
if (not widget.pre)
7+
widget.pre = std::make_shared<xthree::preview>(xthree::preview(widget));
8+
return mime_bundle_repr(*widget.pre);
9+
}
10+
11+
#ifdef XTHREEJS_PRECOMPILED
12+
template class xw::xmaterialize<xthree::{{ xclassName }}>;
13+
template xw::xmaterialize<xthree::{{ xclassName }}>::xmaterialize();
14+
template class xw::xtransport<xw::xmaterialize<xthree::{{ xclassName }}>>;
15+
template class xw::xgenerator<xthree::{{ xclassName }}>;
16+
template xw::xgenerator<xthree::{{ xclassName }}>::xgenerator();
17+
template class xw::xtransport<xw::xgenerator<xthree::{{ xclassName }}>>;
18+
#endif
19+
{{/unless}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef XTHREE_XTHREE_HPP
2+
#define XTHREE_XTHREE_HPP
3+
4+
{{#each files as |file|}}
5+
#include "x{{ file }}.hpp"
6+
{{/each}}
7+
#endif

xthreejs/xthreejsConfig.cmake.in

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# xthreejs cmake module
2+
# This module sets the following variables in your project::
3+
#
4+
# xthreejs_FOUND - true if xthreejs found on the system
5+
# xthreejs_INCLUDE_DIRS - the directory containing xthreejs headers
6+
# xthreejs_LIBRARY - empty
7+
8+
@PACKAGE_INIT@
9+
10+
set(PN xthreejs)
11+
set_and_check(${PN}_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@")
12+
set(${PN}_LIBRARY "")
13+
check_required_components(${PN})

0 commit comments

Comments
 (0)