Skip to content

Commit 2d7df33

Browse files
-wip2
1 parent ecabf9a commit 2d7df33

6 files changed

+289
-1
lines changed

examples_static_refl/CMakeLists.txt

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.18)
22
project(figcone_examples_static_refl)
33

4-
file(GLOB SRC_FILES "../examples/*.cpp")
4+
file(GLOB SRC_FILES "../examples/demo_*.cpp")
55
foreach(SRC_FILE ${SRC_FILES})
66
SealLake_StringAfterLast(${SRC_FILE} "/" TEST_NAME)
77
SealLake_StringBeforeLast(${TEST_NAME} "." TEST_NAME)
@@ -16,4 +16,26 @@ foreach(SRC_FILE ${SRC_FILES})
1616
figcone::figcone
1717
)
1818
target_compile_definitions(${TEST_NAME}_static_refl PUBLIC "FIGCONE_EXAMPLE_STATIC_REFLECTION")
19+
endforeach()
20+
21+
set(EXAMPLE_SRC
22+
ex01_static_refl.cpp
23+
ex02_static_refl.cpp
24+
ex03_static_refl.cpp
25+
ex04_static_refl.cpp
26+
ex05_static_refl.cpp
27+
)
28+
foreach(SRC_FILE ${EXAMPLE_SRC})
29+
SealLake_StringAfterLast(${SRC_FILE} "/" TEST_NAME)
30+
SealLake_StringBeforeLast(${TEST_NAME} "." TEST_NAME)
31+
32+
SealLake_Executable(
33+
NAME ${TEST_NAME}
34+
SOURCES ${SRC_FILE}
35+
COMPILE_FEATURES cxx_std_20
36+
PROPERTIES
37+
CXX_EXTENSIONS OFF
38+
LIBRARIES
39+
figcone::figcone
40+
)
1941
endforeach()
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "figcone/config.h"
2+
#include "figcone/configreader.h"
3+
#include <filesystem>
4+
#include <iostream>
5+
#include <vector>
6+
7+
struct ThumbnailCfg {
8+
int maxWidth;
9+
int maxHeight;
10+
};
11+
12+
struct PhotoViewerCfg {
13+
std::filesystem::path rootDir;
14+
std::vector<std::string> supportedFiles;
15+
ThumbnailCfg thumbnailSettings;
16+
};
17+
18+
int main()
19+
{
20+
auto cfgReader = figcone::ConfigReader{};
21+
auto cfg = cfgReader.readToml<PhotoViewerCfg>(R"(
22+
rootDir = "~/Photos"
23+
supportedFiles = [".jpg", ".png"]
24+
[thumbnailSettings]
25+
maxWidth = 256
26+
maxHeight = 256
27+
)");
28+
//At this point your config is ready to use
29+
std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl;
30+
31+
if (!cfg.supportedFiles.empty())
32+
std::cout << "Supported files:" << std::endl;
33+
for (const auto& file : cfg.supportedFiles)
34+
std::cout << " " << file << std::endl;
35+
36+
std::cout << "Thumbnail settings:" << std::endl;
37+
std::cout << " Max width:" << cfg.thumbnailSettings.maxWidth << std::endl;
38+
std::cout << " Max height:" << cfg.thumbnailSettings.maxHeight << std::endl;
39+
return 0;
40+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <figcone/config.h>
2+
#include <figcone/configreader.h>
3+
#include <filesystem>
4+
#include <iostream>
5+
#include <vector>
6+
7+
struct ThumbnailCfg {
8+
int maxWidth;
9+
int maxHeight;
10+
};
11+
12+
struct PhotoViewerCfg {
13+
std::filesystem::path rootDir;
14+
std::vector<std::string> supportedFiles;
15+
ThumbnailCfg thumbnailSettings;
16+
};
17+
18+
int main()
19+
{
20+
auto cfgReader = figcone::ConfigReader{figcone::NameFormat::SnakeCase};
21+
auto cfg = cfgReader.readToml<PhotoViewerCfg>(R"(
22+
root_dir = "/home/kamchatka-volcano/photos"
23+
supported_files = [".jpg", ".png"]
24+
[thumbnail_settings]
25+
max_width = 256
26+
max_height = 256
27+
)");
28+
//At this point your config is ready to use
29+
std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl;
30+
31+
if (!cfg.supportedFiles.empty())
32+
std::cout << "Supported files:" << std::endl;
33+
for (const auto& file : cfg.supportedFiles)
34+
std::cout << " " << file << std::endl;
35+
36+
std::cout << "Thumbnail settings:" << std::endl;
37+
std::cout << " Max width:" << cfg.thumbnailSettings.maxWidth << std::endl;
38+
std::cout << " Max height:" << cfg.thumbnailSettings.maxHeight << std::endl;
39+
return 0;
40+
}
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <figcone/config.h>
2+
#include <figcone/configreader.h>
3+
#include <figcone/shortmacros.h> //enables macros without FIGCONE_ prefix
4+
#include <filesystem>
5+
#include <iostream>
6+
#include <map>
7+
#include <vector>
8+
9+
struct Host {
10+
std::string ip;
11+
int port;
12+
};
13+
14+
namespace figcone {
15+
template<>
16+
struct StringConverter<Host> {
17+
static std::optional<Host> fromString(const std::string& data)
18+
{
19+
auto delimPos = data.find(':');
20+
if (delimPos == std::string::npos)
21+
return {};
22+
auto host = Host{};
23+
host.ip = data.substr(0, delimPos);
24+
host.port = std::stoi(data.substr(delimPos + 1, data.size() - delimPos - 1));
25+
return host;
26+
}
27+
};
28+
} //namespace figcone
29+
30+
struct SharedAlbumCfg {
31+
std::filesystem::path dir;
32+
std::string name;
33+
std::vector<Host> hosts;
34+
};
35+
struct PhotoViewerCfg {
36+
std::filesystem::path rootDir;
37+
std::vector<std::string> supportedFiles;
38+
std::vector<SharedAlbumCfg> sharedAlbums;
39+
std::map<std::string, std::string> envVars;
40+
41+
using traits = figcone::FieldTraits<figcone::OptionalField<&PhotoViewerCfg::envVars>>;
42+
};
43+
44+
int main()
45+
{
46+
auto cfgReader = figcone::ConfigReader{};
47+
auto cfg = cfgReader.readYaml<PhotoViewerCfg>(R"(
48+
rootDir: ~/Photos
49+
supportedFiles: [ ".jpg", "png"]
50+
sharedAlbums:
51+
-
52+
dir: "summer_2019"
53+
name: "Summer 2019"
54+
hosts: ["127.0.0.1:8080"]
55+
)");
56+
57+
std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl;
58+
59+
if (!cfg.supportedFiles.empty())
60+
std::cout << "Supported files:" << std::endl;
61+
for (const auto& file : cfg.supportedFiles)
62+
std::cout << " " << file << std::endl;
63+
64+
if (!cfg.sharedAlbums.empty())
65+
std::cout << "Shared albums:" << std::endl;
66+
for (const auto& album : cfg.sharedAlbums) {
67+
std::cout << " Album:" << album.name << std::endl;
68+
std::cout << " Hosts:" << std::endl;
69+
for (const auto& host : album.hosts)
70+
std::cout << " " << host.ip << ":" << host.port << std::endl;
71+
}
72+
73+
return 0;
74+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <figcone/config.h>
2+
#include <figcone/configreader.h>
3+
#include <figcone/shortmacros.h>
4+
#include <filesystem>
5+
#include <iostream>
6+
#include <map>
7+
#include <vector>
8+
9+
struct NotEmpty {
10+
template<typename TList>
11+
void operator()(const TList& list)
12+
{
13+
if (!list.empty())
14+
throw figcone::ValidationError{"can't be empty."};
15+
}
16+
};
17+
18+
struct PathExists {
19+
void operator()(const std::filesystem::path& path)
20+
{
21+
if (!std::filesystem::exists(path))
22+
throw figcone::ValidationError{"a path must exist"};
23+
}
24+
};
25+
26+
struct PhotoViewerCfg {
27+
std::filesystem::path rootDir;
28+
std::vector<std::string> supportedFiles;
29+
std::map<std::string, std::string> envVars;
30+
31+
using traits = figcone::FieldTraits<
32+
figcone::ValidatedField<&PhotoViewerCfg::rootDir, PathExists>,
33+
figcone::ValidatedField<&PhotoViewerCfg::supportedFiles, NotEmpty>,
34+
figcone::OptionalField<&PhotoViewerCfg::envVars>>;
35+
};
36+
37+
int main()
38+
{
39+
try {
40+
auto cfgReader = figcone::ConfigReader{};
41+
auto cfg = cfgReader.readYaml<PhotoViewerCfg>(R"(
42+
rootDir: ~/Photos
43+
supportedFiles: []
44+
)");
45+
46+
std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl;
47+
48+
if (!cfg.supportedFiles.empty())
49+
std::cout << "Supported files:" << std::endl;
50+
for (const auto& file : cfg.supportedFiles)
51+
std::cout << " " << file << std::endl;
52+
53+
return 0;
54+
}
55+
catch (const figcone::ConfigError& e) {
56+
std::cout << "Config error:" << e.what();
57+
return 1;
58+
}
59+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <figcone/config.h>
2+
#include <figcone/configreader.h>
3+
#include <algorithm>
4+
#include <filesystem>
5+
#include <iostream>
6+
#include <vector>
7+
8+
9+
struct ThumbnailCfg {
10+
int maxWidth;
11+
int maxHeight;
12+
};
13+
14+
struct PhotoViewerCfg {
15+
std::filesystem::path rootDir;
16+
std::vector<std::string> supportedFiles;
17+
ThumbnailCfg thumbnailSettings;
18+
};
19+
20+
namespace figcone {
21+
template<>
22+
void PostProcessor<PhotoViewerCfg>::operator()(PhotoViewerCfg& cfg)
23+
{
24+
auto supportPng = std::find(cfg.supportedFiles.begin(), cfg.supportedFiles.end(), ".png") !=
25+
cfg.supportedFiles.end();
26+
if (supportPng && cfg.thumbnailSettings.maxWidth > 128)
27+
throw ValidationError{"thumbnail width can't be larger than 128px when png images are present"};
28+
}
29+
} //namespace figcone
30+
31+
int main()
32+
{
33+
auto cfgReader = figcone::ConfigReader{};
34+
auto cfg = cfgReader.readToml<PhotoViewerCfg>(R"(
35+
rootDir = "~/Photos"
36+
supportedFiles = [".jpg", ".png"]
37+
[thumbnailSettings]
38+
maxWidth = 256
39+
maxHeight = 256
40+
)");
41+
//At this point your config is ready to use
42+
std::cout << "Launching PhotoViewer in directory " << cfg.rootDir << std::endl;
43+
44+
if (!cfg.supportedFiles.empty())
45+
std::cout << "Supported files:" << std::endl;
46+
for (const auto& file : cfg.supportedFiles)
47+
std::cout << " " << file << std::endl;
48+
49+
std::cout << "Thumbnail settings:" << std::endl;
50+
std::cout << " Max width:" << cfg.thumbnailSettings.maxWidth << std::endl;
51+
std::cout << " Max height:" << cfg.thumbnailSettings.maxHeight << std::endl;
52+
return 0;
53+
}

0 commit comments

Comments
 (0)