From 8d46f80bb9fb98869e6eb5ce643418d6e2c639eb Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Wed, 9 Jul 2025 19:24:03 +0300 Subject: [PATCH] mu-config: split get() into get_str() and decode() Makes it easier to use elsewhere. --- lib/mu-config.hh | 90 +++++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 32 deletions(-) diff --git a/lib/mu-config.hh b/lib/mu-config.hh index eaa0cd94..c7fd1020 100644 --- a/lib/mu-config.hh +++ b/lib/mu-config.hh @@ -1,5 +1,5 @@ /* -** Copyright (C) 2023 Dirk-Jan C. Binnema +** Copyright (C) 2023-2025 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -39,18 +39,18 @@ namespace Mu { struct Property { enum struct Id { - BatchSize, /**< Xapian batch-size */ - Contacts, /**< Cache of contact information */ - Created, /**< Time of creation */ - IgnoredAddresses,/**< Email addresses ignored for the contacts-cache */ - LastChange, /**< Time of last change */ - LastIndex, /**< Time of last index */ - MaxMessageSize, /**< Maximum message size (in bytes) */ + BatchSize, /**< Xapian batch-size */ + Contacts, /**< Cache of contact information */ + Created, /**< Time of creation */ + IgnoredAddresses, /**< Email addresses ignored for the contacts-cache */ + LastChange, /**< Time of last change */ + LastIndex, /**< Time of last index */ + MaxMessageSize, /**< Maximum message size (in bytes) */ PersonalAddresses, /**< List of personal e-mail addresses */ - RootMaildir, /**< Root maildir path */ - SchemaVersion, /**< Xapian DB schema version */ - SupportNgrams, /**< Support ngrams for indexing & querying - * for e.g. CJK languages */ + RootMaildir, /**< Root maildir path */ + SchemaVersion, /**< Xapian DB schema version */ + SupportNgrams, /**< Support ngrams for indexing & querying + * for e.g. CJK languages */ /* */ _count_ /* Number of Ids */ }; @@ -65,6 +65,7 @@ struct Property { Configurable = 1 << 1, /**< A user-configurable parameter; name * starts with 'conf-' */ Internal = 1 << 2, /**< Mu-internal field */ + Runtime = 1 << 3, /**< May change at runtime */ }; enum struct Type { Boolean, /**< Some boolean value */ @@ -132,7 +133,7 @@ public: { Id::LastChange, Type::Timestamp, - Flags::ReadOnly, + Flags::ReadOnly | Flags::Runtime, MetadataIface::last_change_key, {}, "Time when last change occurred" @@ -140,7 +141,7 @@ public: { Id::LastIndex, Type::Timestamp, - Flags::ReadOnly, + Flags::ReadOnly | Flags::Runtime, "last-index", {}, "Time when last indexing operation was completed" @@ -222,6 +223,47 @@ public: return Nothing; } + + /** + * Get the string-value for prop. + * + * For internal use + * + * @param prop some property + * + * @return a string + */ + std::string get_str(const Property& prop) const { + const auto str = cstore_.metadata(std::string{prop.name}); + return str.empty() ? std::string{prop.default_val} : str; + } + + + /** + * Get the property value decoded based on the type + * + * @param prop_id a property id + * + * @return the value or Nothing + */ + template + static constexpr auto decode(const std::string& str) { + if constexpr (type == Type::Number) + return static_cast(str.empty() ? 0 : std::atoll(str.c_str())); + if constexpr (type == Type::Boolean) + return static_cast(str.empty() ? false : + std::atol(str.c_str()) != 0); + else if constexpr (type == Type::Timestamp) + return static_cast(str.empty() ? 0 : std::atoll(str.c_str())); + else if constexpr (type == Type::Path || type == Type::String) + return str; + else if constexpr (type == Type::StringList) + return split(str, SepaChar1); + throw std::logic_error("invalid type"); + } + + + /** * Get the property value of the correct type * @@ -231,24 +273,8 @@ public: */ template auto get() const { - constexpr auto&& prop{property()}; - const auto str = std::invoke([&]()->std::string { - const auto str = cstore_.metadata(std::string{prop.name}); - return str.empty() ? std::string{prop.default_val} : str; - }); - if constexpr (prop.type == Type::Number) - return static_cast(str.empty() ? 0 : std::atoll(str.c_str())); - if constexpr (prop.type == Type::Boolean) - return static_cast(str.empty() ? false : - std::atol(str.c_str()) != 0); - else if constexpr (prop.type == Type::Timestamp) - return static_cast(str.empty() ? 0 : std::atoll(str.c_str())); - else if constexpr (prop.type == Type::Path || prop.type == Type::String) - return str; - else if constexpr (prop.type == Type::StringList) - return split(str, SepaChar1); - - throw std::logic_error("invalid prop " + std::string{prop.name}); + constexpr auto& prop{property()}; + return decode(get_str(prop)); } /**