Skip to content

Commit 9ef602c

Browse files
committed
UI library: added stubs allowing to compile library without use of additional UModel's code
1 parent 1b16fa3 commit 9ef602c

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed

UI/stl-stub/Core.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#ifndef __CORE_H__
2+
#define __CORE_H__
3+
4+
//todo: should be defined before any include like windows.h etc - see BaseDialog.cpp
5+
#define _CRT_SECURE_NO_WARNINGS
6+
7+
#include <stdio.h>
8+
#include <stdarg.h>
9+
10+
11+
#define HAS_UI 1
12+
13+
#undef min
14+
#undef max
15+
16+
#define min(a,b) ( ((a) < (b)) ? (a) : (b) )
17+
#define max(a,b) ( ((a) > (b)) ? (a) : (b) )
18+
#define bound(a,minval,maxval) ( ((a) > (minval)) ? ( ((a) < (maxval)) ? (a) : (maxval) ) : (minval) )
19+
20+
#undef ARRAY_COUNT
21+
22+
#define ARRAY_ARG(array) array, sizeof(array)/sizeof(array[0])
23+
#define ARRAY_COUNT(array) (sizeof(array)/sizeof(array[0]))
24+
25+
#if _MSC_VER
26+
# define FORCEINLINE __forceinline
27+
# define stricmp _stricmp
28+
# define strnicmp _strnicmp
29+
#else
30+
# error Unsupported compiler
31+
#endif
32+
33+
#define guard(x)
34+
#define unguard
35+
#define unguardf(...)
36+
37+
38+
static void appError(const char* Format, ...)
39+
{
40+
va_list argptr;
41+
va_start(argptr, Format);
42+
char buffer[1024];
43+
_vsnprintf_s(buffer, ARRAY_COUNT(buffer), Format, argptr);
44+
va_end(argptr);
45+
46+
printf("%s", buffer);
47+
exit(1);
48+
}
49+
50+
static int appSprintf(char *dest, int size, const char *Format, ...)
51+
{
52+
va_list argptr;
53+
va_start(argptr, Format);
54+
int len = _vsnprintf_s(dest, size, size, Format, argptr);
55+
va_end(argptr);
56+
return len;
57+
}
58+
59+
static void appStrncpyz(char *dst, const char *src, int count)
60+
{
61+
int len = (int)strlen(src);
62+
if (len >= count)
63+
len = count-1;
64+
strncpy(dst, src, len);
65+
dst[len] = 0;
66+
}
67+
68+
#ifndef assert
69+
#define assert(x) if (!(x)) { appError("Assert: %s", #x); }
70+
#endif
71+
72+
#define appNotify(...)
73+
74+
#endif // __CORE_H__

UI/stl-stub/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This directory contains stub files which implements parts of UModel's core using STL. Should either copy these files
2+
into UI library's directory, or put path to this directory into compiler's include directories.
3+
4+
Additional build requirements which are not here, but exists in UModel project: callback.hpp, Win32Types.h

