template <typename F, typename T, typename = std::enable_if_t<is_serializable<T, F>::value>>
rll::configuration_file class

Configuration file that can be loaded and saved.

Template parameters
F Format type. Must be value of tag rolly::serialization::format or your own format tag.
T Type to serialize and deserialize. Must implement rolly::serialization::serializable_and_deserializable with character type char.

This class is used to store configuration values in a configuration file in given format. Accepts any type that implements serializer with character type char and given format.

Example usage:

struct DummyConfiguration
{
  u32 test = 0;
  struct IpAddress
  {
    string ip;
    u16 port;
    struct SockMode
    {
      bool tcp = true;
      bool udp = false;
    } sock_mode{};
  } ip_address = { .ip = "127.0.0.1", .port = 25565 };
};
template <>
auto rolly::serialization::serialize<fl::serialization::format::toml>(DummyConfiguration const&
value) -> std::basic_string<char> { / implementation / }

template <>
auto rolly::serialization::deserialize<fl::serialization::format::toml>(std::basic_string<char>
const& value) -> DummyConfiguration { / implementation / }

auto main() -> int
{
  using enum rolly::saving_policy;
  using namespace rolly::serialization::format;

  auto config = rolly::configuration_file<format::toml, DummyConfiguration>(
    "test.toml",
    fs::current_path() / "test-cfg",
    explicit_
  );
  if(not config)
    return 1;
  std::cout << config().ip_address.ip << std::endl;
  try {
    config().ip_address.sock_mode.udp = true;
    config.save();
  } catch(...) {
    return 1;
  }
  return 0;
}

Base classes

class filedevice

Public static variables

static auto write_permissions constexpr

Public static functions

static auto read_from(std::filesystem::path const& path) →  std::string
static auto try_read_from(std::filesystem::path const& path) →  result<std::string> noexcept
static auto try_write_to(std::filesystem::path const& path, std::string_view content) →  result noexcept
static void write_to(std::filesystem::path const& path, std::string_view content) noexcept(…)

Constructors, destructors, conversion operators

configuration_file(std::filesystem::path path, saving_policy policy) explicit
Creates or loads configuration file from given path with saving policy.
configuration_file(std::string_view const filename, std::filesystem::path const& folder, saving_policy policy) explicit
Creates or loads configuration file from given path and folder with saving policy.
configuration_file(configuration_file const&) defaulted
configuration_file(configuration_file&&) defaulted
operator bool() const explicit noexcept
Returns whether configuration file is valid or not.
~configuration_file() virtual noexcept
Closes configuration file.

Public functions

auto default_values() const →  T const&
Default values of configuration file.
auto default_values_mut() →  T&
Mutable reference to default values of configuration file.
auto exists() const →  bool
auto is_regular_file() const →  bool
auto load() →  result
Loads configuration file from file.
auto operator()() const →  T const&
Constant reference to the current values of configuration file.
auto operator()() →  T&
Mutable reference to the current values of configuration file.
auto operator=(configuration_file const&) →  configuration_file& defaulted
auto operator=(configuration_file&&) →  configuration_file& defaulted
auto path() const →  std::filesystem::path const&
auto read() const →  std::string
auto revert_to_default() →  result
Reverts configuration file to default values.
auto save() const →  result
Saves configuration file to file.
auto saving_policy() const →  enum saving_policy
Saving policy.
auto suffixed_path(std::string_view const suffix) const →  std::filesystem::path
auto try_read() const →  result<std::string> noexcept
auto try_write(std::string_view content) const →  result noexcept
auto valid() const →  bool
Returns whether configuration file is valid or not.
auto values() const →  T const&
Constant reference to the current values of configuration file.
auto values_mut() →  T&
Mutable reference to the current values of configuration file.
void write(std::string_view content) const noexcept(…)

Function documentation

template <typename F, typename T, typename _3>
static std::string rll::configuration_file::read_from(std::filesystem::path const& path)

template <typename F, typename T, typename _3>
static result<std::string> rll::configuration_file::try_read_from(std::filesystem::path const& path) noexcept

template <typename F, typename T, typename _3>
static result rll::configuration_file::try_write_to(std::filesystem::path const& path, std::string_view content) noexcept

template <typename F, typename T, typename _3>
static void rll::configuration_file::write_to(std::filesystem::path const& path, std::string_view content) noexcept(…)

template <typename F, typename T, typename _3>
rll::configuration_file::configuration_file(std::filesystem::path path, saving_policy policy) explicit

Creates or loads configuration file from given path with saving policy.

Parameters
path Path to configuration file.
policy Saving policy.

template <typename F, typename T, typename _3>
rll::configuration_file::configuration_file(std::string_view const filename, std::filesystem::path const& folder, saving_policy policy) explicit

Creates or loads configuration file from given path and folder with saving policy.

Parameters
filename Filename of configuration file.
folder Folder where configuration file is located.
policy Saving policy.

template <typename F, typename T, typename _3>
rll::configuration_file::operator bool() const explicit noexcept

Returns whether configuration file is valid or not.

template <typename F, typename T, typename _3>
rll::configuration_file::~configuration_file() virtual noexcept

Closes configuration file.

If saving policy is set to saving_policy::autosave, file will be saved automatically on closing.

template <typename F, typename T, typename _3>
bool rll::configuration_file::exists() const

template <typename F, typename T, typename _3>
bool rll::configuration_file::is_regular_file() const

template <typename F, typename T, typename _3>
T const& rll::configuration_file::operator()() const

Constant reference to the current values of configuration file.

template <typename F, typename T, typename _3>
T& rll::configuration_file::operator()()

Mutable reference to the current values of configuration file.

template <typename F, typename T, typename _3>
std::filesystem::path const& rll::configuration_file::path() const

template <typename F, typename T, typename _3>
std::string rll::configuration_file::read() const

template <typename F, typename T, typename _3>
result rll::configuration_file::save() const

Saves configuration file to file.

Exceptions
std::exception if saving fails.

template <typename F, typename T, typename _3>
std::filesystem::path rll::configuration_file::suffixed_path(std::string_view const suffix) const

template <typename F, typename T, typename _3>
result<std::string> rll::configuration_file::try_read() const noexcept

template <typename F, typename T, typename _3>
result rll::configuration_file::try_write(std::string_view content) const noexcept

template <typename F, typename T, typename _3>
void rll::configuration_file::write(std::string_view content) const noexcept(…)

Variable documentation

template <typename F, typename T, typename _3>
static auto rll::configuration_file::write_permissions constexpr