#region Apache License 2.0
/*
Nuclex .NET Framework
Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion // Apache License 2.0
using System;
using System.Collections.Generic;
namespace Nuclex.Support.Settings {
/// Stores settings in memory
public class MemoryStore : ISettingsStore {
/// Initializes a new settings store managing settings in memory
public MemoryStore() {
this.options = new Dictionary>();
this.rootOptions = new Dictionary();
}
/// Enumerates the categories defined in the configuration
/// An enumerable list of all used categories
public IEnumerable EnumerateCategories() {
return this.options.Keys;
}
/// Enumerates the options stored under the specified category
/// Category whose options will be enumerated
/// An enumerable list of all options in the category
public IEnumerable EnumerateOptions(string category = null) {
IDictionary categoryOptions;
if(string.IsNullOrEmpty(category)) {
categoryOptions = this.rootOptions;
} else if(!this.options.TryGetValue(category, out categoryOptions)) {
yield break;
}
foreach(KeyValuePair option in categoryOptions) {
OptionInfo optionInfo = new OptionInfo() {
Name = option.Key,
OptionType = option.Value.GetType()
};
yield return optionInfo;
}
}
/// Retrieves the value of the specified option
/// Type the option will be converted to
/// Category the option can be found in. Can be null.
/// Name of the option that will be looked up
/// The value of the option with the specified name
public TValue Get(string category, string optionName) {
TValue value;
if(TryGet(category, optionName, out value)) {
return value;
} else {
if(string.IsNullOrEmpty(category)) {
throw new KeyNotFoundException(
"There is no option named '" + optionName + "' in the settings"
);
} else {
throw new KeyNotFoundException(
"There is no option named '" + optionName + "' under the category '" +
category + "' in the settings"
);
}
}
}
/// Tries to retrieve the value of the specified option
/// Type the option will be converted to
/// Category the option can be found in. Can be null.
/// Name of the option that will be looked up
/// Will receive the value of the option, if found
///
/// True if the option existed and its value was written into the
/// parameter, false otherwise
///
public bool TryGet(string category, string optionName, out TValue value) {
IDictionary categoryOptions = getCategoryByName(category);
if(categoryOptions != null) {
object valueAsObject;
if(categoryOptions.TryGetValue(optionName, out valueAsObject)) {
value = (TValue)Convert.ChangeType(valueAsObject, typeof(TValue));
return true;
}
}
value = default(TValue);
return false;
}
/// Saves an option in the settings store
/// Type of value that will be saved
/// Category the option will be placed in. Can be null.
/// Name of the option that will be saved
/// The value under which the option will be saved
public void Set(string category, string optionName, TValue value) {
IDictionary targetCategory;
if(string.IsNullOrEmpty(category)) {
targetCategory = this.rootOptions;
} else if(!this.options.TryGetValue(category, out targetCategory)) {
targetCategory = new Dictionary();
this.options.Add(category, targetCategory);
targetCategory.Add(optionName, value);
return;
}
targetCategory[optionName] = value;
}
/// Removes the option with the specified name
/// Category the option is found in. Can be null.
/// Name of the option that will be removed
/// True if the option was found and removed
public bool Remove(string category, string optionName) {
IDictionary targetCategory = getCategoryByName(category);
if(targetCategory == null) {
return false;
}
return targetCategory.Remove(optionName);
}
/// Looks up a category by its name
/// Name of the category that will be looked up
/// The category with the specified name if found, null otherwise
private IDictionary getCategoryByName(string name) {
IDictionary category;
if(string.IsNullOrEmpty(name)) {
category = this.rootOptions;
} else if(!this.options.TryGetValue(name, out category)) {
return null;
}
return category;
}
/// Categories and the options stored in them
private IDictionary> options;
/// Options stored at the root level
private IDictionary rootOptions;
}
} // namespace Nuclex.Support.Settings