jlib.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef JLIB_HPP
  14. #define JLIB_HPP
  15. #define EXPLICIT_INIT
  16. #ifdef _MSC_VER
  17. //disable these throughout the system because they occur a lot
  18. #pragma warning(disable : 4275 ) // should get link errors if something is wrong...
  19. #pragma warning(disable : 4251 ) // should get link errors if something is wrong...
  20. #pragma warning(disable : 4786 ) // identifier was truncated to '255' characters in the debug information
  21. #pragma warning(disable : 4355 ) // 'this' : used in base member initializer list
  22. #endif
  23. #include "modinit.h"
  24. #include "jiface.hpp"
  25. #include <assert.h>
  26. #include "jarray.hpp"
  27. #define loop for(;;)
  28. #define _elements_in(a) (sizeof(a)/sizeof((a)[0]))
  29. #define _memclr(s, n) memset(s, 0, n)
  30. #define _clear(a) memset(&a, 0, sizeof(a))
  31. #define _copy(dest, src) memcpy(&dest, &src, sizeof(src))
  32. class jlib_decl ICopyArray : public CopyReferenceArrayOf<IInterface> {};
  33. class jlib_decl IArray : public OwnedReferenceArrayOf<IInterface> {};
  34. class jlib_decl IPointerArray : public OwnedPointerArrayOf<IInterface> {};
  35. class jlib_decl CICopyArray : public CopyReferenceArrayOf<CInterface> {};
  36. class jlib_decl CIArray : public OwnedReferenceArrayOf<CInterface> {};
  37. class jlib_decl CharArray : public ArrayOf<char> { };
  38. class jlib_decl IntArray : public ArrayOf<int> { };
  39. class jlib_decl ShortArray : public ArrayOf<short> { };
  40. class jlib_decl FloatArray : public ArrayOf<float> { };
  41. class jlib_decl DoubleArray : public ArrayOf<double> { };
  42. class jlib_decl UnsignedArray : public ArrayOf<unsigned> { };
  43. class jlib_decl UnsignedShortArray : public ArrayOf<unsigned short> { };
  44. class jlib_decl PointerArray : public ArrayOf<void *> { };
  45. class jlib_decl ConstPointerArray : public ArrayOf<const void *> { };
  46. #ifdef _WIN32
  47. class jlib_decl BoolArray : public ArrayOf<bool> { };
  48. #else
  49. typedef CharArray BoolArray;
  50. #endif
  51. class jlib_decl Int64Array : public ArrayOf<__int64> { };
  52. class jlib_decl UInt64Array : public ArrayOf<unsigned __int64> { };
  53. template <class A, class C, class IITER>
  54. class ArrayIIteratorOf : public CInterface, implements IITER
  55. {
  56. protected:
  57. ArrayIteratorOf <A, C &> iterator;
  58. public:
  59. IMPLEMENT_IINTERFACE;
  60. ArrayIIteratorOf(A &array) : iterator(array) { }
  61. virtual bool first() { return iterator.first(); }
  62. virtual bool next() { return iterator.next(); }
  63. virtual bool isValid() { return iterator.isValid(); }
  64. virtual C & query() { return iterator.query(); }
  65. };
  66. template <class TYPE>
  67. class CIArrayOf : public CIArray
  68. {
  69. public:
  70. inline TYPE & item(aindex_t pos) const { return (TYPE &)CIArray::item(pos); }
  71. inline TYPE & popGet() { return (TYPE &)CIArray::popGet(); }
  72. inline TYPE & tos(void) const { return (TYPE &)CIArray::tos(); }
  73. inline TYPE & tos(aindex_t num) const { return (TYPE &)CIArray::tos(num); }
  74. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)CIArray::getArray(pos); }
  75. inline void append(TYPE& obj) { assert(&obj); CIArray::append(obj); }
  76. inline void appendUniq(TYPE& obj) { assert(&obj); CIArray::appendUniq(obj); }
  77. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); CIArray::add(obj, pos); }
  78. inline aindex_t find(TYPE & obj) const { assert(&obj); return CIArray::find(obj); }
  79. inline void replace(TYPE &obj, aindex_t pos, bool nodel=false) { assert(&obj); CIArray::replace(obj, pos, nodel); }
  80. inline bool zap(TYPE & obj, bool nodel=false) { assert(&obj); return CIArray::zap(obj, nodel); }
  81. };
  82. template <class TYPE>
  83. class CICopyArrayOf : public CICopyArray
  84. {
  85. public:
  86. inline TYPE & item(aindex_t pos) const { return (TYPE &)CICopyArray::item(pos); }
  87. inline TYPE & popGet() { return (TYPE &)CICopyArray::popGet(); }
  88. inline TYPE & tos(void) const { return (TYPE &)CICopyArray::tos(); }
  89. inline TYPE & tos(aindex_t num) const { return (TYPE &)CICopyArray::tos(num); }
  90. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)CICopyArray::getArray(pos); }
  91. inline void append(TYPE& obj) { assert(&obj); CICopyArray::append(obj); }
  92. inline void appendUniq(TYPE& obj) { assert(&obj); CICopyArray::appendUniq(obj); }
  93. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); CICopyArray::add(obj, pos); }
  94. inline aindex_t find(TYPE & obj) const { assert(&obj); return CICopyArray::find(obj); }
  95. inline void replace(TYPE &obj, aindex_t pos) { assert(&obj); CICopyArray::replace(obj, pos); }
  96. inline bool zap(TYPE & obj) { assert(&obj); return CICopyArray::zap(obj); }
  97. };
  98. template <class TYPE>
  99. class IArrayOf : public IArray
  100. {
  101. public:
  102. inline TYPE & item(aindex_t pos) const { return (TYPE &)IArray::item(pos); }
  103. inline TYPE & popGet() { return (TYPE &)IArray::popGet(); }
  104. inline TYPE & tos(void) const { return (TYPE &)IArray::tos(); }
  105. inline TYPE & tos(aindex_t num) const { return (TYPE &)IArray::tos(num); }
  106. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)IArray::getArray(pos); }
  107. inline void append(TYPE& obj) { assert(&obj); IArray::append(obj); }
  108. inline void appendUniq(TYPE& obj) { assert(&obj); IArray::appendUniq(obj); }
  109. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); IArray::add(obj, pos); }
  110. inline aindex_t find(TYPE & obj) const { assert(&obj); return IArray::find(obj); }
  111. inline void replace(TYPE &obj, aindex_t pos, bool nodel=false) { assert(&obj); IArray::replace(obj, pos, nodel); }
  112. inline bool zap(TYPE & obj, bool nodel=false) { assert(&obj); return IArray::zap(obj, nodel); }
  113. };
  114. template <class TYPE>
  115. class ICopyArrayOf : public ICopyArray
  116. {
  117. public:
  118. inline TYPE & item(aindex_t pos) const { return (TYPE &)ICopyArray::item(pos); }
  119. inline TYPE & popGet() { return (TYPE &)ICopyArray::popGet(); }
  120. inline TYPE & tos(void) const { return (TYPE &)ICopyArray::tos(); }
  121. inline TYPE & tos(aindex_t num) const { return (TYPE &)ICopyArray::tos(num); }
  122. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)ICopyArray::getArray(pos); }
  123. inline void append(TYPE& obj) { assert(&obj); ICopyArray::append(obj); }
  124. inline void appendUniq(TYPE& obj) { assert(&obj); ICopyArray::appendUniq(obj); }
  125. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); ICopyArray::add(obj, pos); }
  126. inline aindex_t find(TYPE & obj) const { assert(&obj); return ICopyArray::find(obj); }
  127. inline void replace(TYPE &obj, aindex_t pos) { assert(&obj); ICopyArray::replace(obj, pos); }
  128. inline bool zap(TYPE & obj) { assert(&obj); return ICopyArray::zap(obj); }
  129. };
  130. template <class TYPE>
  131. class IPointerArrayOf : public IPointerArray
  132. {
  133. public:
  134. inline TYPE * item(aindex_t pos) const { return (TYPE *)IPointerArray::item(pos); }
  135. inline TYPE * popGet() { return (TYPE *)IPointerArray::popGet(); }
  136. inline TYPE * tos(void) const { return (TYPE *)IPointerArray::tos(); }
  137. inline TYPE * tos(aindex_t num) const { return (TYPE *)IPointerArray::tos(num); }
  138. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)IPointerArray::getArray(pos); }
  139. inline void append(TYPE * obj) { IPointerArray::append(obj); }
  140. inline void appendUniq(TYPE * obj) { IPointerArray::appendUniq(obj); }
  141. inline void add(TYPE * obj, aindex_t pos) { IPointerArray::add(obj, pos); }
  142. inline aindex_t find(TYPE * obj) const { return IPointerArray::find(obj); }
  143. inline void replace(TYPE * obj, aindex_t pos, bool nodel=false) { IPointerArray::replace(obj, pos, nodel); }
  144. inline bool zap(TYPE * obj, bool nodel=false) { return IPointerArray::zap(obj, nodel); }
  145. };
  146. template <class TYPE>
  147. class PointerArrayOf : public PointerArray
  148. {
  149. typedef int (*PointerOfCompareFunc)(TYPE **, TYPE **);
  150. public:
  151. inline void add(TYPE * x, aindex_t pos) { PointerArray::add(x, pos); }
  152. inline void append(TYPE * x) { PointerArray::append(x); }
  153. inline aindex_t bAdd(TYPE * & newItem, PointerOfCompareFunc f, bool & isNew) { return PointerArray::bAdd(*(void * *)&newItem, (CompareFunc)f, isNew); }
  154. inline aindex_t bSearch(const TYPE * & key, CompareFunc f) const { return PointerArray:: bSearch(*(const void * *)&key, f); }
  155. inline aindex_t find(TYPE * x) const { return PointerArray::find(x); }
  156. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)PointerArray::getArray(pos); }
  157. inline TYPE * item(aindex_t pos) const { return (TYPE *)PointerArray::item(pos); }
  158. inline TYPE * popGet() { return (TYPE *)PointerArray::popGet(); }
  159. inline void replace(TYPE * x, aindex_t pos) { PointerArray::replace(x, pos); }
  160. inline TYPE * tos(void) const { return (TYPE *)PointerArray::tos(); }
  161. inline TYPE * tos(aindex_t num) const { return (TYPE *)PointerArray::tos(num); }
  162. inline bool zap(TYPE * x) { return PointerArray::zap(x); }
  163. };
  164. #include "jstring.hpp"
  165. #include "jarray.hpp"
  166. #include "jhash.hpp"
  167. #include "jstream.hpp"
  168. #include "jutil.hpp"
  169. template <class ARRAY>
  170. inline void appendArray(ARRAY & target, const ARRAY & source)
  171. {
  172. unsigned max = source.ordinality();
  173. if (max)
  174. {
  175. target.ensure(target.ordinality() + max);
  176. for (unsigned i=0; i < max; ++i)
  177. target.append(OLINK(source.item(i)));
  178. }
  179. }
  180. inline void appendArray(IArray & target, const IArray & source)
  181. {
  182. unsigned max = source.ordinality();
  183. if (max)
  184. {
  185. target.ensure(target.ordinality() + max);
  186. for (unsigned i=0; i < max; ++i)
  187. target.append(OLINK(source.item(i)));
  188. }
  189. }
  190. typedef bool (*boolFunc)(void);
  191. typedef void (*voidFunc)(void);
  192. typedef HINSTANCE SoContext;
  193. class ModExit;
  194. enum InitializerState { ITS_Uninitialized, ITS_Initialized };
  195. struct InitializerType
  196. {
  197. boolFunc initFunc;
  198. ModExit *modExit;
  199. unsigned int priority;
  200. unsigned int modpriority;
  201. SoContext soCtx;
  202. byte state;
  203. bool operator == (const InitializerType & other) const { return this == &other; }
  204. };
  205. class jlib_decl InitializerArray : public StructArrayOf<InitializerType> { };
  206. class jlib_decl InitTable
  207. {
  208. public:
  209. InitializerArray initializers;
  210. static int sortFuncDescending(InitializerType const *i1, InitializerType const *i2);
  211. static int sortFuncAscending(InitializerType const *i1, InitializerType const *i2);
  212. public:
  213. InitTable();
  214. void init(SoContext soCtx=0);
  215. void exit(SoContext soCtx=0);
  216. void add(InitializerType &iT);
  217. void add(boolFunc func, unsigned int priority, unsigned int modpriority, byte state=ITS_Uninitialized);
  218. count_t items() { return initializers.ordinality(); }
  219. InitializerType & element(aindex_t i) { return initializers.element(i); }
  220. };
  221. class jlib_decl ModInit
  222. {
  223. public:
  224. ModInit(boolFunc func, unsigned int _priority, unsigned int _modprio);
  225. };
  226. class jlib_decl ModExit
  227. {
  228. public:
  229. ModExit(voidFunc func);
  230. #if !defined(EXPLICIT_INIT)
  231. ~ModExit();
  232. #endif
  233. voidFunc func;
  234. };
  235. #ifndef MODULE_PRIORITY
  236. #define MODULE_PRIORITY 1
  237. #endif
  238. #define __glue(a,b) a ## b
  239. #define glue(a,b) __glue(a,b)
  240. #define MODULE_INIT(PRIORITY) \
  241. static bool glue(_modInit,__LINE__) (); \
  242. static ModInit glue(modInit, __LINE__) (& glue(_modInit, __LINE__), PRIORITY, MODULE_PRIORITY); \
  243. static bool glue(_modInit, __LINE__) ()
  244. // Assumes related MODULE_INIT preceded the use of MODULE_EXIT
  245. #define MODULE_EXIT() \
  246. static void glue(_modExit, __LINE__)(); \
  247. static ModExit glue(modExit, __LINE__)(& glue(_modExit, __LINE__)); \
  248. static void glue(_modExit, __LINE__)()
  249. extern jlib_decl void _InitModuleObjects();
  250. extern jlib_decl void ExitModuleObjects();
  251. extern jlib_decl void ExitModuleObjects(SoContext soCtx);
  252. #define InitModuleObjects() { _InitModuleObjects(); atexit(&ExitModuleObjects); }
  253. struct DynamicScopeCtx
  254. {
  255. DynamicScopeCtx();
  256. ~DynamicScopeCtx();
  257. void setSoContext(SoContext _soCtx) { soCtx = _soCtx; }
  258. SoContext soCtx;
  259. InitTable initTable;
  260. };
  261. #ifndef USING_MPATROL
  262. #ifdef _WIN32
  263. #ifndef _INC_CRTDBG
  264. #define _CRTDBG_MAP_ALLOC
  265. #include <crtdbg.h>
  266. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  267. #endif
  268. #endif
  269. #endif
  270. typedef CIArrayOf<StringAttrItem> StringAttrArray;
  271. typedef CIArrayOf<StringBufferItem> StringBufferArray;
  272. #endif