forked from zeromq/clrzmq4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZSymbol.cs
140 lines (109 loc) · 3.12 KB
/
ZSymbol.cs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
namespace ZeroMQ
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using lib;
public abstract class ZSymbol
{
public ZSymbol(int errno)
: this(errno, null, null) { }
public ZSymbol(int errno, string errname, string errtext)
{
this._num = errno;
this._name = errname;
this._txt = errtext;
}
private int _num;
public int Number { get { return _num; } protected set { _num = value; } }
private string _name;
public string Name { get { return _name; } protected set { _name = value; } }
private string _txt;
public string Text { get { return _txt; } protected set { _txt = value; } }
private static void PickupConstantSymbols<T>(ref HashSet<ZSymbol> symbols, bool lookupText = false)
where T : ZSymbol
{
Type type = typeof(T);
FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
Type codeType = type.GetNestedType("Code");
// Pickup constant symbols
foreach (FieldInfo symbolField in fields.Where(f => typeof(ZSymbol).IsAssignableFrom(f.FieldType)))
{
FieldInfo symbolCodeField = codeType.GetField(symbolField.Name);
if (symbolCodeField != null)
{
var symbolNumber = (int)symbolCodeField.GetValue(null);
var symbol = Activator.CreateInstance(typeof(T), new object[] {
symbolNumber,
symbolCodeField.Name,
lookupText ? Marshal.PtrToStringAnsi(zmq.strerror(symbolNumber)) : string.Empty
});
symbolField.SetValue(null, symbol);
symbols.Add((ZSymbol)symbol);
}
}
}
public static readonly ZSymbol None = default(ZSymbol);
static ZSymbol()
{
// System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(zmq).TypeHandle);
var symbols = new HashSet<ZSymbol>();
PickupConstantSymbols<ZError>(ref symbols, true);
_allSymbols = symbols.ToArray();
}
public bool IsEmpty()
{
return this == default(ZSymbol);
}
static readonly ZSymbol[] _allSymbols;
public static IEnumerable<ZSymbol> Find(string symbol)
{
return _allSymbols
.Where(s => s.Name == null ? false : (s.Name == symbol));
}
public static IEnumerable<ZSymbol> Find(string ns, int num)
{
return _allSymbols
.Where(s => s.Name == null ? false : (s.Name.StartsWith(ns) && s.Number == num));
}
public override bool Equals(object obj)
{
return ZSymbol.Equals(this, obj);
}
public static new bool Equals(object a, object b)
{
if (object.ReferenceEquals(a, b))
{
return true;
}
var symbolA = a as ZSymbol;
if (symbolA == null)
{
return false;
}
var symbolB = b as ZSymbol;
if (symbolB == null)
{
return false;
}
return symbolA.GetHashCode() == symbolB.GetHashCode();
}
public override int GetHashCode()
{
int hash = Number.GetHashCode();
if (Name != null)
hash ^= Name.GetHashCode();
return hash;
}
public override string ToString()
{
return (Name == null ? string.Empty : Name) + "(" + Number + "): " + Text;
}
public static implicit operator int(ZSymbol errnum)
{
return errnum == null ? -1 : errnum.Number;
}
}
}