Skip to content

Instantly share code, notes, and snippets.

@enjoysmath
Created May 25, 2013 21:29
Show Gist options
  • Save enjoysmath/5650848 to your computer and use it in GitHub Desktop.
Save enjoysmath/5650848 to your computer and use it in GitHub Desktop.
/++
Defines a valid range of possible values for a data variable
+/
module TheApp.DataVar;
import std.algorithm;
interface VarI
{
}
class Var(T) : VarI
{
private:
T val, min, max;
bool minMaxUsed;
T[] poss; // null will indicate not used
public:
this(T val_)
{
val = val_;
poss = null;
minMaxUsed = false;
}
this(T val_, T[] poss_)
in {
assert (canFind(poss_, val_));
}
body {
val = val_;
poss = poss_;
minMaxUsed = false;
}
this(T val_, T min_, T max_)
in {
assert (val_ >= min && val_ <= max);
}
body {
val = val_;
min = min_;
max = max_;
}
}
------ Build started: Project: TheApp, Configuration: Debug Win32 ------
Building Debug\TheApp.exe...
GlobalConfig.d(35): Error: no property 'opCall' for type 'TheApp.DataVar.Var!(uint).Var'
GlobalConfig.d(36): Error: no property 'opCall' for type 'TheApp.DataVar.Var!(uint).Var'
GlobalConfig.d(37): Error: no property 'opCall' for type 'TheApp.DataVar.Var!(uint).Var'
GlobalConfig.d(38): Error: no property 'opCall' for type 'TheApp.DataVar.Var!(uint).Var'
Building Debug\TheApp.exe failed!
Details saved as "file://C:\home\Dropbox\Dev\AudioSynth\TheApp\TheApp\Debug\TheApp.buildlog.html"
Build time: 4 s
Solution build stopped.
Build has been canceled.
/++
The global application configuration file
The global config is loaded from a file in the app directory. That is where all the global settings
of app are saved. There will be an option in the app to override the project settings in regard to the gui.
If the config file is ruined, a new one is created and the default values are specified here.
Copyright: Public Domain
License: use freely for any purpose
+/
module TheApp.GlobalConfig;
import sdlang;
debug import std.stdio;
import TheApp.DataVar;
struct GlobalConfig
{
private:
const static VarI[string][string] defaults;
static Tag config; // set to defaults by module constructor
public:
/++
Static constructor, called before main()
Defines the defaults and calls load().
+/
static this()
{
// define defaults
defaults = [
"window" : [
"width" : Var!uint(600, 100, 4000),
"height" : Var!uint(400, 100, 4000),
"x" : Var!uint(0, 0, 4000),
"y" : Var!uint(0, 0, 4000)
]
];
load();
}
/++
Static destructor, called before main() returns
Saves the global config to file so it can be loaded next time.
+/
static ~this()
{
save();
}
/++
Loads the last saved config or leaves config vars at defaults if it couldn't load.
If the last saved config could be loaded but a certain config var could not be found,
then the default val of that var will be left untouched.
+/
static void load()
{
//// load using SDLang
//try {
// config = parseFile("GlobalConfig.sdl");
//}
//catch(SDLangParseException e)
//{
// // Messages will be of the form:
// // myFile.sdl(5:28): Error: Invalid integer suffix.
// debug stderr.writeln(e.msg);
//}
//
//// test that each tag and value exists
//foreach (group; defaults.TagRange)
//{
// try {
// config.tags[ group.name ]; // check if it exists
// // it does exist
// foreach (var; group.TagRange)
// {
// }
// }
// catch (SDLangRangeException e)
// {
// debug stderr.writeln(e.msg);
// config.tags[ group.name ][0] = new Tag(config, "", group.name, [Value(group.values[0])]);
// }
//}
}
/++
Restore all configuration vars to their default values.
+/
static void restoreAllToDefault()
{
}
void save()
{
try {
// @@TODO
// try writing to the file if it exists
// if the file does not exist, create it
}
catch (Exception e)
{
// @@TODO
// couldn't save last window preset
}
}
private:
///++
// Parse the tag values that are under the tag aa["tag name"]. Start at the root tag.
//+/
//static void loadHelper (Value[string] aa, Tag root)
//{
// Tag config = root.tags[ aa["tag name"] ][0];
//
// foreach (key, val; aa)
// {
// // load using SDLang
// try {
// aa[key] = config.tags[ key ][0].values[0];
// }
// catch (SDLangException e)
// {
// // do nothing, default will be used
// debug stderr.writeln(e.msg);
// }
// }
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment