1 /**
2  * Copyright: Copyright (c) 2012 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Aug 26, 2012
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module mambo.text.ConfigMap;
8 
9 import mambo.core._;
10 
11 template ConfigMapMixin ()
12 {
13 	private Value[string] map;
14 
15 	@property ref Value opDispatch (string name) ()
16 	{
17 		if (auto v = name in map)
18 			return *v;
19 
20 		else
21 		{
22 			map[name] = Value();
23 			return map[name];
24 		}
25 	}
26 
27 	@property ref Value opDispatch (string name) (string value)
28 	{
29 		map[name] = Value(value);
30 		return map[name];
31 	}
32 }
33 
34 struct ConfigMap
35 {
36 	mixin ConfigMapMixin;
37 	alias map this;
38 }
39 
40 struct Value
41 {
42 	string value;
43 	alias value this;
44 
45 	private Value[string] map;
46 
47 	@property auto opDispatch (string name) ()
48 	{
49 		static if (__traits(compiles, mixin("{ value." ~ name ~ "();}")))
50 			mixin("return value." ~ name ~ "();");
51 
52 		else
53 		{
54 			if (auto v = name in map)
55 				return *v;
56 
57 			else
58 			{
59 				map[name] = Value();
60 				return map[name];
61 			}
62 		}
63 	}
64 
65 	@property auto opDispatch (string name) (string value)
66 	{
67 		static if (__traits(compiles, mixin("{ this.value." ~ name ~ "(value);}")))
68 			mixin("return this.value." ~ name ~ "(value);");
69 
70 		else
71 		{
72 			map[name] = Value(value);
73 			return map[name];
74 		}
75 	}
76 
77 	string toString () const
78 	{
79 		return isSet ? value : map.toString;
80 	}
81 
82 	@property bool isSet () const
83 	{
84 		return value.any;
85 	}
86 }