summaryrefslogtreecommitdiffstats
path: root/src/Ref.vala
blob: 8ed6d0e3391963a0a3f224a53065ff25008377e0 (plain)
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
namespace Eva {
  public class Ref : Object, Term, Gee.Comparable<Ref> {
    public string node {get; construct;}
    public int len {get; construct;}
    public uint[] n {get; private set;}
    public uint creation {get; construct;}
    
    internal Ref(Erl.Ref reference) {
      this.create(binary_to_string(reference.node, Erl.MAXATOMLEN), reference.len, reference.n, reference.creation);
    }
    
    private Ref.create(string node0, int len0, uint* n0, uint creation0) {
      Object(node: node0, len: len0, creation: creation0);
      
      n = new uint[len];
      for(int i = 0; i < len; ++i) {
        n[i] = n0[i];
      }
    }
    
    public string to_string() {
      return "#Ref";
    }
    
    public int compare_to(Ref o) {
      for(int i = 0; i < len; ++i) {
        if(n[i] < o.n[i])
          return -1;
        if(n[i] > o.n[i])
          return 1;
      }
      
      return 0;
    }
    
    protected bool do_match(Term o, Gee.Map<string, Term> vars, Gee.Map<string, string> aliases) {
      if(o is Var) {
        return o.do_match(this, vars, aliases);
      }
      
      if(!(o is Ref))
        return false;
      
      Ref r = o as Ref;
      
      if(node != r.node || len != r.len || creation != r.creation) {
        return false;
      }
      
      for(int i = 0; i < len; ++i) {
        if(n[i] != r.n[i])
          return false;
      }
      
      return true;
    }
    
    internal void encode(Buffer buffer) {
      Erl.Ref reference = Erl.Ref();
      char[]? nodedata = string_to_binary(node);
      assert(nodedata != null);
      
      Memory.copy(reference.node, nodedata, int.min(Erl.MAXATOMLEN, nodedata.length));
      reference.node[int.min(Erl.MAXATOMLEN, nodedata.length)] = 0;
      
      reference.len = len;
      
      for(int i = 0; i < len; ++i) {
        reference.n[i] = n[i];
      }
      
      reference.creation = creation;
      
      buffer.buffer.encode_ref(reference);
    }
  }
}