-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsingleheader.h
2541 lines (1893 loc) · 75 KB
/
singleheader.h
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __GFInitialize_H
#define __GFInitialize_H
namespace Groundfloor {
/// Initializes custom memory mapping system
bool initGroundfloor();
/// Frees custom memory mapping system
bool finiGroundfloor();
}
#endif // __GFInitialize_H
#ifndef __GFFreeable_H_
#define __GFFreeable_H_
/***************************************************************************/
#include <cstdio>
#include <cstring>
namespace Groundfloor {
/** TGFFreeable adds a virtual destructor, so that when
* an inherited class is created, it can still be destroyed by calling
* delete (TGFFreeable *)object;
*/
class Freeable {
private:
Freeable& operator=(const Freeable* s); // not allowed to make deep copies
protected:
virtual const char* classname() { return "Freeable"; }
public:
Freeable() {};
/** a default virtual destructor with no implementation */
virtual ~Freeable() {};
virtual int DebugString(char *buffer, long bufferlen);
};
}
/***************************************************************************/
#endif // __GFFreeable_H_
#ifndef __GFCALLABLE_H
#define __GFCALLABLE_H
namespace Groundfloor {
/** TGFCallable defines an abstruct function execute() that
* other objects may call upon.
*/
class Callable: public Groundfloor::Freeable
{
public:
/// abstract function to implement
virtual void execute() = 0;
};
}
#endif // __GFCALLABLE_H
#ifndef __GFMemFuncs_H
#define __GFMemFuncs_H
#include <cstdlib>
// To use the system defaults directly, use these defines
#define GFMalloc(a) malloc(a)
#define GFFree(a) free(a)
#define GFRealloc(a,c) realloc(a,c)
namespace Groundfloor {
void nullifyMem(void *p, unsigned int count);
}
#endif //__GFMemFuncs_H
#ifndef __GFDEFINES_H
#define __GFDEFINES_H
#include <cstddef>
#if defined(__linux) || defined(macintosh) || (defined(__MACH__) && defined(__APPLE__)) || defined(__FreeBSD__) || defined(__FreeBSD) || (defined (__SVR4) && defined (__sun))
#define GF_OS_LINUX
#define GF_CP_GCC
#if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__))
#define GF_OS_MAC
#define GF_OS_BSD
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD)
#define GF_OS_BSD
#endif
#if (defined (__SVR4) && defined (__sun))
#define GF_OS_SOLARIS
#endif
#else
#ifdef _WIN32
#define GF_OS_WIN32
#if defined(_MSC_VER)
#define GF_CP_MVC
#endif
#ifdef __MINGW32__
#define GF_CP_MINGW
#endif
#else
#define GF_OS_UNKNOWN
#define GF_CP_UNKNOWN
#endif
#endif
#ifdef GF_OS_LINUX
#include <unistd.h>
#define GFMillisleep(a) usleep(a*1000)
typedef long long __int64;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
#else
// we'll never want winsock1
#define _WINSOCKAPI_
#include <windows.h>
#define GFMillisleep(a) Sleep(a)
#endif
#include <cstdio>
#define MILLISLEEP(a) DoNotUseThisFunction(a)
#define GFWIN32NEXTLINE {13,10,0}
#define GFLINUXNEXTLINE {10,0}
#define GFMACOSNEXTLINE {10,0}
#ifdef GF_OS_WIN32
#define GFNATIVENEXTLINE GFWIN32NEXTLINE
#else
#ifdef GF_OS_MAC
#define GFNATIVENEXTLINE GFMACOSNEXTLINE
#else
#define GFNATIVENEXTLINE GFLINUXNEXTLINE
#endif
#endif
const char S_ANSI_WIN32_SLASH[] = "\\";
const char S_ANSI_LINUX_SLASH[] = "/";
const wchar_t S_WIDE_WIN32_SLASH[] = L"\\";
const wchar_t S_WIDE_LINUX_SLASH[] = L"/";
#ifdef GF_OS_WIN32
#define S_ANSI_NATIVE_SLASH S_ANSI_WIN32_SLASH
#define S_WIDE_NATIVE_SLASH S_WIDE_WIN32_SLASH
#else
#define S_ANSI_NATIVE_SLASH S_ANSI_LINUX_SLASH
#define S_WIDE_NATIVE_SLASH S_WIDE_LINUX_SLASH
#endif
//#define GFTRACING
#ifdef GFTRACING
#define GFTRACE_ASSERT(b) if ( b ) { printf( "%d:<%s>:%s(ASSERT Failed)\n", __LINE__, __FILE__, __FUNCTION__ ); }
#define GFTRACE() printf( "%d:<%s>:%s\n", __LINE__, __FILE__, __FUNCTION__ )
#define GFTRACE_D(d) printf( "%d:<%s>::%s(%d)\n", __LINE__, __FILE__, __FUNCTION__, d )
#define GFTRACE_SD(s,d) printf( "%d:<%s>::%s(%d)\n", __LINE__, __FILE__, __FUNCTION__, s, d )
#define GFTRACE_X(d) printf( "%d:<%s>::%s(%x)\n", __LINE__, __FILE__, __FUNCTION__, d )
#define GFTRACE_S(s) printf( "%d:<%s>::%s(%s)\n", __LINE__, __FILE__, __FUNCTION__, s )
#else
#define GFTRACE_ASSERT(b) /* b */
#define GFTRACE() /* */
#define GFTRACE_D(d) /* d */
#define GFTRACE_SD(s,d) /* s d */
#define GFTRACE_X(d) /* d */
#define GFTRACE_S(s) /* s */
#endif
#endif // __GFDEFINES_H
#ifndef __GFLOCKABLE_H
#define __GFLOCKABLE_H
#ifdef GF_OS_LINUX
#include <pthread.h>
#endif
#ifdef GF_OS_WIN32
#define GFLOCK_NOWAIT 0
#define GFLOCK_INFINITEWAIT INFINITE
#else
#define GFLOCK_NOWAIT 0
#define GFLOCK_INFINITEWAIT -1
#endif
namespace Groundfloor {
unsigned long getCurrentThreadID();
/** TGFLockable is a simple wrapper around a native mutex.
* When compiled for the Linux platform, it uses the PThread library.
* The mutex is thread-orientated, so calling lock() functions within the same
* thread will not cause the function to fail the second time.
* If a second thread attempts to lock it, it will fail until it is unlocked by the first thread.
*/
class Lockable : public Freeable
{
protected:
#ifdef GF_OS_WIN32
HANDLE hndMutex;
#else
pthread_mutex_t hndMutex;
pthread_mutexattr_t pAttr;
#if !defined(CLOCK_REALTIME) || defined(GF_OS_MAC)
int iFunctionTime;
#endif
#endif
int iNestingLevel;
public:
/// Creates a native mutex without initial ownership.
Lockable();
/// Frees the mutex.
~Lockable();
/// Tries to obtain ownership over the mutex or just fails. (same as lockWhenAvailable(GFLOCK_NOWAIT))
bool lock();
/// Tries to obtain ownership over the mutex, when the specified timeout (in milliseconds) has elapsed or the ownership is obtained, the function exits.
/// The defines GFLOCK_NOWAIT or GFLOCK_INFINITEWAIT may also be used to specify iTimeout.
bool lockWhenAvailable(int iTimeout = GFLOCK_INFINITEWAIT);
/// Releases the mutex for other threads to access.
bool unlock();
};
class LockableScope : public Freeable
{
protected:
Lockable *lock;
public:
LockableScope(Lockable *lock);
~LockableScope();
};
}
#endif // __GFMUTEXABLE_H
#ifndef __GFSTRING_H_
#define __GFSTRING_H_
/***************************************************************************/
#include <iostream>
bool bytewisematch( const char *s1, const char *s2, unsigned int iLen );
bool bytewisematch_reference( const char *s1, const char *s2, unsigned int iLen );
namespace Groundfloor {
/** String is a class that defines a variable length string
* that can be manipulated and queried by member functions.
* Internally it is always treated as binary data, unless a member function indicates the use of zero-terminated strings.
* The string does not - and will not ever - know internally whether it contains an ansi-string or a wide-string,
* therefor it is recommended using widestring functions with caution.
*/
class String : public Freeable {
private:
String& operator=(const String* s); // not allowed to make deep copies
protected:
const char* classname() { return "String"; }
unsigned int strlength;
unsigned int size;
char *value;
public:
/// Initializes the string to "", length 0. Not recommended to use.
String();
/// Initializes the string to sValue of given length.
String(const char *sValue, unsigned int iLength);
String(const unsigned char *sValue, unsigned int iLength);
String(const String *sValue);
String(const std::string sValue);
/// Initializes the string to sValue using strlen() for length (\\0 terminated)
String(const char *sValue); // ANSI
String(const wchar_t *sValue); // Wide (Windows UTF-16 / Linux UTF-32)
// copy constructor
String(const String &obj);
~String();
//---------------------------------------------------------------------------
// binary-oriented base functions
/// Copies the given binary string into itself, freeing the old string data.
bool setValue(const char *sValue, unsigned int iLength);
bool setValue(const unsigned char *sValue, unsigned int iLength);
/// Appends the given binary string to the current string, adjusts the length and resizes memory space if necessary.
bool append(const char *sValue, unsigned int iLength);
/// Prepends the given binary string to the current string, adjusts the length and always remaps the string data.
bool prepend(const char *sValue, unsigned int iLength);
/// Returns whether or not the string equals the string sNeedle by length and content
bool match(const char *sNeedle, unsigned int l) const;
/// Returns whether or not the string starts with the substring sNeedle
bool startsWith(const char *sNeedle, unsigned int l) const;
/// Returns whether or not the string ends with the substring sNeedle
bool endsWith(const char *sNeedle, unsigned int l) const;
/// Returns the first index of the substring sNeedle, searching from iStart to the end of the string.
int pos(unsigned int iStart, const char *sNeedle, unsigned int iNeedleLen) const;
/// Returns the last index of the substring sNeedle, searching from 0 to the end of the string.
/*** todo: make this function search backwards ***/
int lastpos(const char *sNeedle, unsigned int l) const;
//---------------------------------------------------------------------------
bool match(const String *sNeedle) const;
bool match(const std::string sValue) const;
bool startsWith(const String *sNeedle) const;
bool endsWith(const String *sNeedle) const;
int pos(unsigned int iStart, const String *sNeedle) const;
int lastpos(const String *sNeedle) const;
/// Appends the given String to the current string, adjusts the length and resizes memory space if necessary.
bool append(const String *sValue);
/// Appends the given String of given length to the current string, adjusts the length and resizes memory space if necessary.
bool append(const String *sValue, unsigned int iLength);
bool append(const std::string sValue);
/// Appends the given char/byte to the current string, adjusts the length and resizes memory space if necessary.
bool append(char aChar);
/// Copies the given String into itself, freeing the old string data.
bool setValue(const String *sValue);
bool setValue(const std::string sValue);
/// Replace substring a with substring b
bool replace(const String *sSubStringA, const String *sSubStringB);
/// Remove a piece of the string from start to end;
bool remove(unsigned int iStartPos, unsigned int iEndPos);
/// Prepends the given String to the current string, adjusts the length and always remaps the string data.
bool prepend(const String *sValue);
/// Prepends the given String of given length to the current string, adjusts the length and always remaps the string data.
bool prepend(const String *sValue, unsigned int iLength);
/// Prepends the given char/byte to the current string, adjusts the length and resizes memory space if necessary.
bool prepend(char aChar);
/// Remaps the currently used memory space to a length of iSize. If iSize is shorter than the length of the string, the string is truncated.
/** If memory failes to allocate; false is returned.
* Also a precautionary false and printf message is given if the size exceeds 0x3FFFFFF,
* in order to avoid memory violations. Please use StringVector for big chunks of data.
**/
bool setSize(unsigned int iSize);
bool setSize_old(unsigned int iSize);
/// Sets the length of the string, remapping the memory used if the iLength is longer than the size. Places a \\0 at the end of the string (at length + 1).
/** Fails if internal setSize() failed.
**/
bool setLength(unsigned int iLength);
/// Returns the currently set length of the string.
unsigned int getLength() const;
/// Returns the currently allocated size of the string data in bytes.
unsigned int getSize() const;
//---------------------------------------------------------------------------
/// Returns a pointer to the beginning of the string data that is currently used.
/** Use this function with caution, as the location of the string data will be remapped as soon
* as the string is deleted or resized by one of the following functions:
* setValue(), prepend(), append(), setSize(), setLength()
*/
char *getValue() const;
/// Returns a pointer reinterpreted as a wchar_t*, the contents of the string remain unchanged.
/// (To actually convert ansi to wide, use the function transformToWidestring())
wchar_t *getValueAsWide() const;
/// Returns a pointer to the string from given position. Use with even more caution than getValue().
char *getPointer(unsigned int iPosition) const;
/// Causes the length of the string to be adjusted so that there are no trailing characters of the following subset: \\r, \\n, \\t and ' '.
void rtrim_ansi(bool bOnlyRemoveCrlf = false);
/// left trim (\\r, \\n, \\t and ' ')
void ltrim_ansi();
void trimzero_ansi();
bool match_ansi(const char *sNeedle) const;
bool match_wide(const wchar_t *sNeedle) const;
bool startsWith_ansi(const char *sNeedle) const;
bool endsWith_ansi(const char *sNeedle) const;
int pos_ansi(unsigned int iStart, const char *sNeedle) const;
int pos_ansi(const char *sNeedle) const;
int lastpos_ansi(const char *sNeedle) const;
bool setValue_ansi(const char *sValue);
bool setValue_wide(const wchar_t *sValue);
bool append_ansi(const char *sValue);
bool append_wide(const wchar_t *sValue);
bool prepend_ansi(const char *sValue);
bool prepend_wide(const wchar_t *sValue);
bool replace_ansi(const char *sSubStringA, const char *sSubStringB);
/// Makes all lowercase alpha characters uppercase.
void uppercase_ansi();
void uppercase_wide();
/// Makes all uppercase alpha characters lowercase.
void lowercase_ansi();
void lowercase_wide();
/// Transforms the current string into a wchar_t* based string based on the current locality,
/// if bCurrentIsUtf8 is true the string is assumed to be UTF-8 instead of the current locality
bool transformToWidestring(bool bCurrentIsUtf8, bool bFakeIfFailed = true);
/// Transforms the current wchar_t* based string into an ansi string that matches the current locality,
/// if bCurrentIsUtf8 is true the string is transformed into an UTF-8 string instead of using the current locality
bool transformWidestringToString(bool bForceToUtf8, bool bFakeIfFailed = true);
/// operators
String& operator=(const char* s);
String& operator=(const wchar_t* s);
String& operator=(const std::string s);
String& operator<<(const String *s);
String& operator<<(const char *s);
String& operator<<(const wchar_t *s);
friend std::ostream& operator<<(std::ostream& Ostr, String* s);
//friend std::ostream& operator<<(std::ostream& Ostr, String s);
};
}
/***************************************************************************/
#endif // __GFSTRING_H_
#ifndef __GFBaseCommunicator_H
#define __GFBaseCommunicator_H
namespace Groundfloor {
/** Basic error and statistics information that can be utilized when
* sending and receiving data.
*/
class CommReturnData : public Freeable {
public:
CommReturnData();
~CommReturnData();
bool eof;
bool error;
int errorcode;
__int64 affected;
};
/** TGFBaseCommunicator is intended to be a base class for stream-oriented communication classes.
*/
class BaseCommunicator : public Freeable {
protected:
bool bConnected;
public:
/// initializes object, sets connection status to false
BaseCommunicator();
/// automatically disconnects before destruction
~BaseCommunicator();
/// returns and/or checks whether or not the communicator is still connected
virtual bool isConnected();
/// connect (open) the stream, returns false if the attempt failed
virtual bool connect() = 0;
/// disconnect (close) the stream
virtual bool disconnect() = 0;
/// sends data (as binary string), returns false if attempt failed, fills errData when object is given
virtual bool send(const String *sData, CommReturnData *errData = NULL) = 0;
/// receives data (as binary string), returns false if attempt failed, fills errData when object is given
/// potentially overwrites contents of given data string (sData->getSize() is used as bufferlength)
virtual bool receive(String *sData, CommReturnData *errData = NULL) = 0;
};
}
#endif // __GFBaseCommunicator_H
#ifndef __GFThread_H_
#define __GFThread_H_
/***************************************************************************/
#ifdef GF_OS_LINUX
#include <pthread.h>
#endif
namespace Groundfloor {
/** Thread creates a new native thread where the execution code is called from.
* Add your own code by overriding the execute() function, this function gets
* called every given interval during the timespan of the thread.
* Warning: do not call stopAndWait() within the execute() running code,
* the thread will deadlock on this call.
*/
class Thread : public Callable {
protected:
bool bRunning;
bool bStartedExecutionMethod;
bool bShouldTerminate;
bool bExceptionOccurred;
#ifdef GF_OS_LINUX
pthread_t aPThreadStruct;
#endif
void *pHandle;
bool bThreadIsCreated;
unsigned int iCurrentInterval;
unsigned int iDefaultSleepValue;
void cleanup();
public:
/// Constructor. Initializes the interval at 100ms. The thread will not be started yet.
Thread();
/// Destructor. Stops the thread and exits.
~Thread();
/// Returns whether or not the executionloop() is still running.
bool isRunning();
/// Spawns a new thread to work in.
void start();
/// Signals executionloop() to stop looping, and exits.
/** Do not delete this object right after calling stop(), check for its running state first. Or better: use stopAndWait() */
void stop();
/// Signals executionloop() to stop looping, and waits until the executionloop has exited as well.
void stopAndWait();
/// Default waiting loop until the thread has stopped, do not run while in execute
void waitWhileRunning();
/// Waits for both threads until they've stopped, do not run while in execute
void join(Thread *anOtherThread);
/// Sets the interval at which the execute() function is called.
void setInterval(unsigned int iInterval);
/// This function must stay public, yet should not be called.
/** It is the entrypoint of the native thread and will loop every set interval until the thread is stopped. */
void executionloop();
};
}
/***************************************************************************/
#endif // __GFThread_H_
#ifndef __GFRESOURCE_H
#define __GFRESOURCE_H
namespace Groundfloor {
void generateResourceFrom( const String *source, const String *destination, const String *name );
}
#endif // __GFRESOURCE_H
#ifndef __GFDIRECTORY_H
#define __GFDIRECTORY_H
#ifndef GF_OS_WIN32
#include <dirent.h>
#else
struct _FIX_WIN32_FIND_DATAW {
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
WCHAR cFileName[512];
WCHAR cAlternateFileName[28];
};
#endif
namespace Groundfloor {
/** Directory is a helper class to browse through a directory index.
* Be aware that there is no support for wildcards yet on the Linux platform.
*/
class Directory : public Freeable
{
protected:
String sCurrentDir;
bool bOpen;
#ifdef GF_OS_WIN32
bool bFirstTime;
HANDLE hDir;
WIN32_FIND_DATAA anEntryA;
_FIX_WIN32_FIND_DATAW anEntryW;
#else
DIR *hDir;
struct dirent *anEntry;
#endif
public:
/// Initialize a closed index.
Directory();
~Directory();
/// Opens a directory for browsing, you need to call next() after this in order to retreive the first element. sDir should be in UTF8.
bool open(const String *sDir);
/// Go to the first or next element.
bool next();
/// Close browsing.
bool close();
bool isOpen();
/// Retreive the filename of the current element in UTF8. The returned String must be freeed after using it.
String *getFileName();
String *getShortName();
/// Checks whether or not the current element is a directory.
bool isDirectory();
};
}
#endif // __GFDIRECTORY_H
//---------------------------------------------------------------------------
#ifndef GFVectorH
#define GFVectorH
//---------------------------------------------------------------------------
namespace Groundfloor {
/** TGFVector is a dynamic array with autoresize behaviour.
*
* Note:
* In every function with an index reference, there is a check if the index
* is within bounds of the vector. If you don't want the vector to check this,
* you can compile the class with the define GFVECTOR_NOINDEXCHECK.
*/
class Vector: public Freeable {
protected:
Freeable **paVector;
unsigned int iAllocated;
unsigned int iElementCount;
unsigned int iPreAllocateCount;
unsigned int iRemovedCount;
unsigned int ptrsize;
public:
/// To enable/disable the ability to insert an object on insertSomewhere() without checking if the removeElement() function was used before.
/** The default on creation is False. */
bool forceInsert;
/// To enable/disable the behaviour of auto-deleting all objects in the array on deletion.
/** The default on creation is True. */
bool autoClear;
/// Default constructor, sets Preallocation count on 1 and initializes the array with 0 objects.
Vector();
/// Constructor with preallocation count argument.
/** If a memory-remap is scheduled on addElement()
* or insert functions, the size of the array is incremented by this Preallocation count.
* This is intended as a speedup feature, use 1 to be more memory efficient.
*/
Vector( unsigned int iPreAlloc );
/// Copy constructor
Vector(const Vector &AnotherVector);
/// Destructor, frees memory and optionally deletes all containing objects.
~Vector();
void setPreAlloc(unsigned int iPreAlloc);
/// Adds an object to the end of the array, resizing if necessary.
int addElement(Freeable *pElement );
/// Inserts the pointer to pElement at the first position that is not occupied by another element.
/** Resizes the vector if necessary. */
int insertSomewhere(Freeable *pElement );
/// Inserts the pointer to pElement at position iAt, shifting 0 or more elements to their index +1.
/** Resizes the vector if necessary. */
int insertAt( unsigned int iAt, Freeable *pElement );
/// Returns a pointer to the object that is at index i.
Freeable *elementAt( unsigned int i ) const;
/// Removes the element at index i by replacing it by a NULL-value and returns the removed object pointer.
/** The destructor of the element will not be called by this function. */
Freeable *removeElement( unsigned int i );
/// Replaces the element at index i by pElement and returns the object pointer that was replaced.
Freeable *replaceElement( unsigned int i, Freeable *pElement );
/// Removes (and returns) the first element in the vector, and shifts 2 to size() to the front
Freeable *removeFirstElementAndShiftToLeft();
/// Swaps the element at index i with the element at index j
void swapElements( unsigned int i, unsigned int j );
/// Removes the element obj by replacing it by a NULL-value.
/** The destructor of the element will not be called by this function. */
void removeElement( const Freeable *pElement );
/// Remaps the used memory for the dynamic array, truncating the array if its new size is smaller than the original size.
bool resizeVector( unsigned int size );
/// Calls the destructors of every remaining object in the array, and sets the array size to 0.
void clear();
/// Returns the index of pElement in the dynamic array. If the object was not found, -1 is returned.
int findElement( const Freeable *pElement ) const;
/// Returns the element count of the array. Removed/nilled elements are not subtracted from this count.
unsigned int size() const;
/// Returns the ammount of elements the array can contain at the moment.
unsigned int allocated() const;
/// Compresses the array by element shifting and reallocation.
/** Shifts out removed or nilled elements from the array and resets the element count
* and finally remaps the array to a more enclosed allocation.
*/
void compress();
/// Copies the used array to a newly allocated piece of memory that is assigned to the given vector.
/** Replaces the old mapping without calling the destructors of the old elements. */
bool fastCopy( Vector *into );
};
}
//---------------------------------------------------------------------------
#endif
#ifndef __GFCallbacks_H
#define __GFCallbacks_H
#define GFCreateNotify(a,b,c,d) new NotifyFunctionPointer<a,b>( c, d )
#define GFCreateRetreive(a,b,c,d) new RetreiveFunctionPointer<a,b>( c, d )
namespace Groundfloor {
// template implementaties moeten helaas in de header file, omdat iedere
// keer bij een nieuw type bij [new NotifyFunction<type>()] een nieuwe implementatie
// moet worden aangemaakt en worden gecompileerd in de uiteindelijke binary.
template <class P>
class NotifyAbstract : public Callable
{
protected:
P aDefaultParam;
public:
NotifyAbstract() : Callable() {
aDefaultParam = 0;
}
~NotifyAbstract() {
}
void setDefaultParam(P aParam) {
aDefaultParam = aParam;
}
virtual void execute(P aParam) = 0;
};
template <class P>
class RetreiveAbstract : public Freeable
{
public:
RetreiveAbstract() : Freeable() {
}
~RetreiveAbstract() {
}
virtual P retreive() = 0;
};
template <class P>
class MultiNotify : public NotifyAbstract<P>
{
protected:
Groundfloor::Vector *aNotifyList;
P aDefaultParam;
public:
MultiNotify() : NotifyAbstract<P>::NotifyAbstract() {
aNotifyList = new Vector();
aNotifyList->autoClear = true;
aDefaultParam = 0;
}
MultiNotify(MultiNotify<P> *pNotify) : NotifyAbstract<P>::NotifyAbstract() {
aNotifyList = new Vector();
pNotify->aNotifyList->fastCopy(aNotifyList);
aNotifyList->autoClear = false;
aDefaultParam = pNotify->aDefaultParam;
}
~MultiNotify() {
delete aNotifyList;
}
void setDefaultParam(P aParam) {
aDefaultParam = aParam;
}
void execute() {
execute(aDefaultParam);
}
void execute(P aParam) {
unsigned int c = aNotifyList->size();
for (unsigned int i = 0; i < c; i++) {
NotifyAbstract<P> *obj = static_cast<NotifyAbstract<P> *>(aNotifyList->elementAt(i));
if (obj != 0) {
obj->execute(aParam);
}
}
}
void addNotify(NotifyAbstract<P> *aNotify) {
aNotifyList->addElement(aNotify);
}
void clearNotifies() {
aNotifyList->clear();
}
void removeNotify(NotifyAbstract<P> *aNotify) {
aNotifyList->removeElement(aNotify);
}
NotifyAbstract<P> *getNotify(unsigned int i) {
return static_cast<NotifyAbstract<P> *>(aNotifyList->elementAt(i));
}
unsigned int getNotifyCount() {
return aNotifyList->size();
}
// operators
MultiNotify& operator+= (NotifyAbstract<P> *aNotify) {
aNotifyList->addElement(aNotify);
return *this;
}
};
template <class P, class T>
class NotifyFunctionPointer : public NotifyAbstract<P>
{
protected:
typedef void (T::*FuncCallbackOneParam)(P);
T *pObject;
FuncCallbackOneParam pFunction;
public:
NotifyFunctionPointer(T *pObject, FuncCallbackOneParam pFunction) : NotifyAbstract<P>::NotifyAbstract() {
this->pObject = pObject;
this->pFunction = pFunction;
};
~NotifyFunctionPointer() {
};
void execute() {
(pObject->*pFunction)(this->aDefaultParam);
};
void execute(P aParam) {
(pObject->*pFunction)(aParam);
};
};
template <class P, class T>
class RetreiveFunctionPointer : public RetreiveAbstract<P>
{
protected:
typedef P(T::*FuncCallbackNoParam)();
T *pObject;
FuncCallbackNoParam pFunction;
public:
RetreiveFunctionPointer(T *pObject, FuncCallbackNoParam pFunction) : RetreiveAbstract<P>::RetreiveAbstract() {
this->pObject = pObject;
this->pFunction = pFunction;
};
~RetreiveFunctionPointer() {
};
P retreive() {
return (pObject->*pFunction)();
};
};
}
#endif // __GFCallbackPointerClasses_H
//---------------------------------------------------------------------------
#ifndef GFStringLookupH
#define GFStringLookupH
//---------------------------------------------------------------------------
namespace Groundfloor {
class SimpleStringLookupObject : public Freeable {
public:
SimpleStringLookupObject(const char *sName, Freeable *anObject);
SimpleStringLookupObject(const String *sName, Freeable *anObject);
~SimpleStringLookupObject();
String name;
Freeable *object;
};
class SimpleStringLookup : public Vector {
public:
SimpleStringLookup();
~SimpleStringLookup();
void addObjectByString(const char *sName, Freeable *anObject);
Freeable *getObjectByString(const String *sName, bool bCaseSensitive = true);
Freeable *getObjectByString(const char *sName, bool bCaseSensitive = true);
void removeObjectByString(const char *sName);
void insertObjectSortedByLength(const char *sName, Freeable *anObject, bool bAscending = true);
void insertObjectSortedByLength(const String *sName, Freeable *anObject, bool bAscending = true);
void deleteAndClear();
};
}
//---------------------------------------------------------------------------