jlib.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #ifndef JLIB_HPP
  15. #define JLIB_HPP
  16. #include "jexpdef.hpp"
  17. #if !defined(_WIN32)
  18. #define EXPLICIT_INIT
  19. #endif
  20. #ifdef _MSC_VER
  21. //disable these throughout the system because they occur a lot
  22. #pragma warning(disable : 4275 ) // should get link errors if something is wrong...
  23. #pragma warning(disable : 4251 ) // should get link errors if something is wrong...
  24. #pragma warning(disable : 4786 ) // identifier was truncated to '255' characters in the debug information
  25. #pragma warning(disable : 4355 ) // 'this' : used in base member initializer list
  26. #endif
  27. #include "modinit.h"
  28. #include "jiface.hpp"
  29. #include <assert.h>
  30. #include "jarray.hpp"
  31. #define loop for(;;)
  32. #define _elements_in(a) (sizeof(a)/sizeof((a)[0]))
  33. #define _memclr(s, n) memset(s, 0, n)
  34. #define _clear(a) memset(&a, 0, sizeof(a))
  35. #define _copy(dest, src) memcpy(&dest, &src, sizeof(src))
  36. inline IInterface & Array__Member2Param(IInterface * src) { return *src; }
  37. inline void Array__Assign(IInterface * & dest, IInterface & src) { dest = &src; assert(dest); }
  38. inline bool Array__Equal(IInterface * const & m, const IInterface & p) { return m==&p; }
  39. inline void Array__Destroy(IInterface * & next) { if (next) next->Release(); }
  40. inline IInterface * Array__Member2ParamPtr(IInterface * src) { return src; }
  41. inline void Array__Assign(IInterface * & dest, IInterface * src) { dest = src; }
  42. inline bool Array__Equal(IInterface * const & m, const IInterface * p) { return m==p; }
  43. inline CInterface & Array__Member2Param(CInterface * src) { return *src; }
  44. inline void Array__Assign(CInterface * & dest, CInterface & src) { dest = &src; assert(dest);}
  45. inline bool Array__Equal(CInterface * const & m, const CInterface & p) { return m==&p; }
  46. inline void Array__Destroy(CInterface * & next) { if (next) next->Release(); }
  47. // Used by StringArray
  48. inline const char *Array__Member2Param(const char *src) { return src; }
  49. inline void Array__Assign(const char * & dest, const char *src) { const char *p = strdup(src); dest=p; }
  50. inline bool Array__Equal(const char *m, const char *p) { return strcmp(m, p)==0; }
  51. inline void Array__Destroy(const char *p) { free((void*) p); }
  52. MAKECopyArrayOf(IInterface *, IInterface &, jlib_decl CopyArray);
  53. MAKEArrayOf(IInterface *, IInterface &, jlib_decl Array);
  54. MAKEPtrArrayOf(IInterface *, IInterface *, jlib_decl PointerIArray);
  55. MAKECopyArrayOf(CInterface *, CInterface &, jlib_decl CopyCIArray);
  56. MAKEArrayOf(CInterface *, CInterface &, jlib_decl CIArray);
  57. MAKEValueArray(char, jlib_decl CharArray);
  58. MAKEValueArray(int, jlib_decl IntArray);
  59. MAKEValueArray(short, jlib_decl ShortArray);
  60. MAKEValueArray(float, jlib_decl FloatArray);
  61. MAKEValueArray(double, jlib_decl DoubleArray);
  62. MAKEValueArray(unsigned, jlib_decl UnsignedArray);
  63. MAKEValueArray(unsigned short, jlib_decl UnsignedShortArray);
  64. MAKEValueArray(void *, jlib_decl PointerArray);
  65. MAKEValueArray(const void *, jlib_decl ConstPointerArray);
  66. MAKEValueArray(unsigned char *,BytePointerArray);
  67. #ifdef _WIN32
  68. MAKEValueArray(bool, jlib_decl BoolArray);
  69. #else
  70. typedef CharArray BoolArray;
  71. #endif
  72. MAKEValueArray(__int64, jlib_decl Int64Array);
  73. MAKEValueArray(unsigned __int64, jlib_decl UInt64Array);
  74. template <class A, class C, class IITER>
  75. class ArrayIIteratorOf : public CInterface, implements IITER
  76. {
  77. protected:
  78. ArrayIteratorOf <A, C &> iterator;
  79. public:
  80. IMPLEMENT_IINTERFACE;
  81. ArrayIIteratorOf(A &array) : iterator(array) { }
  82. virtual bool first() { return iterator.first(); }
  83. virtual bool next() { return iterator.next(); }
  84. virtual bool isValid() { return iterator.isValid(); }
  85. virtual C & query() { return iterator.query(); }
  86. };
  87. template <class TYPE>
  88. class CIArrayOf : public CIArray
  89. {
  90. public:
  91. inline TYPE & item(aindex_t pos) const { return (TYPE &)CIArray::item(pos); }
  92. inline TYPE & popGet() { return (TYPE &)CIArray::popGet(); }
  93. inline TYPE & tos(void) const { return (TYPE &)CIArray::tos(); }
  94. inline TYPE & tos(aindex_t num) const { return (TYPE &)CIArray::tos(num); }
  95. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)CIArray::getArray(pos); }
  96. inline void append(TYPE& obj) { assert(&obj); CIArray::append(obj); }
  97. inline void appendUniq(TYPE& obj) { assert(&obj); CIArray::appendUniq(obj); }
  98. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); CIArray::add(obj, pos); }
  99. inline aindex_t find(TYPE & obj) const { assert(&obj); return CIArray::find(obj); }
  100. inline void replace(TYPE &obj, aindex_t pos, bool nodel=false) { assert(&obj); CIArray::replace(obj, pos, nodel); }
  101. inline bool zap(TYPE & obj, bool nodel=false) { assert(&obj); return CIArray::zap(obj, nodel); }
  102. };
  103. template <class TYPE>
  104. class CopyCIArrayOf : public CopyCIArray
  105. {
  106. public:
  107. inline TYPE & item(aindex_t pos) const { return (TYPE &)CopyCIArray::item(pos); }
  108. inline TYPE & pop() { return (TYPE &)CopyCIArray::pop(); }
  109. inline TYPE & tos(void) const { return (TYPE &)CopyCIArray::tos(); }
  110. inline TYPE & tos(aindex_t num) const { return (TYPE &)CopyCIArray::tos(num); }
  111. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)CopyCIArray::getArray(pos); }
  112. inline void append(TYPE& obj) { assert(&obj); CopyCIArray::append(obj); }
  113. inline void appendUniq(TYPE& obj) { assert(&obj); CopyCIArray::appendUniq(obj); }
  114. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); CopyCIArray::add(obj, pos); }
  115. inline aindex_t find(TYPE & obj) const { assert(&obj); return CopyCIArray::find(obj); }
  116. inline void replace(TYPE &obj, aindex_t pos) { assert(&obj); CopyCIArray::replace(obj, pos); }
  117. inline bool zap(TYPE & obj) { assert(&obj); return CopyCIArray::zap(obj); }
  118. };
  119. template <class TYPE>
  120. class IArrayOf : public Array
  121. {
  122. public:
  123. inline TYPE & item(aindex_t pos) const { return (TYPE &)Array::item(pos); }
  124. inline TYPE & popGet() { return (TYPE &)Array::popGet(); }
  125. inline TYPE & tos(void) const { return (TYPE &)Array::tos(); }
  126. inline TYPE & tos(aindex_t num) const { return (TYPE &)Array::tos(num); }
  127. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)Array::getArray(pos); }
  128. inline void append(TYPE& obj) { assert(&obj); Array::append(obj); }
  129. inline void appendUniq(TYPE& obj) { assert(&obj); Array::appendUniq(obj); }
  130. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); Array::add(obj, pos); }
  131. inline aindex_t find(TYPE & obj) const { assert(&obj); return Array::find(obj); }
  132. inline void replace(TYPE &obj, aindex_t pos, bool nodel=false) { assert(&obj); Array::replace(obj, pos, nodel); }
  133. inline bool zap(TYPE & obj, bool nodel=false) { assert(&obj); return Array::zap(obj, nodel); }
  134. };
  135. template <class TYPE>
  136. class ICopyArrayOf : public CopyArray
  137. {
  138. public:
  139. inline TYPE & item(aindex_t pos) const { return (TYPE &)CopyArray::item(pos); }
  140. inline TYPE & pop(bool nodel = 0) { return (TYPE &)CopyArray::pop(); }
  141. inline TYPE & tos(void) const { return (TYPE &)CopyArray::tos(); }
  142. inline TYPE & tos(aindex_t num) const { return (TYPE &)CopyArray::tos(num); }
  143. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)CopyArray::getArray(pos); }
  144. inline void append(TYPE& obj) { assert(&obj); CopyArray::append(obj); }
  145. inline void appendUniq(TYPE& obj) { assert(&obj); CopyArray::appendUniq(obj); }
  146. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); CopyArray::add(obj, pos); }
  147. inline aindex_t find(TYPE & obj) const { assert(&obj); return CopyArray::find(obj); }
  148. inline void replace(TYPE &obj, aindex_t pos) { assert(&obj); CopyArray::replace(obj, pos); }
  149. inline bool zap(TYPE & obj) { assert(&obj); return CopyArray::zap(obj); }
  150. };
  151. template <class TYPE>
  152. class PointerIArrayOf : public PointerIArray
  153. {
  154. public:
  155. inline TYPE * item(aindex_t pos) const { return (TYPE *)PointerIArray::item(pos); }
  156. inline TYPE * popGet() { return (TYPE *)PointerIArray::popGet(); }
  157. inline TYPE * tos(void) const { return (TYPE *)PointerIArray::tos(); }
  158. inline TYPE * tos(aindex_t num) const { return (TYPE *)PointerIArray::tos(num); }
  159. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)PointerIArray::getArray(pos); }
  160. inline void append(TYPE * obj) { PointerIArray::append(obj); }
  161. inline void appendUniq(TYPE * obj) { PointerIArray::appendUniq(obj); }
  162. inline void add(TYPE * obj, aindex_t pos) { PointerIArray::add(obj, pos); }
  163. inline aindex_t find(TYPE * obj) const { return PointerIArray::find(obj); }
  164. inline void replace(TYPE * obj, aindex_t pos, bool nodel=false) { PointerIArray::replace(obj, pos, nodel); }
  165. inline bool zap(TYPE * obj, bool nodel=false) { return PointerIArray::zap(obj, nodel); }
  166. };
  167. template <class TYPE>
  168. class PointerArrayOf : public PointerArray
  169. {
  170. typedef int (*PointerOfCompareFunc)(TYPE **, TYPE **);
  171. public:
  172. inline void add(TYPE * x, aindex_t pos) { PointerArray::add(x, pos); }
  173. inline void append(TYPE * x) { PointerArray::append(x); }
  174. inline aindex_t bAdd(TYPE * & newItem, PointerOfCompareFunc f, bool & isNew) { return PointerArray::bAdd(*(void * *)&newItem, (CompareFunc)f, isNew); }
  175. inline aindex_t bSearch(const TYPE * & key, CompareFunc f) const { return PointerArray:: bSearch(*(const void * *)&key, f); }
  176. inline aindex_t find(TYPE * x) const { return PointerArray::find(x); }
  177. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)PointerArray::getArray(pos); }
  178. inline TYPE * item(aindex_t pos) const { return (TYPE *)PointerArray::item(pos); }
  179. inline TYPE * pop(bool nodel = 0) { return (TYPE *)PointerArray::pop(nodel); }
  180. inline void replace(TYPE * x, aindex_t pos) { PointerArray::replace(x, pos); }
  181. inline TYPE * tos(void) const { return (TYPE *)PointerArray::tos(); }
  182. inline TYPE * tos(aindex_t num) const { return (TYPE *)PointerArray::tos(num); }
  183. inline bool zap(TYPE * x) { return PointerArray::zap(x); }
  184. };
  185. #include "jstring.hpp"
  186. #include "jarray.tpp"
  187. #include "jhash.hpp"
  188. #include "jstream.hpp"
  189. #include "jutil.hpp"
  190. inline void appendArray(CIArray & target, const CIArray & source)
  191. {
  192. unsigned max = source.ordinality();
  193. if (max)
  194. {
  195. target.ensure(target.ordinality() + max);
  196. for (unsigned i=0; i < max; ++i)
  197. target.append(OLINK(source.item(i)));
  198. }
  199. }
  200. inline void appendArray(Array & target, const Array & source)
  201. {
  202. unsigned max = source.ordinality();
  203. if (max)
  204. {
  205. target.ensure(target.ordinality() + max);
  206. for (unsigned i=0; i < max; ++i)
  207. target.append(OLINK(source.item(i)));
  208. }
  209. }
  210. typedef bool (*boolFunc)(void);
  211. typedef void (*voidFunc)(void);
  212. typedef HINSTANCE SoContext;
  213. class ModExit;
  214. enum InitializerState { ITS_Uninitialized, ITS_Initialized };
  215. struct InitializerType
  216. {
  217. boolFunc initFunc;
  218. ModExit *modExit;
  219. unsigned int priority;
  220. unsigned int modpriority;
  221. SoContext soCtx;
  222. byte state;
  223. };
  224. inline InitializerType & Array__Member2Param(InitializerType &src) { return src; }
  225. inline void Array__Assign(InitializerType & dest, InitializerType & src) { dest = src; }
  226. inline bool Array__Equal(InitializerType const & m, const InitializerType & p) { return &m==&p; }
  227. inline void Array__Destroy(InitializerType & /*next*/) { }
  228. MAKEArrayOf(InitializerType, InitializerType &, jlib_decl InitializerArray);
  229. class jlib_decl InitTable
  230. {
  231. public:
  232. InitializerArray initializers;
  233. static int sortFuncDescending(InitializerType *i1, InitializerType *i2);
  234. static int sortFuncAscending(InitializerType *i1, InitializerType *i2);
  235. public:
  236. InitTable();
  237. void init(SoContext soCtx=0);
  238. void exit(SoContext soCtx=0);
  239. void add(InitializerType &iT);
  240. void add(boolFunc func, unsigned int priority, unsigned int modpriority, byte state=ITS_Uninitialized);
  241. count_t items() { return initializers.ordinality(); }
  242. InitializerType &item(aindex_t i) { return initializers.item(i); }
  243. };
  244. class jlib_decl ModInit
  245. {
  246. public:
  247. ModInit(boolFunc func, unsigned int _priority, unsigned int _modprio);
  248. };
  249. class jlib_decl ModExit
  250. {
  251. public:
  252. ModExit(voidFunc func);
  253. #if !defined(EXPLICIT_INIT)
  254. ~ModExit();
  255. #endif
  256. voidFunc func;
  257. };
  258. #ifndef MODULE_PRIORITY
  259. #define MODULE_PRIORITY 1
  260. #endif
  261. #define __glue(a,b) a ## b
  262. #define glue(a,b) __glue(a,b)
  263. #define MODULE_INIT(PRIORITY) \
  264. static bool glue(_modInit,__LINE__) (); \
  265. static ModInit glue(modInit, __LINE__) (& glue(_modInit, __LINE__), PRIORITY, MODULE_PRIORITY); \
  266. static bool glue(_modInit, __LINE__) ()
  267. // Assumes related MODULE_INIT preceeded the use of MODULE_EXIT
  268. #define MODULE_EXIT() \
  269. static void glue(_modExit, __LINE__)(); \
  270. static ModExit glue(modExit, __LINE__)(& glue(_modExit, __LINE__)); \
  271. static void glue(_modExit, __LINE__)()
  272. extern jlib_decl void _InitModuleObjects();
  273. extern jlib_decl void ExitModuleObjects();
  274. extern jlib_decl void ExitModuleObjects(SoContext soCtx);
  275. #define InitModuleObjects() { _InitModuleObjects(); atexit(&ExitModuleObjects); }
  276. struct DynamicScopeCtx
  277. {
  278. DynamicScopeCtx();
  279. ~DynamicScopeCtx();
  280. void setSoContext(SoContext _soCtx) { soCtx = _soCtx; }
  281. SoContext soCtx;
  282. InitTable initTable;
  283. };
  284. #ifndef USING_MPATROL
  285. #ifdef _WIN32
  286. #ifndef _INC_CRTDBG
  287. #define _CRTDBG_MAP_ALLOC
  288. #include <crtdbg.h>
  289. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  290. #endif
  291. #endif
  292. #endif
  293. typedef CIArrayOf<StringAttrItem> StringAttrArray;
  294. class StringBuffer;
  295. #endif