forked from ishani/ClangVSx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVXRegistry.cs
175 lines (143 loc) · 5.25 KB
/
CVXRegistry.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* ClangVSx - Compiler Bridge for CLang in MS Visual Studio
* Harry Denholm, ishani.org 2011-2012
*
* https://github.com/ishani/ClangVSx
* http://www.ishani.org/web/articles/code/clangvsx/
*
* Released under LLVM Release License. See LICENSE.TXT for details.
*/
using System;
using System.Linq.Expressions;
using System.Windows.Forms;
using Microsoft.Win32;
namespace ClangVSx
{
#region Generic registry access
public static class RegistryBooleanSupport
{
public static object GetValueFromKey(this RegistryKey reg, String keyName, object state)
{
if (state.GetType() == typeof(Boolean))
{
var stateAsBool = (Boolean)state;
Int32 defaultState = (stateAsBool ? 1 : 0);
defaultState = (Int32)reg.GetValue(keyName, defaultState);
return (defaultState != 0);
}
else
{
return reg.GetValue(keyName, state);
}
}
public static void SetKeyFromValue(this RegistryKey reg, String keyName, object state)
{
if (state.GetType() == typeof(Boolean))
{
var stateAsBool = (Boolean)state;
Int32 saveValue = (stateAsBool ? 1 : 0);
reg.SetValue(keyName, saveValue);
}
else
{
reg.SetValue(keyName, state);
}
}
}
internal class Win32Registry
{
// root reg key
private const String CVXRegistryKey = "Software\\Ishani\\ClangVSx";
public T LoadFrom<T>(String keyName, T defaultValue)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(CVXRegistryKey);
if (key == null)
{
key = Registry.CurrentUser.CreateSubKey(CVXRegistryKey);
}
T itemDefaultState = defaultValue;
try
{
itemDefaultState = (T)key.GetValueFromKey(keyName, itemDefaultState);
}
catch (Exception e)
{
MessageBox.Show("Error when reading setting from registry:\n\n" + e.StackTrace);
}
finally
{
key.Close();
}
return ((itemDefaultState != null) ? itemDefaultState : defaultValue);
}
public void SaveTo<T>(String keyName, T saveValue)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(CVXRegistryKey, true);
if (key == null)
{
key = Registry.CurrentUser.CreateSubKey(CVXRegistryKey);
}
try
{
key.SetKeyFromValue(keyName, saveValue);
}
finally
{
key.Close();
}
}
}
#endregion
internal class CVXRegistryItem<T>
{
private readonly T _default_value;
private readonly string _name;
private readonly Win32Registry _registry = new Win32Registry();
public CVXRegistryItem(Expression<Func<CVXRegistryItem<T>>> expr, T defaultValue)
{
var body = ((MemberExpression)expr.Body);
_name = body.Member.Name;
_default_value = defaultValue;
}
// push or pull values from the registry automatically
public T Value
{
get
{
// sketchy way to get name of property from inside accessors
return _registry.LoadFrom(_name, _default_value);
}
set { _registry.SaveTo(_name, value); }
}
// this is just sugar that allows for
// String foo = ShowCommands;
public static implicit operator T(CVXRegistryItem<T> d)
{
return d.Value;
}
}
/// <summary>
/// Wrapper around registry access to load/save AddIn settings
/// </summary>
internal abstract class CVXRegistry
{
public static CVXRegistryItem<String> PathToClang = new CVXRegistryItem<String>(() => PathToClang,
"C:\\clang\\clang.exe");
// output options
public static CVXRegistryItem<Boolean> ShowCommands = new CVXRegistryItem<Boolean>(() => ShowCommands, false);
public static CVXRegistryItem<Boolean> MakeBatchFiles = new CVXRegistryItem<Boolean>(() => MakeBatchFiles, false);
public static CVXRegistryItem<Boolean> EchoInternal = new CVXRegistryItem<Boolean>(() => EchoInternal, false);
public static CVXRegistryItem<Boolean> ShowPhases = new CVXRegistryItem<Boolean>(() => ShowPhases, false);
// compiler options toggles
public static CVXRegistryItem<Boolean> COptCPP11 = new CVXRegistryItem<Boolean>(() => COptCPP11, true);
public static CVXRegistryItem<Boolean> COptMSABI = new CVXRegistryItem<Boolean>(() => COptMSABI, false);
// global options
public static CVXRegistryItem<String> CommonArgs = new CVXRegistryItem<String>(() => CommonArgs, "");
public static CVXRegistryItem<String> TripleWin32 = new CVXRegistryItem<String>(() => TripleWin32,
"i686-pc-win32");
public static CVXRegistryItem<String> TripleX64 = new CVXRegistryItem<String>(() => TripleX64, "x86_64-pc-win32");
public static CVXRegistryItem<String> TripleARM = new CVXRegistryItem<String>(() => TripleARM,
"armv7-apple-darwin10");
public static CVXRegistryItem<Boolean> TOptOldSyntax = new CVXRegistryItem<Boolean>(() => TOptOldSyntax, false);
}
}