/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Gee;
using VQDR.Common;

namespace VQDR.Expression {
  
  public class Context : GLib.Object{
    private bool changed;
    private Gee.TreeMap<string, Variable?> values;
    
    
    construct {
      changed = false;
      values = new Gee.TreeMap<string, Variable?>
                              (Common.Utils.str_cmp, null);
    }
    
    public Context () {
    }
    
    public void set_value (string name, 
                           int min_val,
                           int max_val,
                           int current_val) {
      set_variable (name, Variable (min_val, max_val, current_val));
    }
    
    public Variable get_variable (string name) throws ArgError {
    throw_name (name);
    return values.@get (name.down ());
    }
    
    public void set_variable (string name, Variable? variable) {
      string new_name = name.down ();
      
    if (!(values.has_key (new_name)) ||
          !(values.get(new_name).equals(variable))) {
          
          values.set (new_name, variable);
          changed = true;
      }
    }
    
    private void throw_name (string name) throws ArgError {
      if (! (values.has_key (name.down ()))) {
         throw new ArgError.INVALID_ARGUMENT ("Name \"" +
                                               name.down () +
                                               "\" not defined.");
       }
    }
    
    public int get_value (string name) throws ArgError {
      throw_name (name);
      return values.@get (name.down ()).current_val;
    }
    
    public int get_min_value (string name) throws ArgError {
      throw_name (name);
      return values.@get (name.down ()).min_val;
    }
    
    public int get_max_value (string name) throws ArgError {
      throw_name (name);
      return values.@get (name.down ()).max_val;
    }

    public int get_current_value (string name) throws ArgError {
      throw_name (name);
      return values.@get (name.down ()).current_val;
    }
    
    public bool has_name (string name) {
      return values.has_key (name.down ());
    }
    
    protected void reset () {
      changed = false;
    }
    
  }
}