1 | // tPlatform.cpp  |
2 | //  |
3 | // Tacent platform defines, architecture, and endianness detection. The Tacent library has some preprocessor define  |
4 | // requirements. One of PLATFORM_NNN, ARCHITECTURE_NNN, and CONFIG_NNN need to be defined. If you haven't bothered  |
5 | // to define these in the project file with a /D switch, an attempt is made to define them automatically for you.  |
6 | //  |
7 | // Copyright (c) 2004-2006, 2015, 2017, 2020 Tristan Grimmer.  |
8 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
9 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
10 | //  |
11 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
12 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
13 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
14 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
15 | // PERFORMANCE OF THIS SOFTWARE.  |
16 |   |
17 | #include "Foundation/tPlatform.h"  |
18 | #include "Foundation/tString.h"  |
19 |   |
20 |   |
21 | tPlatform tGetPlatform()  |
22 | {  |
23 | #if defined(PLATFORM_WINDOWS)  |
24 | return tPlatform::Windows;  |
25 | #elif defined(PLATFORM_LINUX)  |
26 | return tPlatform::Linux;  |
27 | #elif defined(PLATFORM_MACOS)  |
28 | return tPlatform::MacOS;  |
29 | #elif defined(PLATFORM_ANDROID)  |
30 | return tPlatform::Android;  |
31 | #elif defined(PLATFORM_IOS)  |
32 | return tPlatform::iOS;  |
33 | #else  |
34 | return tPlatform::Invalid;  |
35 | #endif  |
36 | }  |
37 |   |
38 |   |
39 | tPlatform tGetPlatform(const tString& name)  |
40 | {  |
41 | for (int p = 0; p < int(tPlatform::NumPlatforms); p++)  |
42 | if (name == tGetPlatformName(tPlatform(p)))  |
43 | return tPlatform(p);  |
44 |   |
45 | return tPlatform::Invalid;  |
46 | }  |
47 |   |
48 |   |
49 | const char* tGetPlatformName(tPlatform plat)  |
50 | {  |
51 | const static char* platNames[] =  |
52 | {  |
53 | "Windows" ,  |
54 | "Linux" ,  |
55 | "MacOS" ,  |
56 | "Android" ,  |
57 | "iOS" ,  |
58 | "All" ,  |
59 | "Invalid" ,  |
60 | };  |
61 | tStaticAssert( ((sizeof(platNames)/sizeof(*platNames)) - 2) == int(tPlatform::NumPlatforms) );  |
62 |   |
63 | if (plat == tPlatform::Invalid)  |
64 | return platNames[int(tPlatform::NumPlatforms)+1];  |
65 |   |
66 | return platNames[int(plat)];  |
67 | }  |
68 |   |
69 |   |
70 | const char* tGetPlatformNameLong(tPlatform plat)  |
71 | {  |
72 | const static char* platNames[] =  |
73 | {  |
74 | "Windows" ,  |
75 | "Linux" ,  |
76 | "OSX" ,  |
77 | "Android" ,  |
78 | "iOS" ,  |
79 | "All Platforms" ,  |
80 | "Invalid" ,  |
81 | };  |
82 | tStaticAssert( ((sizeof(platNames)/sizeof(*platNames)) - 2) == int(tPlatform::NumPlatforms) );  |
83 |   |
84 | if (plat == tPlatform::Invalid)  |
85 | return platNames[int(tPlatform::NumPlatforms)+1];  |
86 |   |
87 | return platNames[int(plat)];  |
88 | }  |
89 |   |
90 |   |
91 | tArchitecture tGetArchitecture()  |
92 | {  |
93 | #if defined(ARCHITECTURE_X64)  |
94 | return tArchitecture::x64;  |
95 | #elif defined(ARCHITECTURE_A64)  |
96 | return tArchitecture::A64;  |
97 | #else  |
98 | return tArchitecture::Invalid;  |
99 | #endif  |
100 | }  |
101 |   |
102 |   |
103 | const char* tGetArchitectureName(tArchitecture arch)  |
104 | {  |
105 | const static char* archNames[] =  |
106 | {  |
107 | "x64" ,  |
108 | "A64" ,  |
109 | "Invalid"   |
110 | };  |
111 | tStaticAssert( ((sizeof(archNames)/sizeof(*archNames)) - 1) == int(tArchitecture::NumArchitectures) );  |
112 |   |
113 | if (arch == tArchitecture::Invalid)  |
114 | return archNames[int(tArchitecture::NumArchitectures)];  |
115 |   |
116 | return archNames[int(arch)];  |
117 | }  |
118 |   |
119 |   |
120 | const char* tGetArchitectureNameLong(tArchitecture arch)  |
121 | {  |
122 | const static char* archNames[] =  |
123 | {  |
124 | "x64|AMD64|x86-64|64bit" ,  |
125 | "A64|AArch64|64bit" ,  |
126 | "Invalid"   |
127 | };  |
128 | tStaticAssert( ((sizeof(archNames)/sizeof(*archNames)) - 1) == int(tArchitecture::NumArchitectures) );  |
129 |   |
130 | if (arch == tArchitecture::Invalid)  |
131 | return archNames[int(tArchitecture::NumArchitectures)];  |
132 |   |
133 | return archNames[int(arch)];  |
134 | }  |
135 |   |
136 |   |
137 | tEndianness tGetEndianness(tPlatform plat)  |
138 | {  |
139 | switch (plat)  |
140 | {  |
141 | case tPlatform::Windows:  |
142 | case tPlatform::Linux:  |
143 | case tPlatform::MacOS:  |
144 | case tPlatform::Android:  |
145 | case tPlatform::iOS:  |
146 | return tEndianness::Little;  |
147 | }  |
148 |   |
149 | return tEndianness::Invalid;  |
150 | }  |
151 | |