jlib.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 _elements_in(a) (sizeof(a)/sizeof((a)[0]))
  28. #define _memclr(s, n) memset(s, 0, n)
  29. #define _clear(a) memset(&a, 0, sizeof(a))
  30. #define _copy(dest, src) memcpy(&dest, &src, sizeof(src))
  31. class jlib_decl ICopyArray : public CopyReferenceArrayOf<IInterface> {};
  32. class jlib_decl IArray : public OwnedReferenceArrayOf<IInterface> {};
  33. class jlib_decl IPointerArray : public OwnedPointerArrayOf<IInterface> {};
  34. class jlib_decl CICopyArray : public CopyReferenceArrayOf<CInterface> {};
  35. class jlib_decl CIArray : public OwnedReferenceArrayOf<CInterface> {};
  36. class jlib_decl CharArray : public ArrayOf<char> { };
  37. class jlib_decl IntArray : public ArrayOf<int> { };
  38. class jlib_decl ShortArray : public ArrayOf<short> { };
  39. class jlib_decl FloatArray : public ArrayOf<float> { };
  40. class jlib_decl DoubleArray : public ArrayOf<double> { };
  41. class jlib_decl UnsignedArray : public ArrayOf<unsigned> { };
  42. class jlib_decl UnsignedShortArray : public ArrayOf<unsigned short> { };
  43. class jlib_decl PointerArray : public ArrayOf<void *> { };
  44. class jlib_decl ConstPointerArray : public ArrayOf<const void *> { };
  45. #ifdef _WIN32
  46. class jlib_decl BoolArray : public ArrayOf<bool> { };
  47. #else
  48. typedef CharArray BoolArray;
  49. #endif
  50. class jlib_decl Int64Array : public ArrayOf<__int64> { };
  51. class jlib_decl UInt64Array : public ArrayOf<unsigned __int64> { };
  52. template <class A, class C, class IITER>
  53. class ArrayIIteratorOf : implements IITER, public CInterface
  54. {
  55. protected:
  56. ArrayIteratorOf <A, C &> iterator;
  57. public:
  58. IMPLEMENT_IINTERFACE;
  59. ArrayIIteratorOf(A &array) : iterator(array) { }
  60. virtual bool first() { return iterator.first(); }
  61. virtual bool next() { return iterator.next(); }
  62. virtual bool isValid() { return iterator.isValid(); }
  63. virtual C & query() { return iterator.query(); }
  64. };
  65. template <class TYPE>
  66. class CIArrayOf : public CIArray
  67. {
  68. public:
  69. inline TYPE & item(aindex_t pos) const { return (TYPE &)CIArray::item(pos); }
  70. inline TYPE & popGet() { return (TYPE &)CIArray::popGet(); }
  71. inline TYPE & tos(void) const { return (TYPE &)CIArray::tos(); }
  72. inline TYPE & tos(aindex_t num) const { return (TYPE &)CIArray::tos(num); }
  73. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)CIArray::getArray(pos); }
  74. inline void append(TYPE& obj) { assert(&obj); CIArray::append(obj); }
  75. inline void appendUniq(TYPE& obj) { assert(&obj); CIArray::appendUniq(obj); }
  76. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); CIArray::add(obj, pos); }
  77. inline aindex_t find(TYPE & obj) const { assert(&obj); return CIArray::find(obj); }
  78. inline void replace(TYPE &obj, aindex_t pos, bool nodel=false) { assert(&obj); CIArray::replace(obj, pos, nodel); }
  79. inline bool zap(TYPE & obj, bool nodel=false) { assert(&obj); return CIArray::zap(obj, nodel); }
  80. };
  81. template <class TYPE>
  82. class CICopyArrayOf : public CICopyArray
  83. {
  84. public:
  85. inline TYPE & item(aindex_t pos) const { return (TYPE &)CICopyArray::item(pos); }
  86. inline TYPE & popGet() { return (TYPE &)CICopyArray::popGet(); }
  87. inline TYPE & tos(void) const { return (TYPE &)CICopyArray::tos(); }
  88. inline TYPE & tos(aindex_t num) const { return (TYPE &)CICopyArray::tos(num); }
  89. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)CICopyArray::getArray(pos); }
  90. inline void append(TYPE& obj) { assert(&obj); CICopyArray::append(obj); }
  91. inline void appendUniq(TYPE& obj) { assert(&obj); CICopyArray::appendUniq(obj); }
  92. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); CICopyArray::add(obj, pos); }
  93. inline aindex_t find(TYPE & obj) const { assert(&obj); return CICopyArray::find(obj); }
  94. inline void replace(TYPE &obj, aindex_t pos) { assert(&obj); CICopyArray::replace(obj, pos); }
  95. inline bool zap(TYPE & obj) { assert(&obj); return CICopyArray::zap(obj); }
  96. };
  97. template <class TYPE>
  98. class IArrayOf : public IArray
  99. {
  100. public:
  101. inline TYPE & item(aindex_t pos) const { return (TYPE &)IArray::item(pos); }
  102. inline TYPE & popGet() { return (TYPE &)IArray::popGet(); }
  103. inline TYPE & tos(void) const { return (TYPE &)IArray::tos(); }
  104. inline TYPE & tos(aindex_t num) const { return (TYPE &)IArray::tos(num); }
  105. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)IArray::getArray(pos); }
  106. inline void append(TYPE& obj) { assert(&obj); IArray::append(obj); }
  107. inline void appendUniq(TYPE& obj) { assert(&obj); IArray::appendUniq(obj); }
  108. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); IArray::add(obj, pos); }
  109. inline aindex_t find(TYPE & obj) const { assert(&obj); return IArray::find(obj); }
  110. inline void replace(TYPE &obj, aindex_t pos, bool nodel=false) { assert(&obj); IArray::replace(obj, pos, nodel); }
  111. inline bool zap(TYPE & obj, bool nodel=false) { assert(&obj); return IArray::zap(obj, nodel); }
  112. };
  113. template <class TYPE, class BASE>
  114. class IBasedArrayOf : public IArray
  115. {
  116. public:
  117. inline TYPE & item(aindex_t pos) const { return (TYPE &)(BASE &)IArray::item(pos); }
  118. inline TYPE & popGet() { return (TYPE &)(BASE &)IArray::popGet(); }
  119. inline TYPE & tos(void) const { return (TYPE &)(BASE &)IArray::tos(); }
  120. inline TYPE & tos(aindex_t num) const { return (TYPE &)(BASE &)IArray::tos(num); }
  121. inline void append(TYPE& obj) { assert(&obj); IArray::append((BASE &)obj); }
  122. inline void appendUniq(TYPE& obj) { assert(&obj); IArray::appendUniq((BASE &)obj); }
  123. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); IArray::add((BASE &)obj, pos); }
  124. inline aindex_t find(TYPE & obj) const { assert(&obj); return IArray::find((BASE&)obj); }
  125. inline void replace(TYPE &obj, aindex_t pos, bool nodel=false) { assert(&obj); IArray::replace((BASE &)obj, pos, nodel); }
  126. inline bool zap(TYPE & obj, bool nodel=false) { assert(&obj); return IArray::zap((BASE &)obj, nodel); }
  127. };
  128. template <class TYPE>
  129. class ICopyArrayOf : public ICopyArray
  130. {
  131. public:
  132. inline TYPE & item(aindex_t pos) const { return (TYPE &)ICopyArray::item(pos); }
  133. inline TYPE & popGet() { return (TYPE &)ICopyArray::popGet(); }
  134. inline TYPE & tos(void) const { return (TYPE &)ICopyArray::tos(); }
  135. inline TYPE & tos(aindex_t num) const { return (TYPE &)ICopyArray::tos(num); }
  136. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)ICopyArray::getArray(pos); }
  137. inline void append(TYPE& obj) { assert(&obj); ICopyArray::append(obj); }
  138. inline void appendUniq(TYPE& obj) { assert(&obj); ICopyArray::appendUniq(obj); }
  139. inline void add(TYPE& obj, aindex_t pos) { assert(&obj); ICopyArray::add(obj, pos); }
  140. inline aindex_t find(TYPE & obj) const { assert(&obj); return ICopyArray::find(obj); }
  141. inline void replace(TYPE &obj, aindex_t pos) { assert(&obj); ICopyArray::replace(obj, pos); }
  142. inline bool zap(TYPE & obj) { assert(&obj); return ICopyArray::zap(obj); }
  143. };
  144. template <class TYPE>
  145. class IPointerArrayOf : public IPointerArray
  146. {
  147. public:
  148. inline TYPE * item(aindex_t pos) const { return (TYPE *)IPointerArray::item(pos); }
  149. inline TYPE * popGet() { return (TYPE *)IPointerArray::popGet(); }
  150. inline TYPE * tos(void) const { return (TYPE *)IPointerArray::tos(); }
  151. inline TYPE * tos(aindex_t num) const { return (TYPE *)IPointerArray::tos(num); }
  152. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)IPointerArray::getArray(pos); }
  153. inline void append(TYPE * obj) { IPointerArray::append(obj); }
  154. inline void appendUniq(TYPE * obj) { IPointerArray::appendUniq(obj); }
  155. inline void add(TYPE * obj, aindex_t pos) { IPointerArray::add(obj, pos); }
  156. inline aindex_t find(TYPE * obj) const { return IPointerArray::find(obj); }
  157. inline void replace(TYPE * obj, aindex_t pos, bool nodel=false) { IPointerArray::replace(obj, pos, nodel); }
  158. inline bool zap(TYPE * obj, bool nodel=false) { return IPointerArray::zap(obj, nodel); }
  159. };
  160. template <class TYPE>
  161. class PointerArrayOf : public PointerArray
  162. {
  163. typedef int (*PointerOfCompareFunc)(TYPE **, TYPE **);
  164. public:
  165. inline void add(TYPE * x, aindex_t pos) { PointerArray::add(x, pos); }
  166. inline void append(TYPE * x) { PointerArray::append(x); }
  167. inline aindex_t bAdd(TYPE * & newItem, PointerOfCompareFunc f, bool & isNew) { return PointerArray::bAdd(*(void * *)&newItem, (CompareFunc)f, isNew); }
  168. inline aindex_t bSearch(const TYPE * & key, CompareFunc f) const { return PointerArray:: bSearch(*(const void * *)&key, f); }
  169. inline aindex_t find(TYPE * x) const { return PointerArray::find(x); }
  170. inline TYPE **getArray(aindex_t pos = 0) { return (TYPE **)PointerArray::getArray(pos); }
  171. inline TYPE * item(aindex_t pos) const { return (TYPE *)PointerArray::item(pos); }
  172. inline TYPE * popGet() { return (TYPE *)PointerArray::popGet(); }
  173. inline void replace(TYPE * x, aindex_t pos) { PointerArray::replace(x, pos); }
  174. inline TYPE * tos(void) const { return (TYPE *)PointerArray::tos(); }
  175. inline TYPE * tos(aindex_t num) const { return (TYPE *)PointerArray::tos(num); }
  176. inline bool zap(TYPE * x) { return PointerArray::zap(x); }
  177. };
  178. enum DAFSConnectCfg { SSLNone = 0, SSLOnly, SSLFirst, UnsecureFirst };
  179. #include "jstring.hpp"
  180. #include "jarray.hpp"
  181. #include "jhash.hpp"
  182. #include "jstream.hpp"
  183. #include "jutil.hpp"
  184. template <class ARRAY>
  185. inline void appendArray(ARRAY & target, const ARRAY & source)
  186. {
  187. unsigned max = source.ordinality();
  188. if (max)
  189. {
  190. target.ensure(target.ordinality() + max);
  191. for (unsigned i=0; i < max; ++i)
  192. target.append(OLINK(source.item(i)));
  193. }
  194. }
  195. inline void appendArray(IArray & target, const IArray & source)
  196. {
  197. unsigned max = source.ordinality();
  198. if (max)
  199. {
  200. target.ensure(target.ordinality() + max);
  201. for (unsigned i=0; i < max; ++i)
  202. target.append(OLINK(source.item(i)));
  203. }
  204. }
  205. typedef bool (*boolFunc)(void);
  206. typedef void (*voidFunc)(void);
  207. typedef HINSTANCE SoContext;
  208. class ModExit;
  209. enum InitializerState { ITS_Uninitialized, ITS_Initialized };
  210. struct InitializerType
  211. {
  212. boolFunc initFunc;
  213. ModExit *modExit;
  214. unsigned int priority;
  215. unsigned int modpriority;
  216. SoContext soCtx;
  217. byte state;
  218. bool operator == (const InitializerType & other) const { return this == &other; }
  219. };
  220. class jlib_decl InitializerArray : public StructArrayOf<InitializerType> { };
  221. class jlib_decl InitTable
  222. {
  223. public:
  224. InitializerArray initializers;
  225. static int sortFuncDescending(InitializerType const *i1, InitializerType const *i2);
  226. static int sortFuncAscending(InitializerType const *i1, InitializerType const *i2);
  227. public:
  228. InitTable();
  229. void init(SoContext soCtx=0);
  230. void exit(SoContext soCtx=0);
  231. void add(InitializerType &iT);
  232. void add(boolFunc func, unsigned int priority, unsigned int modpriority, byte state=ITS_Uninitialized);
  233. count_t items() { return initializers.ordinality(); }
  234. InitializerType & element(aindex_t i) { return initializers.element(i); }
  235. };
  236. class jlib_decl ModInit
  237. {
  238. public:
  239. ModInit(boolFunc func, unsigned int _priority, unsigned int _modprio);
  240. };
  241. class jlib_decl ModExit
  242. {
  243. public:
  244. ModExit(voidFunc func);
  245. #if !defined(EXPLICIT_INIT)
  246. ~ModExit();
  247. #endif
  248. voidFunc func;
  249. };
  250. #ifndef MODULE_PRIORITY
  251. #define MODULE_PRIORITY 1
  252. #endif
  253. #define __glue(a,b) a ## b
  254. #define glue(a,b) __glue(a,b)
  255. #define MODULE_INIT(PRIORITY) \
  256. static bool glue(_modInit,__LINE__) (); \
  257. static ModInit glue(modInit, __LINE__) (& glue(_modInit, __LINE__), PRIORITY, MODULE_PRIORITY); \
  258. static bool glue(_modInit, __LINE__) ()
  259. // Assumes related MODULE_INIT preceded the use of MODULE_EXIT
  260. #define MODULE_EXIT() \
  261. static void glue(_modExit, __LINE__)(); \
  262. static ModExit glue(modExit, __LINE__)(& glue(_modExit, __LINE__)); \
  263. static void glue(_modExit, __LINE__)()
  264. extern jlib_decl void _InitModuleObjects();
  265. extern jlib_decl void ExitModuleObjects();
  266. extern jlib_decl void ExitModuleObjects(SoContext soCtx);
  267. #define InitModuleObjects() { _InitModuleObjects(); atexit(&ExitModuleObjects); }
  268. struct DynamicScopeCtx
  269. {
  270. DynamicScopeCtx();
  271. ~DynamicScopeCtx();
  272. void setSoContext(SoContext _soCtx) { soCtx = _soCtx; }
  273. SoContext soCtx;
  274. InitTable initTable;
  275. };
  276. #ifndef USING_MPATROL
  277. #ifdef _WIN32
  278. #ifndef _INC_CRTDBG
  279. #define _CRTDBG_MAP_ALLOC
  280. #include <crtdbg.h>
  281. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  282. #endif
  283. #endif
  284. #endif
  285. typedef CIArrayOf<StringAttrItem> StringAttrArray;
  286. typedef CIArrayOf<StringBufferItem> StringBufferArray;
  287. #endif