1 module autowrap.common;
2 
3 /**
4    Returns a string to be mixed in that defines DllMain, needed on Windows.
5  */
6 public string dllMainMixinStr() @safe pure {
7     return q{
8 
9         static if (!is(DllMainDefined)) {
10             enum DllMainDefined;
11 
12             import core.sys.windows.windows: HINSTANCE, BOOL, ULONG, LPVOID;
13 
14             __gshared HINSTANCE g_hInst;
15 
16             extern (Windows) BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved) {
17 
18                 import core.sys.windows.windows;
19                 import core.sys.windows.dll;
20 
21                 switch (ulReason)  {
22 
23                     case DLL_PROCESS_ATTACH:
24                         g_hInst = hInstance;
25                         dll_process_attach(hInstance, true);
26                     break;
27 
28                     case DLL_PROCESS_DETACH:
29                         dll_process_detach(hInstance, true);
30                         break;
31 
32                     case DLL_THREAD_ATTACH:
33                         dll_thread_attach(true, true);
34                         break;
35 
36                     case DLL_THREAD_DETACH:
37                         dll_thread_detach(true, true);
38                         break;
39 
40                     default:
41                 }
42 
43                 return true;
44             }
45         }
46     };
47 }
48 
49 
50 /// Converts an identifier from camelCase or PascalCase to snake_case.
51 string toSnakeCase(in string str) @safe pure {
52 
53     import std.algorithm: all, map;
54     import std.ascii: isUpper;
55 
56     if(str.all!isUpper) return str;
57 
58     string ret;
59 
60     string convert(in size_t index, in char c) {
61         import std.ascii: isLower, toLower;
62 
63         const prefix = index == 0 ? "" : "_";
64         const isHump =
65             (index == 0 && c.isUpper) ||
66             (index > 0 && c.isUpper && str[index - 1].isLower);
67 
68         return isHump ? prefix ~ c.toLower : "" ~ c;
69     }
70 
71     foreach(i, c; str) {
72         ret ~= convert(i, c);
73     }
74 
75     return ret;
76 }
77 
78 
79 version(AutowrapStrict)
80     enum AlwaysTry = true;
81 else
82     enum AlwaysTry = false;