Skip to content

Commit

Permalink
add an example
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchabannes committed Feb 7, 2024
1 parent 8184b08 commit b6d4f76
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(NAPP_CMAKE_PROJECT_TARGETS_FILE "${NAPP_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Tar

option(NAPP_ENABLE_TESTS "Build the unit tests when NAPP_ENABLE_TESTS is enabled." ${MAIN_PROJECT})
option(NAPP_ENABLE_DOC "Build the doc examples tests when NAPP_ENABLE_DOC is enabled." ${MAIN_PROJECT})
option(NAPP_ENABLE_EXAMPLES "Build examples when NAPP_ENABLE_EXAMPLES is enabled." ${MAIN_PROJECT})

# option(NAPP_MultipleHeaders "Use non-amalgamated version of the library." OFF)
# if (NAPP_MultipleHeaders)
Expand Down Expand Up @@ -66,11 +67,16 @@ if (NAPP_ENABLE_TESTS)
endif()


if (NAPP_ENABLE_EXAMPLES)
add_subdirectory(examples)
endif()

if (NAPP_ENABLE_DOC)
add_subdirectory(doc)
endif()



install(
DIRECTORY ${NAPP_INCLUDE_BUILD_DIR}
DESTINATION ${NAPP_INCLUDE_INSTALL_DIR}
Expand Down
7 changes: 7 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file(GLOB files ex*.cpp)
foreach(file ${files})
get_filename_component(file_basename ${file} NAME_WE)
set(testcase napp_example_${file_basename})
add_executable(${testcase} ${file})
target_link_libraries(${testcase} PRIVATE ${NAPP_TARGET_NAME})
endforeach()
122 changes: 122 additions & 0 deletions examples/ex1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <napp/na.hpp>
#include <tuple>
#include <iostream>

constexpr auto& _parameters = NA::identifier<struct parameters_tag>;
constexpr auto& _task = NA::identifier<struct task_tag>;

// namespace na {
// using parameters = typename std::decay_t<decltype(_parameters)>::identifier_type;
// using task = typename std::decay_t<decltype(_task)>::identifier_type;
// }

namespace Backend{

template<typename ...T, size_t... I>
auto extractParametersAsTuple( std::tuple<T...> && t, std::index_sequence<I...>)
{
return std::forward_as_tuple( std::get<I>(t).getValue()...);
}

struct Runtime{
template <typename ... Ts>
void task(Ts && ... ts ) {
auto t = std::make_tuple( std::forward<Ts>(ts)... );
auto callback = std::get<sizeof...(Ts) - 1>(t);
auto parameters = extractParametersAsTuple( std::move(t), std::make_index_sequence<sizeof...(Ts)-1>{} );
std::apply( callback, std::move(parameters) );
}
};

template <typename T,bool b>
class SpData
{
static_assert(std::is_reference<T>::value,
"The given type must be a reference");
public:
using value_type = T;
static constexpr bool isWrite = b;

template <typename U, typename = std::enable_if_t<std::is_convertible_v<U,T>> >
constexpr explicit SpData( U && u ) : M_val( std::forward<U>(u) ) {}

constexpr value_type getValue() { return M_val; }
private:
value_type M_val;
};

template <typename T>
auto spRead( T && t )
{
return SpData<T,false>{ std::forward<T>( t ) };
}
template <typename T>
auto spWrite( T && t )
{
return SpData<T,true>{ std::forward<T>( t ) };
}

template<typename T>
auto toSpData( T && t )
{
if constexpr ( std::is_const_v<std::remove_reference_t<T>> )
return spRead( std::forward<T>( t ) );
else
return spWrite( std::forward<T>( t ) );
}

template<typename ...T, size_t... I>
auto makeSpDataHelper( std::tuple<T...>& t, std::index_sequence<I...>)
{
return std::make_tuple( toSpData(std::get<I>(t))...);
}
template<typename ...T>
auto makeSpData( std::tuple<T...>& t ){
return makeSpDataHelper<T...>(t, std::make_index_sequence<sizeof...(T)>{});
}
}

namespace pm
{
template <typename ... Ts>
void
runTask( Ts && ... ts )
{
auto args = NA::make_arguments( std::forward<Ts>(ts)... );
auto && task = args.get(_task);
auto && parameters = args.get_else(_parameters,std::make_tuple());

Backend::Runtime runtime;
std::apply( [&runtime](auto... args){ runtime.task(args...); }, std::tuple_cat( Backend::makeSpData( parameters ), std::make_tuple( task ) ) );
}

template <typename ... Ts>
auto parameters(Ts && ... ts)
{
return std::forward_as_tuple( std::forward<Ts>(ts)... );
}
}

int main()
{
const int i1=4;
double d1=2.34;

// use variables state (if non const -> SpWrite else SpRead).
pm::runTask( _parameters=pm::parameters(i1,d1),
_task=[](int p1,double & p2){
p2 *= p1;
} );
std::cout << "d1="<< d1 << std::endl;

// use std::ref/std::cref (if non const -> SpWrite else SpRead).
int i2 = 123;
std::string s2 = "string value : ";
pm::runTask( _parameters=pm::parameters(std::cref(i2),std::ref(s2)),
_task=[](int p1,std::string & p2){
p2 += std::to_string(p1);
} );
std::cout << "s2="<< s2 << std::endl;

return 0;
}

0 comments on commit b6d4f76

Please sign in to comment.