1 module cassowary.Variable;
2 
3 import std.conv;
4 import cassowary.AbstractVariable;
5 import cassowary.LinearExpression;
6 
7 class ClVariable : ClAbstractVariable
8 {
9 	this(string name, double value = 0)
10 	{
11 		super(name);
12 		_value = value;
13 		if (varMap !is null)
14 		{
15 			varMap[name] = this;
16 		}
17 	}
18 
19 	this(double value = 0)
20 	{
21 		_value = value;
22 	}
23 
24 	this(long number, string prefix, double value = 0)
25 	{
26 		super(number, prefix);
27 		_value = value;
28 	}
29 
30 	override bool isDummy() const
31 	{
32 		return false;
33 	}
34 
35 	override bool isExternal() const
36 	{
37 		return true;
38 	}
39 
40 	override bool isPivotable() const
41 	{
42 		return false;
43 	}
44 
45 	override bool isRestricted() const
46 	{
47 		return false;
48 	}
49 
50 	override string toString() const
51 	{
52 		return "[" ~ name ~ ":" ~ _value.to!string() ~ "]";
53 	}
54 
55 	// change the value held -- should *not* use this if the variable is
56 	// in a solver -- instead use addEditVar() and suggestValue() interface
57 	final double value() const
58 	{
59 		return _value;
60 	}
61 
62 	final void set_value(double value)
63 	{
64 		_value = value;
65 	}
66 
67 	// permit overriding in subclasses in case something needs to be
68 	// done when the value is changed by the solver
69 	// may be called when the value hasn't actually changed -- just
70 	// means the solver is setting the external variable
71 	void change_value(double value)
72 	{
73 		_value = value;
74 	}
75 
76 
77 	ClLinearExpression opBinary(string op) (double arg)
78 	{
79 		static if (op == "+")
80 			return new ClLinearExpression(this, 1, arg);
81 		else
82 			static if (op == "-")
83 				return new ClLinearExpression(this, 1, -arg);
84 			else
85 				static if (op == "*")
86 					return new ClLinearExpression(this, arg, 0);
87 				else
88 					static if (op == "/")
89 						return new ClLinearExpression(this, 1.0 / arg, 0);
90 	}
91 
92 	ClLinearExpression opBinaryRight(string op) (double arg)
93 	{
94 		static if (op == "+")
95 			return new ClLinearExpression(this, 1, arg);
96 		else
97 			static if (op == "-")
98 				return new ClLinearExpression(this, -1, arg);
99 			else
100 				static if (op == "*")
101 					return new ClLinearExpression(this, arg, 0);
102 	}
103 
104 	ClLinearExpression opBinary(string op) (ClVariable arg)
105 	{
106 		static if (op == "+")
107 			return (new ClLinearExpression(this)).plus(arg);
108 		else
109 			static if (op == "-")
110 				return (new ClLinearExpression(this)).minus(arg);
111 			else
112 				static if (op == "*")
113 					return new ClLinearExpression(this).times(arg);
114 				else
115 					static if (op == "/")
116 						return new ClLinearExpression(this).divide(arg);
117 	}
118 
119 	Object attachedObject;
120 	static ClVariable[string] varMap;
121 	private double _value;
122 }