-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecabf9a
commit b7cf6bd
Showing
6 changed files
with
290 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |