diff --git a/examples_static_refl/CMakeLists.txt b/examples_static_refl/CMakeLists.txt index ca40a64..5b4b83a 100644 --- a/examples_static_refl/CMakeLists.txt +++ b/examples_static_refl/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.18) project(figcone_examples_static_refl) -file(GLOB SRC_FILES "../examples/*.cpp") +file(GLOB SRC_FILES "../examples/demo_*.cpp") foreach(SRC_FILE ${SRC_FILES}) SealLake_StringAfterLast(${SRC_FILE} "/" TEST_NAME) SealLake_StringBeforeLast(${TEST_NAME} "." TEST_NAME) @@ -16,4 +16,26 @@ foreach(SRC_FILE ${SRC_FILES}) figcone::figcone ) target_compile_definitions(${TEST_NAME}_static_refl PUBLIC "FIGCONE_EXAMPLE_STATIC_REFLECTION") +endforeach() + +set(EXAMPLE_SRC + ex01_static_refl.cpp + ex02_static_refl.cpp + ex03_static_refl.cpp + ex04_static_refl.cpp + ex05_static_refl.cpp +) +foreach(SRC_FILE ${EXAMPLE_SRC}) + SealLake_StringAfterLast(${SRC_FILE} "/" TEST_NAME) + SealLake_StringBeforeLast(${TEST_NAME} "." TEST_NAME) + + SealLake_Executable( + NAME ${TEST_NAME} + SOURCES ${SRC_FILE} + COMPILE_FEATURES cxx_std_20 + PROPERTIES + CXX_EXTENSIONS OFF + LIBRARIES + figcone::figcone + ) endforeach() \ No newline at end of file diff --git a/examples_static_refl/ex01_static_refl.cpp b/examples_static_refl/ex01_static_refl.cpp new file mode 100644 index 0000000..346a0ac --- /dev/null +++ b/examples_static_refl/ex01_static_refl.cpp @@ -0,0 +1,40 @@ +#include "figcone/config.h" +#include "figcone/configreader.h" +#include +#include +#include + +struct ThumbnailCfg { + int maxWidth; + int maxHeight; +}; + +struct PhotoViewerCfg { + std::filesystem::path rootDir; + std::vector supportedFiles; + ThumbnailCfg thumbnailSettings; +}; + +int main() +{ + auto cfgReader = figcone::ConfigReader{}; + auto cfg = cfgReader.readToml(R"( + rootDir = "~/Photos" + supportedFiles = [".jpg", ".png"] + [thumbnailSettings] + maxWidth = 256 + maxHeight = 256 + )"); + //At this point your config is ready to use + std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl; + + if (!cfg.supportedFiles.empty()) + std::cout << "Supported files:" << std::endl; + for (const auto& file : cfg.supportedFiles) + std::cout << " " << file << std::endl; + + std::cout << "Thumbnail settings:" << std::endl; + std::cout << " Max width:" << cfg.thumbnailSettings.maxWidth << std::endl; + std::cout << " Max height:" << cfg.thumbnailSettings.maxHeight << std::endl; + return 0; +} \ No newline at end of file diff --git a/examples_static_refl/ex02_static_refl.cpp b/examples_static_refl/ex02_static_refl.cpp new file mode 100644 index 0000000..3d54003 --- /dev/null +++ b/examples_static_refl/ex02_static_refl.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include + +struct ThumbnailCfg +{ + int maxWidth; + int maxHeight; +}; + +struct PhotoViewerCfg +{ + std::filesystem::path rootDir; + std::vector supportedFiles; + ThumbnailCfg thumbnailSettings; +}; + +int main() +{ + auto cfgReader = figcone::ConfigReader{figcone::NameFormat::SnakeCase}; + auto cfg = cfgReader.readToml(R"( + root_dir = "/home/kamchatka-volcano/photos" + supported_files = [".jpg", ".png"] + [thumbnail_settings] + max_width = 256 + max_height = 256 + )"); + //At this point your config is ready to use + std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl; + + if (!cfg.supportedFiles.empty()) + std::cout << "Supported files:" << std::endl; + for (const auto& file : cfg.supportedFiles) + std::cout << " " << file << std::endl; + + std::cout << "Thumbnail settings:" << std::endl; + std::cout << " Max width:" << cfg.thumbnailSettings.maxWidth << std::endl; + std::cout << " Max height:" << cfg.thumbnailSettings.maxHeight << std::endl; + return 0; +} \ No newline at end of file diff --git a/examples_static_refl/ex03_static_refl.cpp b/examples_static_refl/ex03_static_refl.cpp new file mode 100644 index 0000000..8a57ecf --- /dev/null +++ b/examples_static_refl/ex03_static_refl.cpp @@ -0,0 +1,73 @@ +#include +#include +#include //enables macros without FIGCONE_ prefix +#include +#include +#include +#include + +struct Host{ + std::string ip; + int port; +}; + +namespace figcone{ +template<> +struct StringConverter{ + static std::optional fromString(const std::string& data) + { + auto delimPos = data.find(':'); + if (delimPos == std::string::npos) + return {}; + auto host = Host{}; + host.ip = data.substr(0, delimPos); + host.port = std::stoi(data.substr(delimPos + 1, data.size() - delimPos - 1)); + return host; + } +}; +} +struct SharedAlbumCfg{ + std::filesystem::path dir; + std::string name; + std::vector hosts; +}; +struct PhotoViewerCfg : public figcone::Config{ + std::filesystem::path rootDir; + std::vector supportedFiles; + std::vector sharedAlbums; + std::map envVars; + + using traits = figcone::FieldTraits>; +}; + +int main() +{ + auto cfgReader = figcone::ConfigReader{}; + auto cfg = cfgReader.readYaml(R"( + rootDir: ~/Photos + supportedFiles: [ ".jpg", "png"] + sharedAlbums: + - + dir: "summer_2019" + name: "Summer 2019" + hosts: ["127.0.0.1:8080"] + )"); + + std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl; + + if (!cfg.supportedFiles.empty()) + std::cout << "Supported files:" << std::endl; + for (const auto& file : cfg.supportedFiles) + std::cout << " " << file << std::endl; + + if (!cfg.sharedAlbums.empty()) + std::cout << "Shared albums:" << std::endl; + for (const auto& album : cfg.sharedAlbums){ + std::cout << " Album:" << album.name << std::endl; + std::cout << " Hosts:" << std::endl; + for (const auto& host : album.hosts) + std::cout << " " << host.ip << ":" << host.port << std::endl; + } + + return 0; +} diff --git a/examples_static_refl/ex04_static_refl.cpp b/examples_static_refl/ex04_static_refl.cpp new file mode 100644 index 0000000..8dd5af6 --- /dev/null +++ b/examples_static_refl/ex04_static_refl.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include + +struct NotEmpty { + template + void operator()(const TList& list) + { + if (!list.empty()) + throw figcone::ValidationError{"can't be empty."}; + } +}; + +struct PathExists { + void operator()(const std::filesystem::path& path) + { + if (!std::filesystem::exists(path)) + throw figcone::ValidationError{"a path must exist"}; + } +}; + +struct PhotoViewerCfg { + std::filesystem::path rootDir; + std::vector supportedFiles; + std::map envVars; + + using traits = figcone::FieldTraits< + figcone::ValidatedField<&PhotoViewerCfg::rootDir, PathExists>, + figcone::ValidatedField<&PhotoViewerCfg::supportedFiles, NotEmpty>, + figcone::OptionalField<&PhotoViewerCfg::envVars>>; +}; + +int main() +{ + try { + auto cfgReader = figcone::ConfigReader{}; + auto cfg = cfgReader.readYaml(R"( + rootDir: ~/Photos + supportedFiles: [] + )"); + + std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl; + + if (!cfg.supportedFiles.empty()) + std::cout << "Supported files:" << std::endl; + for (const auto& file : cfg.supportedFiles) + std::cout << " " << file << std::endl; + + return 0; + } + catch (const figcone::ConfigError& e) { + std::cout << "Config error:" << e.what(); + return 1; + } +} \ No newline at end of file diff --git a/examples_static_refl/ex05_static_refl.cpp b/examples_static_refl/ex05_static_refl.cpp new file mode 100644 index 0000000..161b720 --- /dev/null +++ b/examples_static_refl/ex05_static_refl.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include + + +struct ThumbnailCfg { + int maxWidth; + int maxHeight; +}; + +struct PhotoViewerCfg : public figcone::Config { + std::filesystem::path rootDir; + std::vector supportedFiles; + ThumbnailCfg thumbnailSettings; +}; + +namespace figcone { +template<> +void PostProcessor::operator()(PhotoViewerCfg& cfg) +{ + auto supportPng = std::find(cfg.supportedFiles.begin(), cfg.supportedFiles.end(), ".png") != + cfg.supportedFiles.end(); + if (supportPng && cfg.thumbnailSettings.maxWidth > 128) + throw ValidationError{"thumbnail width can't be larger than 128px when png images are present"}; +} +} //namespace figcone + +int main() +{ + auto cfgReader = figcone::ConfigReader{}; + auto cfg = cfgReader.readToml(R"( + rootDir = "~/Photos" + supportedFiles = [".jpg", ".png"] + [thumbnailSettings] + maxWidth = 256 + maxHeight = 256 + )"); + //At this point your config is ready to use + std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl; + + if (!cfg.supportedFiles.empty()) + std::cout << "Supported files:" << std::endl; + for (const auto& file : cfg.supportedFiles) + std::cout << " " << file << std::endl; + + std::cout << "Thumbnail settings:" << std::endl; + std::cout << " Max width:" << cfg.thumbnailSettings.maxWidth << std::endl; + std::cout << " Max height:" << cfg.thumbnailSettings.maxHeight << std::endl; + return 0; +} \ No newline at end of file