--- nono/lib/config.h 2026/04/29 17:04:30 1.1.1.1 +++ nono/lib/config.h 2026/04/29 17:04:40 1.1.1.3 @@ -1,6 +1,7 @@ // // nono -// Copyright (C) 2018 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // // 設定ファイルは、テキスト形式、行志向で順序を持たない key=value ペアの羅列、 @@ -37,7 +38,7 @@ class ConfigItem }; public: - ConfigItem() { }; + ConfigItem() { } ConfigItem(const std::string& key_, const std::string& val_, from_t from_, const std::string& where_) { key = key_; @@ -45,7 +46,6 @@ class ConfigItem from = from_; where = where_; } - virtual ~ConfigItem() { }; // 値を文字列のまま取得する。 const std::string& AsString() const { return value; } @@ -65,7 +65,6 @@ class ConfigItem // 問題のため、VM 種別によって初期値が決定する場合などはデバイスの // コンストラクタでこれを呼び出すこと。例えば mpu-clock など。 void SetDefault(const std::string& val); - void SetDefault(int val); // from を取得する from_t GetFrom() const { return from; } @@ -75,7 +74,7 @@ class ConfigItem // この項目での指定のエラーメッセージを warnx() で出力する void Err(const std::string& msg) const; - void Err(const char *fmt, ...) const; + void Err(const char *fmt, ...) const __printflike(2, 3); private: // key が変数名、value が値。 @@ -132,20 +131,29 @@ class Config bool Update(const std::vector& options); // 内容を表示する - void Show() const; + void Show(bool all) const; - // key から ConfigItem を取得する。 - // key は必ず存在するので参照を返す。 + // key から ConfigItem を取得する。key は必ず存在するので参照を返す。 // 見付からなければ assert する。それはプログラムミスなので。 - const ConfigItem& Get(const std::string& key) const; - ConfigItem& Get(const std::string& key); + const ConfigItem& Find(const std::string& key) const; - private: - // 項目を追加 (初期状態用) + // デフォルト値を差し替える。 + // key が見付からなければ assert する。それはプログラムミスなので。 + void SetDefault(const std::string& key, int value); + void SetDefault(const std::string& key, const std::string& value); + + // 項目を追加 bool Add(const std::string& key); bool Add(const std::string& key, int value); bool Add(const std::string& key, const std::string& value); + // 項目を削除 + void Delete(const std::string& key); + + // 以降書き込み禁止にする (プログラムミスを避けるため) + void Fix(); + + private: // key から ConfigItem を返す。見付からなければ NULL を返す。 // (こっちは内部で変更するために使う) ConfigItem *FindItem(const std::string& key); @@ -160,6 +168,10 @@ class Config // 設定項目の集合 std::vector list {}; + + // 設定はある時点までは追加更新可能だが、途中からは読み込み専用としたい。 + // プログラムミスを避けるため。 + bool fixed {}; }; extern std::unique_ptr gConfig;