LoginSignup
3
2

More than 5 years have passed since last update.

ApplicationProperties

Last updated at Posted at 2017-12-21

概要

プロパティのコレクションを管理します。

これは、PropertiesFileオブジェクトを管理するためのやや上位のラッパーです。

内部には、ユーザー固有の設定(ユーザーディレクトリに格納されているもの)と、すべてのユーザーに共通の設定(すべてのユーザーがアクセスできるフォルダに格納されているもの)の2種類のPropertiesFileオブジェクトがあります。

このクラスは、オンデマンドでこれらのファイルの作成を管理し、getUserSettings()およびgetCommonSettings()メソッドを介したアクセスを許可します。

ApplicationPropertiesオブジェクトのインスタンスを作成したら、最初にsetStorageParameters()を呼び出して、ファイルの作成に使用するパラメータを指定する必要があります。

いわゆるアプリのプロジェクトファイルやユーザーコンフィグなどアプリの設定値を管理するためのクラスという認識でよさそう。

作成されるファイルは以下のフォルダにデフォルトでは保存される。

windows
C:\Documents and Settings\username\Application Data\[folderName]\[applicationName].[filenameSuffix]
macos
~/Library/[osxLibrarySubFolder]/[folderName]/[applicationName].[filenameSuffix]

リファレンス

検証

test.cpp
//==============================================================================
int main (int argc, char* argv[])
{
    initialiseJuce_GUI();   //GUIが初期化できていないと動作しない

    ApplicationProperties apppro;
    PropertiesFile::Options options;

    //ファイル名を決定する
    options.applicationName = "ApplicationPropertiesTest";
    options.filenameSuffix = ".settings";
    options.osxLibrarySubFolder = "Application Support";
    apppro.setStorageParameters(options);

    //これが初めて呼び出されると、プロパティファイルが作成されてロードされます。
    juce::PropertiesFile* commonProperties = apppro.getCommonSettings(true);


    //環境により以下のフォルダに格納される
    //windows   C:\Documents and Settings\username\Application Data\[folderName]\[applicationName].[filenameSuffix]
    //macos     ~/Library/[osxLibrarySubFolder]/[folderName]/[applicationName].[filenameSuffix]
    //linux     ~/[folderName]/[applicationName].[filenameSuffix]
    juce::PropertiesFile* userProperties = apppro.getUserSettings();
    userProperties->setValue("mode", true);
    userProperties->setValue("x", 1);
    userProperties->setValue("y", 1.5f);
    userProperties->setValue("z", 1.2);

    //現在の値のディスクへの書き込みが強制的に行われます。
    userProperties->save();

    //最後の値から変更されていれば、すべての値がディスクにフラッシュされます
    apppro.saveIfNeeded();

    return 0;
}

out.xml
<?xml version="1.0" encoding="UTF-8"?>

<PROPERTIES>
  <VALUE name="mode" val="1"/>
  <VALUE name="x" val="1"/>
  <VALUE name="y" val="1.5"/>
  <VALUE name="z" val="1.1999999999999999556"/>
</PROPERTIES>

補足

挙動を確認しgetCommonSettings()、getUserSettings()は同じパラメータについて環境共通の状態(common)とユーザーごとの上書き値(UserSettings)を取り扱う想定ではないかと推測する。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2