jscm.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 _JSCM_HPP_
  14. #define _JSCM_HPP_
  15. #undef interface
  16. #define implements public
  17. #define extends public
  18. #ifdef _MSC_VER
  19. #define interface struct __declspec(novtable)
  20. #else
  21. #define interface struct
  22. #endif
  23. #ifdef JLIB_EXPORTS
  24. #define jlib_decl DECL_EXPORT
  25. #define jlib_thrown_decl DECL_EXPORT
  26. #else
  27. #define jlib_decl DECL_IMPORT
  28. #define jlib_thrown_decl DECL_IMPORT
  29. #endif
  30. interface IInterface
  31. {
  32. virtual void Link() const = 0;
  33. virtual bool Release() const = 0;
  34. };
  35. template <class X> inline void Link(X * ptr) { if (ptr) ptr->Link(); }
  36. template <class X> inline void Release(X * ptr) { if (ptr) ptr->Release(); }
  37. #define QUERYINTERFACE(ptr, TYPE) (dynamic_cast<TYPE *>(ptr))
  38. //This base class implements a shared pointer based on a link count held in the object.
  39. //The two derived classes Owned and Linked should be used as the concrete types to construct a shared object
  40. //from a pointer.
  41. template <class CLASS> class Shared
  42. {
  43. public:
  44. inline Shared() { ptr = NULL; }
  45. inline Shared(CLASS * _ptr, bool owned) { ptr = _ptr; if (!owned && _ptr) _ptr->Link(); }
  46. inline Shared(const Shared & other) { ptr = other.getLink(); }
  47. #if defined(__cplusplus) && __cplusplus >= 201100
  48. inline Shared(Shared && other) { ptr = other.getClear(); }
  49. #endif
  50. inline ~Shared() { ::Release(ptr); }
  51. inline Shared<CLASS> & operator = (const Shared<CLASS> & other) { this->set(other.get()); return *this; }
  52. inline CLASS * operator -> () const { return ptr; }
  53. inline operator CLASS *() const { return ptr; }
  54. inline void clear() { CLASS *temp=ptr; ptr=NULL; ::Release(temp); }
  55. inline CLASS * get() const { return ptr; }
  56. inline CLASS * getClear() { CLASS * temp = ptr; ptr = NULL; return temp; }
  57. inline CLASS * getLink() const { if (ptr) ptr->Link(); return ptr; }
  58. inline void set(CLASS * _ptr)
  59. {
  60. CLASS * temp = ptr;
  61. if (temp != _ptr)
  62. {
  63. ::Link(_ptr);
  64. ptr = _ptr;
  65. ::Release(temp);
  66. }
  67. }
  68. inline void set(const Shared<CLASS> &other) { this->set(other.get()); }
  69. inline void setown(CLASS * _ptr) { CLASS * temp = ptr; ptr = _ptr; ::Release(temp); }
  70. inline void swap(Shared<CLASS> & other) { CLASS * temp = ptr; ptr = other.ptr; other.ptr = temp; }
  71. protected:
  72. inline Shared(CLASS * _ptr) { ptr = _ptr; } // deliberately protected
  73. private:
  74. inline void setown(const Shared<CLASS> &other); // illegal - going to cause a -ve leak
  75. inline Shared<CLASS> & operator = (const CLASS * other);
  76. private:
  77. CLASS * ptr;
  78. };
  79. //An Owned Shared object takes ownership of the pointer that is passed in the constructor.
  80. template <class CLASS> class Owned : public Shared<CLASS>
  81. {
  82. public:
  83. inline Owned() { }
  84. inline Owned(CLASS * _ptr) : Shared<CLASS>(_ptr) { }
  85. inline Shared<CLASS> & operator = (const Shared<CLASS> & other) { this->set(other.get()); return *this; }
  86. private:
  87. inline Owned(const Shared<CLASS> & other); // Almost certainly a bug
  88. inline Owned<CLASS> & operator = (const CLASS * other);
  89. };
  90. //A Linked Shared object takes does not take ownership of the pointer that is passed in the constructor.
  91. template <class CLASS> class Linked : public Shared<CLASS>
  92. {
  93. public:
  94. inline Linked() { }
  95. inline Linked(CLASS * _ptr) : Shared<CLASS>(LINK(_ptr)) { }
  96. inline Linked(const Shared<CLASS> & other) : Shared<CLASS>(other) { }
  97. inline Shared<CLASS> & operator = (const Shared<CLASS> & other) { this->set(other.get()); return *this; }
  98. private:
  99. inline Linked<CLASS> & operator = (const CLASS * other);
  100. };
  101. // IStringVal manages returning of arbitrary null-terminated string data between systems that may not share heap managers
  102. interface IStringVal
  103. {
  104. virtual const char * str() const = 0;
  105. virtual void set(const char * val) = 0;
  106. virtual void clear() = 0;
  107. virtual void setLen(const char * val, unsigned length) = 0;
  108. virtual unsigned length() const = 0;
  109. };
  110. // IDataVal manages returning of arbitrary unterminated binary data between systems that may not share heap managers
  111. interface IDataVal
  112. {
  113. virtual const void * data() const = 0;
  114. virtual void clear() = 0;
  115. virtual void setLen(const void * val, size_t length) = 0;
  116. virtual size_t length() const = 0;
  117. virtual void * reserve(size_t length) = 0;
  118. };
  119. // IIterator
  120. interface IIterator : extends IInterface
  121. {
  122. virtual bool first() = 0;
  123. virtual bool next() = 0;
  124. virtual bool isValid() = 0;
  125. virtual IInterface & query() = 0;
  126. virtual IInterface & get() = 0;
  127. };
  128. template <class C>
  129. interface IIteratorOf : public IInterface
  130. {
  131. public:
  132. virtual bool first() = 0;
  133. virtual bool next() = 0;
  134. virtual bool isValid() = 0;
  135. virtual C & query() = 0;
  136. C & get() { C &c = query(); c.Link(); return c; }
  137. };
  138. #define ForEach(i) for((i).first();(i).isValid();(i).next())
  139. typedef IInterface * IInterfacePtr;
  140. typedef Owned<IInterface> OwnedIInterface;
  141. typedef Linked<IInterface> LinkedIInterface;
  142. template <class X> inline X * LINK(X * ptr) { if (ptr) ptr->Link(); return ptr; }
  143. template <class X> inline X & OLINK(X & obj) { obj.Link(); return obj; }
  144. template <class X> inline X * LINK(const Shared<X> &ptr) { return ptr.getLink(); }
  145. class StringBuffer;
  146. // When changing this enum, be sure to update (a) the string functions, and (b) NUM value in jlog.hpp
  147. typedef enum
  148. {
  149. // Target audience: system admins
  150. // Purpose: Information useful for administering the platform, diagnosing errors and
  151. // resolving system issues
  152. MSGAUD_operator = 0x01,
  153. // Target audience: ECL developers
  154. // Purpose: Information useful for diagnosing and resolving issues (performance and faults)
  155. // in the execution ECL queries
  156. MSGAUD_user = 0x02,
  157. // Target audience: HPCC Platform developers
  158. // Purpose: Information related to errors relating to potential bugs or any unexpected errors that
  159. // would not be resolvable by sysadmin or ECL developers. Additional information that may
  160. // be useful for improving the platform.
  161. MSGAUD_programmer = 0x20,
  162. // MSGAUD_legacy = 0x40, REMOVED - may be reused later
  163. // Target audience: persons involved in accounting and security audits
  164. MSGAUD_audit = 0x80,
  165. // MSGAUD_all is to be used for filtering or specifying which messages are to be logged
  166. // (A message itself should not be logged as MSGAUD_all)
  167. MSGAUD_all = 0xFF
  168. } MessageAudience;
  169. interface jlib_thrown_decl IException : public IInterface
  170. {
  171. virtual int errorCode() const = 0;
  172. virtual StringBuffer & errorMessage(StringBuffer &msg) const = 0;
  173. virtual MessageAudience errorAudience() const = 0;
  174. };
  175. #endif