Skip to content

Commit

Permalink
-wip2
Browse files Browse the repository at this point in the history
  • Loading branch information
kamchatka-volcano committed Jan 16, 2024
1 parent ecabf9a commit b7cf6bd
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 1 deletion.
24 changes: 23 additions & 1 deletion examples_static_refl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
40 changes: 40 additions & 0 deletions examples_static_refl/ex01_static_refl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "figcone/config.h"
#include "figcone/configreader.h"
#include <filesystem>
#include <iostream>
#include <vector>

struct ThumbnailCfg {
int maxWidth;
int maxHeight;
};

struct PhotoViewerCfg {
std::filesystem::path rootDir;
std::vector<std::string> supportedFiles;
ThumbnailCfg thumbnailSettings;
};

int main()
{
auto cfgReader = figcone::ConfigReader{};
auto cfg = cfgReader.readToml<PhotoViewerCfg>(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;
}
42 changes: 42 additions & 0 deletions examples_static_refl/ex02_static_refl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <figcone/config.h>
#include <figcone/configreader.h>
#include <filesystem>
#include <iostream>
#include <vector>

struct ThumbnailCfg
{
int maxWidth;
int maxHeight;
};

struct PhotoViewerCfg
{
std::filesystem::path rootDir;
std::vector<std::string> supportedFiles;
ThumbnailCfg thumbnailSettings;
};

int main()
{
auto cfgReader = figcone::ConfigReader{figcone::NameFormat::SnakeCase};
auto cfg = cfgReader.readToml<PhotoViewerCfg>(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;
}
73 changes: 73 additions & 0 deletions examples_static_refl/ex03_static_refl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <figcone/config.h>
#include <figcone/configreader.h>
#include <figcone/shortmacros.h> //enables macros without FIGCONE_ prefix
#include <filesystem>
#include <iostream>
#include <map>
#include <vector>

struct Host{
std::string ip;
int port;
};

namespace figcone{
template<>
struct StringConverter<Host>{
static std::optional<Host> 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<Host> hosts;
};
struct PhotoViewerCfg : public figcone::Config{
std::filesystem::path rootDir;
std::vector<std::string> supportedFiles;
std::vector<SharedAlbumCfg> sharedAlbums;
std::map<std::string, std::string> envVars;

using traits = figcone::FieldTraits<figcone::OptionalField<&PhotoViewerCfg::envVars>>;
};

int main()
{
auto cfgReader = figcone::ConfigReader{};
auto cfg = cfgReader.readYaml<PhotoViewerCfg>(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;
}
59 changes: 59 additions & 0 deletions examples_static_refl/ex04_static_refl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <figcone/config.h>
#include <figcone/configreader.h>
#include <figcone/shortmacros.h>
#include <filesystem>
#include <iostream>
#include <map>
#include <vector>

struct NotEmpty {
template<typename TList>
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<std::string> supportedFiles;
std::map<std::string, std::string> 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<PhotoViewerCfg>(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;
}
}
53 changes: 53 additions & 0 deletions examples_static_refl/ex05_static_refl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <figcone/config.h>
#include <figcone/configreader.h>
#include <algorithm>
#include <filesystem>
#include <iostream>
#include <vector>


struct ThumbnailCfg {
int maxWidth;
int maxHeight;
};

struct PhotoViewerCfg : public figcone::Config {
std::filesystem::path rootDir;
std::vector<std::string> supportedFiles;
ThumbnailCfg thumbnailSettings;
};

namespace figcone {
template<>
void PostProcessor<PhotoViewerCfg>::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<PhotoViewerCfg>(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;
}

0 comments on commit b7cf6bd

Please sign in to comment.