UI/stl-stub/UnCore.h

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#ifndef __UN_CORE_H__
2+
#define __UN_CORE_H__
3+
4+
#include <vector>
5+
#include <string>
6+
7+
template<typename T>
8+
struct TArray
9+
{
10+
private:
11+
std::vector<T> items;
12+
13+
public:
14+
FORCEINLINE T* GetData()
15+
{
16+
return items.size() > 0 ? &items[0] : NULL;
17+
}
18+
FORCEINLINE const T* GetData() const
19+
{
20+
return items.size() > 0 ? &items[0] : NULL;
21+
}
22+
FORCEINLINE T& operator[](int index)
23+
{
24+
return items[index];
25+
}
26+
FORCEINLINE const T& operator[](int index) const
27+
{
28+
return items[index];
29+
}
30+
FORCEINLINE int Num() const
31+
{
32+
return (int)items.size();
33+
}
34+
inline void Empty(int Count = 0)
35+
{
36+
items.resize(0);
37+
if (Count)
38+
{
39+
items.reserve(Count);
40+
}
41+
}
42+
FORCEINLINE void Reset(int Count = 0)
43+
{
44+
Empty(Count);
45+
}
46+
FORCEINLINE int Add(const T& item)
47+
{
48+
size_t OldCount = items.size();
49+
items.push_back(item);
50+
return (int)OldCount;
51+
}
52+
inline int AddZeroed(int Count = 1)
53+
{
54+
size_t OldCount = items.size();
55+
items.resize(OldCount + Count);
56+
return (int)OldCount;
57+
}
58+
FORCEINLINE int AddUninitialized(int Count = 1)
59+
{
60+
return AddZeroed(Count);
61+
}
62+
FORCEINLINE void ResizeTo(int count)
63+
{
64+
items.resize(count);
65+
}
66+
void RemoveAt(int index, int count = 1)
67+
{
68+
items.erase(items.begin() + index, items.begin() + index + count);
69+
}
70+
FORCEINLINE void RemoveAtSwap(int index, int count = 1)
71+
{
72+
RemoveAt(index, count);
73+
}
74+
int FindItem(const T& item, int startIndex = 0) const
75+
{
76+
for (int i = startIndex; i < items.size(); i++)
77+
{
78+
if (items[i] == item)
79+
{
80+
return i;
81+
}
82+
}
83+
return -1;
84+
}
85+
FORCEINLINE void Sort(int (*cmpFunc)(const T*, const T*))
86+
{
87+
if (items.size() > 0)
88+
{
89+
qsort(&items[0], items.size(), sizeof(T), (int (*)(const void*, const void*)) cmpFunc);
90+
}
91+
}
92+
};
93+
94+
template<typename T>
95+
inline void* operator new(size_t size, TArray<T> &Array)
96+
{
97+
assert(size == sizeof(T)); // allocating wrong object? can't disallow allocating of "int" inside "TArray<FString>" at compile time ...
98+
int index = Array.AddUninitialized(1);
99+
return Array.GetData() + index;
100+
}
101+
102+
// Just shut up compiler
103+
template<typename T>
104+
inline void operator delete(void* ptr, TArray<T> &Array)
105+
{
106+
assert(0);
107+
}
108+
109+
struct FString
110+
{
111+
private:
112+
std::string str;
113+
114+
public:
115+
FString()
116+
{}
117+
FString(const char* src)
118+
: str(src)
119+
{}
120+
FString& operator=(const char* src)
121+
{
122+
str = src;
123+
return *this;
124+
}
125+
FString& operator+=(const char* text)
126+
{
127+
str += text;
128+
return *this;
129+
}
130+
FORCEINLINE FString& operator+=(const FString& Str)
131+
{
132+
str += *Str;
133+
return *this;
134+
}
135+
FORCEINLINE FString& AppendChar(char ch)
136+
{
137+
str += ch;
138+
return *this;
139+
}
140+
FORCEINLINE const char* operator*() const
141+
{
142+
return str.c_str();
143+
}
144+
FORCEINLINE bool IsEmpty() const
145+
{
146+
return str.length() == 0;
147+
}
148+
FORCEINLINE char& operator[](int index)
149+
{
150+
return str[index];
151+
}
152+
FORCEINLINE const char& operator[](int index) const
153+
{
154+
return str[index];
155+
}
156+
FORCEINLINE int Len() const
157+
{
158+
return (int)str.length();
159+
}
160+
};
161+
162+
// No TStaticArray FStaticString
163+
164+
template<typename T, int N>
165+
struct TStaticArray : public TArray<T>
166+
{
167+
};
168+
169+
template<int N>
170+
struct FStaticString : public FString
171+
{
172+
FORCEINLINE FStaticString& operator=(const char* src)
173+
{
174+
return (FStaticString&) FString::operator=(src);
175+
}
176+
FORCEINLINE FStaticString& operator=(const FString& src)
177+
{
178+
return (FStaticString&) FString::operator=(src);
179+
}
180+
};
181+
182+
#endif // __UN_CORE_H__

0 commit comments

Comments
 (0)