Skip to content

Commit

Permalink
Sbromberger/fix examples (#36)
Browse files Browse the repository at this point in the history
* fixing examples so that they build.
  • Loading branch information
sbromberger authored Sep 20, 2024
1 parent 7f3daea commit 98fd7b6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cmake_minimum_required(VERSION 3.14)
set(ALLOW_DUPLICATE_CUSTOM_TARGETS TRUE)

project(CLIPPy
VERSION 0.1
VERSION 0.2
DESCRIPTION "Command Line Interface Plus Python"
LANGUAGES CXX)

Expand Down
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
30 changes: 29 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
#
# This function adds an mpi example.
#
# Works with 3.11 and tested through 3.15 (not tested yet)
cmake_minimum_required(VERSION 3.14)
set(ALLOW_DUPLICATE_CUSTOM_TARGETS TRUE)

include(FetchContent)

find_package(Threads)
# PP: added because I got an error "Could NOT find Threads (missing: Threads_FOUND)"
# solution according to: https://github.com/alicevision/geogram/issues/2
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
Expand All @@ -15,17 +21,39 @@ set(CMAKE_USE_WIN32_THREADS_INIT 0)
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)

find_package(Boost 1.75 REQUIRED COMPONENTS)

#
# Metall
find_package(Metall QUIET)
if (NOT Metall_FOUND)
#set(METALL_WORK_DIR ${CMAKE_CURRENT_BINARY_DIR}/metall-work)
set(METALL_SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/metall-src)
set(METALL_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/metall-build)
set(METALL_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/metall-install)
FetchContent_Declare(Metall
GIT_REPOSITORY https://github.com/LLNL/metall.git
GIT_TAG v0.26
)
# SOURCE_DIR ${METALL_SOURCE_DIR}
# BINARY_DIR ${METALL_BUILD_DIR}
# CMAKE_ARGS -DINSTALL_HEADER_ONLY=ON -DCMAKE_INSTALL_PREFIX=${METALL_INSTALL_DIR}
# )
# set(METALL_INCLUDE_DIR ${METALL_INSTALL_DIR}/include)
FetchContent_MakeAvailable(Metall)
endif ()
function ( add_example example_name )
set(example_source "${example_name}.cpp")
set(example_exe "${example_name}")
add_executable(${example_exe} ${example_source})
target_include_directories(${example_exe} PRIVATE ${Boost_INCLUDE_DIRS})
target_include_directories(${example_exe} PRIVATE ${Boost_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/../include)
target_link_libraries(${example_exe} PRIVATE Metall)
target_link_libraries(${example_exe} PRIVATE stdc++fs Threads::Threads)
if (UNIX AND NOT APPLE)
target_link_libraries(${example_exe} PRIVATE rt)
endif ()
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set(SOURCES ${example_source} ${CMAKE_SOURCE_DIR}/../include/clippy/clippy.hpp} ${CMAKE_SOURCE_DIR}/../include/experimental/cxx-compat.hpp)
endfunction()

add_example(grumpy)
Expand Down
1 change: 1 addition & 0 deletions examples/dataframe/experimental
10 changes: 4 additions & 6 deletions examples/grumpy.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// Copyright 2019 Lawrence Livermore National Security, LLC and other Clippy Project Developers.
// See the top-level COPYRIGHT file for details.
// Copyright 2019 Lawrence Livermore National Security, LLC and other Clippy
// Project Developers. See the top-level COPYRIGHT file for details.
//
// SPDX-License-Identifier: MIT

#include <clippy/clippy.hpp>

int main(int argc, char **argv)
{
int main(int argc, char **argv) {
clippy::clippy clip("grumpy", "Always throws errors because he's Grumpy!");
if (clip.parse(argc, argv))
{
if (clip.parse(argc, argv)) {
return 0;
}

Expand Down
66 changes: 31 additions & 35 deletions examples/oo-howdy/howdy-getitem.cpp
Original file line number Diff line number Diff line change
@@ -1,68 +1,64 @@
// Copyright 2021 Lawrence Livermore National Security, LLC and other CLIPPy Project Developers.
// See the top-level COPYRIGHT file for details.
// Copyright 2021 Lawrence Livermore National Security, LLC and other CLIPPy
// Project Developers. See the top-level COPYRIGHT file for details.
//
// SPDX-License-Identifier: MIT

#include <iostream>
#include <boost/json.hpp>
#include "clippy/clippy.hpp"

#include <boost/json.hpp>
#include <iostream>

namespace boostjsn = boost::json;

static const std::string className = "Greeter";
static const std::string methodName = "__getitem__";
static const std::string expr = "expressions";
static const std::string selLetters = "letters";
static const std::string selFieldAll = "all";
static const std::string selFieldVowels = "vowels";
static const std::string selFieldConsonants = "consonants";
static const std::string selLetters = "letters";
static const std::string selFieldAll = "all";
static const std::string selFieldVowels = "vowels";
static const std::string selFieldConsonants = "consonants";
static const std::string stGreeting = "greeting";
static const std::string stGreeted = "greeted";
static const std::string stGreeted = "greeted";
static const std::string stSelected = "selected";

int main(int argc, char** argv)
{
int error_code = 0;
int main(int argc, char **argv) {
int error_code = 0;
clippy::clippy clip{methodName, "Selector example"};

clip.member_of(className, "Customizable Greeting Generator");

clip.add_required<std::vector<boost::json::object>>(expr, "Expression selection");
clip.add_selector<std::string>(selLetters, "selector for letters of the greeting");

clip.add_required<std::vector<boost::json::object>>(expr,
"Expression selection");
clip.update_selectors({{selLetters, "selector for letters of the greeting"}});
//~ clip.returns<std::vector<std::string>>("Expression result");

if (clip.parse(argc, argv)) { return 0; }


if (clip.parse(argc, argv)) {
return 0;
}

// the real thing
try
{
try {
using JsonExpression = std::vector<boost::json::object>;
std::string greeting = clip.get_state<std::string>(stGreeting);
std::string greeted = clip.get_state<std::string>(stGreeted);

std::string greeting = clip.get_state<std::string>(stGreeting);
std::string greeted = clip.get_state<std::string>(stGreeted);
JsonExpression jsonExpression = clip.get<JsonExpression>(expr);
clippy::object res;
clippy::object clippy_type;
clippy::object state;

state.set_val(stGreeting, std::move(greeting));
state.set_val(stGreeted, std::move(greeted));
state.set_val(stGreeted, std::move(greeted));
state.set_val(stSelected, std::move(jsonExpression));

clippy_type.set_val("__class__", className);
clippy_type.set_json("state", std::move(state));
res.set_json("__clippy_type__", std::move(clippy_type));
clippy_type.set_json("state", std::move(state));

res.set_json("__clippy_type__", std::move(clippy_type));
clip.to_return(std::move(res));
}
catch (const std::exception& err)
{
} catch (const std::exception &err) {
clip.to_return(err.what());
error_code = 1;
}

return error_code;
}


0 comments on commit 98fd7b6

Please sign in to comment.