User Guide¶
Welcome to the dross User Guide. This guide provides in-depth information about using the dross library effectively in your C++ projects.
Overview¶
The dross library is designed around several core principles:
Zero Dependencies: Only requires the standard C++ library
Value Semantics: The value types are copyable and assignable;
environmentis the exception, exposing only static membersError Handling: Errors are returned, not thrown -
std::optionalandstd::expected- apart from the bounds-checked accessors, thepathcalls that letstd::filesystemexceptions through and thetimestamparithmetic operators, which throw outside the years 0 to 9999Modern C++: Leverages C++23 features throughout
Pimpl Idiom: Types keep their representation behind an opaque pointer, apart from
error,path,xdg,timestamp,timezoneand thedataiterators, which hold theirs directly; the ABI can change in any 0.x release
Topics Covered¶
Type System Guide¶
Learn how to effectively use the dross type system:
Working with dynamic values
Type conversions and checking
Building complex data structures
Performance considerations
Error Handling¶
Understanding dross’s approach to error handling:
Using
std::optionalfor nullable valuesWorking with
std::expectedfor error propagationCreating and handling error types
Best practices for robust code
Platform Utilities¶
Cross-platform utilities for system interaction:
Environment variable management
Filesystem path operations
XDG Base Directory support
Platform-specific considerations
Best Practices¶
Guidelines for writing efficient and maintainable code:
Design patterns with dross
Performance optimization tips
Memory management strategies
Common pitfalls to avoid
Quick Examples¶
Type System¶
// dictionary has no initializer-list constructor, so it is built up
dross::dictionary alice;
alice["name"] = dross::string("Alice");
alice["age"] = dross::number(30);
dross::dictionary bob;
bob["name"] = dross::string("Bob");
bob["age"] = dross::number(25);
dross::dictionary root;
root["users"] = dross::array{alice, bob};
root["count"] = dross::number(2);
dross::value doc = root;
// Safe access: ask contains() before reading, is<T>() before casting
if (doc.is<dross::dictionary>()) {
dross::dictionary top = doc.as<dross::dictionary>();
if (top.contains("users") && top["users"].is<dross::array>()) {
for (const auto& user : top["users"].as<dross::array>()) {
// Process each user
}
}
}
Error Handling¶
#include <fstream>
#include <iterator>
// Function returning optional
std::optional<dross::string> get_env_config(const dross::string& key)
{
if (auto found = dross::environment::value(key)) { // Implicit string conversion
return dross::string(*found);
}
return std::nullopt;
}
// Function returning expected. dross parses TOML; reading the bytes is
// left to the standard library.
std::expected<dross::dictionary, dross::error> load_config(const dross::path& file)
{
std::ifstream input{file.string(), std::ios::binary};
if (!input) {
return std::unexpected(dross::error{std::errc::no_such_file_or_directory});
}
const std::string text{std::istreambuf_iterator<char>{input},
std::istreambuf_iterator<char>{}};
return dross::toml::deserialize(dross::data{text});
}
Platform Utilities¶
// Working with paths
const std::string home = dross::environment::value("HOME").value_or("/tmp");
const dross::path config_path = dross::path{home}.append(".config").append("myapp");
// XDG directories: the application name is already part of the result
dross::xdg app{"myapp"};
const std::string app_data = app.data_home().value_or(config_path.string());
// Create the directory. mkdir() is idempotent: it succeeds whether it
// creates the directory or finds it already there.
if (auto result = dross::path::mkdir(app_data); !result) {
std::cerr << "mkdir: " << result.error().what() << std::endl;
}