Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add std any example #7

Merged
merged 2 commits into from
Nov 14, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: C/C++ CI

on:
push:
branches: ["main", "new/*", "refine/", "fix/*"]
branches: ["main"]
pull_request:
branches: ["main"]

Expand All @@ -17,7 +17,7 @@ jobs:
# Private repo checkout | https://github.com/actions/checkout/issues/116#issuecomment-644419389
# The PAT for a user (personal access token) can created in [Github -> settings -> Developer settings -> Personal access tokens]
# For the following "token" entry, it's giving the this repo's workflow via the PAT access to user's other private repos
# One has to set it for this repo in [Repo -> Settings -> Secrets and variables -> Actions]
# You have to set it for this repo in [Repo -> Settings -> Secrets and variables -> Actions]
- name: Checkout repository with submodules
uses: actions/checkout@v3
with:
Expand Down
14 changes: 14 additions & 0 deletions examples/std-any/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Use the folder name as the executable name
get_filename_component(exampleName ${CMAKE_CURRENT_LIST_DIR} NAME)

# Collect source files
file(GLOB exampleSources ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp)

# Add the execuatable
add_executable(${exampleName} main.cpp ${exampleSources})

# Add include folders, applying PRIVATE since they don't have to be exposed for installation
target_include_directories(${exampleName} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/inc)

# Link interal libraries, applying PRIVATE since they don't have to be exposed for installation
target_link_libraries(${exampleName} PRIVATE zj-log-interface)
45 changes: 45 additions & 0 deletions examples/std-any/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "ZjLogMacroExtensions.hpp"
#include <any>
#include "eigen3/Eigen/Dense"
#include <iostream>

void func(const std::any input)
{
try {
auto d = std::any_cast<double>(input);
std::cout << d << std::endl;
} catch (const std::bad_any_cast& e) {
std::cout << "e: " << e.what() << std::endl;
std::cout << input.type().name() << std::endl;
std::cout << input.has_value() << std::endl;
}
}


void func2(std::any input)
{
try {
double* d = std::any_cast<double*>(input);
std::cout << *d << std::endl;

*d = 3.14; // Modifying the referenced std::any
} catch (const std::bad_any_cast& e) {
std::cout << "e: " << e.what() << std::endl;
std::cout << input.type().name() << std::endl;
std::cout << input.has_value() << std::endl;
}
}


int main()
{
// std::string a = "2";
// int a = 123;
double a = 1.23;
func(a);

func2(&a);

std::cout << a << std::endl;
return 0;
}