common.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda) and others
  31. //
  32. // Contains basic types and utilities used by the rest of the library.
  33. #ifndef GOOGLE_PROTOBUF_COMMON_H__
  34. #define GOOGLE_PROTOBUF_COMMON_H__
  35. #include <algorithm>
  36. #include <iostream>
  37. #include <map>
  38. #include <memory>
  39. #include <set>
  40. #include <string>
  41. #include <vector>
  42. #include <google/protobuf/stubs/port.h>
  43. #include <google/protobuf/stubs/macros.h>
  44. #include <google/protobuf/stubs/platform_macros.h>
  45. // TODO(liujisi): Remove the following includes after the include clean-up.
  46. #include <google/protobuf/stubs/logging.h>
  47. #include <google/protobuf/stubs/mutex.h>
  48. #include <google/protobuf/stubs/callback.h>
  49. #ifndef PROTOBUF_USE_EXCEPTIONS
  50. #if defined(_MSC_VER) && defined(_CPPUNWIND)
  51. #define PROTOBUF_USE_EXCEPTIONS 1
  52. #elif defined(__EXCEPTIONS)
  53. #define PROTOBUF_USE_EXCEPTIONS 1
  54. #else
  55. #define PROTOBUF_USE_EXCEPTIONS 0
  56. #endif
  57. #endif
  58. #if PROTOBUF_USE_EXCEPTIONS
  59. #include <exception>
  60. #endif
  61. #if defined(__APPLE__)
  62. #include <TargetConditionals.h> // for TARGET_OS_IPHONE
  63. #endif
  64. #if defined(__ANDROID__) || defined(GOOGLE_PROTOBUF_OS_ANDROID) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || defined(GOOGLE_PROTOBUF_OS_IPHONE)
  65. #include <pthread.h>
  66. #endif
  67. #if defined(_WIN32) && defined(GetMessage)
  68. // Allow GetMessage to be used as a valid method name in protobuf classes.
  69. // windows.h defines GetMessage() as a macro. Let's re-define it as an inline
  70. // function. The inline function should be equivalent for C++ users.
  71. inline BOOL GetMessage_Win32(
  72. LPMSG lpMsg, HWND hWnd,
  73. UINT wMsgFilterMin, UINT wMsgFilterMax) {
  74. return GetMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
  75. }
  76. #undef GetMessage
  77. inline BOOL GetMessage(
  78. LPMSG lpMsg, HWND hWnd,
  79. UINT wMsgFilterMin, UINT wMsgFilterMax) {
  80. return GetMessage_Win32(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
  81. }
  82. #endif
  83. namespace std {}
  84. namespace google {
  85. namespace protobuf {
  86. namespace internal {
  87. // Some of these constants are macros rather than const ints so that they can
  88. // be used in #if directives.
  89. // The current version, represented as a single integer to make comparison
  90. // easier: major * 10^6 + minor * 10^3 + micro
  91. #define GOOGLE_PROTOBUF_VERSION 3006001
  92. // A suffix string for alpha, beta or rc releases. Empty for stable releases.
  93. #define GOOGLE_PROTOBUF_VERSION_SUFFIX ""
  94. // The minimum library version which works with the current version of the
  95. // headers.
  96. #define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3006001
  97. // The minimum header version which works with the current version of
  98. // the library. This constant should only be used by protoc's C++ code
  99. // generator.
  100. static const int kMinHeaderVersionForLibrary = 3006001;
  101. // The minimum protoc version which works with the current version of the
  102. // headers.
  103. #define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 3006001
  104. // The minimum header version which works with the current version of
  105. // protoc. This constant should only be used in VerifyVersion().
  106. static const int kMinHeaderVersionForProtoc = 3006001;
  107. // Verifies that the headers and libraries are compatible. Use the macro
  108. // below to call this.
  109. void LIBPROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
  110. const char* filename);
  111. // Converts a numeric version number to a string.
  112. std::string LIBPROTOBUF_EXPORT VersionString(int version);
  113. } // namespace internal
  114. // Place this macro in your main() function (or somewhere before you attempt
  115. // to use the protobuf library) to verify that the version you link against
  116. // matches the headers you compiled against. If a version mismatch is
  117. // detected, the process will abort.
  118. #define GOOGLE_PROTOBUF_VERIFY_VERSION \
  119. ::google::protobuf::internal::VerifyVersion( \
  120. GOOGLE_PROTOBUF_VERSION, GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION, \
  121. __FILE__)
  122. // ===================================================================
  123. // from google3/util/utf8/public/unilib.h
  124. class StringPiece;
  125. namespace internal {
  126. // Checks if the buffer contains structurally-valid UTF-8. Implemented in
  127. // structurally_valid.cc.
  128. LIBPROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len);
  129. inline bool IsStructurallyValidUTF8(const std::string& str) {
  130. return IsStructurallyValidUTF8(str.data(), static_cast<int>(str.length()));
  131. }
  132. // Returns initial number of bytes of structually valid UTF-8.
  133. LIBPROTOBUF_EXPORT int UTF8SpnStructurallyValid(const StringPiece& str);
  134. // Coerce UTF-8 byte string in src_str to be
  135. // a structurally-valid equal-length string by selectively
  136. // overwriting illegal bytes with replace_char (typically ' ' or '?').
  137. // replace_char must be legal printable 7-bit Ascii 0x20..0x7e.
  138. // src_str is read-only.
  139. //
  140. // Returns pointer to output buffer, src_str.data() if no changes were made,
  141. // or idst if some bytes were changed. idst is allocated by the caller
  142. // and must be at least as big as src_str
  143. //
  144. // Optimized for: all structurally valid and no byte copying is done.
  145. //
  146. LIBPROTOBUF_EXPORT char* UTF8CoerceToStructurallyValid(
  147. const StringPiece& str, char* dst, char replace_char);
  148. } // namespace internal
  149. // ===================================================================
  150. // Shutdown support.
  151. // Shut down the entire protocol buffers library, deleting all static-duration
  152. // objects allocated by the library or by generated .pb.cc files.
  153. //
  154. // There are two reasons you might want to call this:
  155. // * You use a draconian definition of "memory leak" in which you expect
  156. // every single malloc() to have a corresponding free(), even for objects
  157. // which live until program exit.
  158. // * You are writing a dynamically-loaded library which needs to clean up
  159. // after itself when the library is unloaded.
  160. //
  161. // It is safe to call this multiple times. However, it is not safe to use
  162. // any other part of the protocol buffers library after
  163. // ShutdownProtobufLibrary() has been called. Furthermore this call is not
  164. // thread safe, user needs to synchronize multiple calls.
  165. LIBPROTOBUF_EXPORT void ShutdownProtobufLibrary();
  166. namespace internal {
  167. // Register a function to be called when ShutdownProtocolBuffers() is called.
  168. LIBPROTOBUF_EXPORT void OnShutdown(void (*func)());
  169. // Run an arbitrary function on an arg
  170. LIBPROTOBUF_EXPORT void OnShutdownRun(void (*f)(const void*), const void* arg);
  171. template <typename T>
  172. T* OnShutdownDelete(T* p) {
  173. OnShutdownRun([](const void* p) { delete static_cast<const T*>(p); }, p);
  174. return p;
  175. }
  176. } // namespace internal
  177. #if PROTOBUF_USE_EXCEPTIONS
  178. class FatalException : public std::exception {
  179. public:
  180. FatalException(const char* filename, int line, const std::string& message)
  181. : filename_(filename), line_(line), message_(message) {}
  182. virtual ~FatalException() throw();
  183. virtual const char* what() const throw();
  184. const char* filename() const { return filename_; }
  185. int line() const { return line_; }
  186. const std::string& message() const { return message_; }
  187. private:
  188. const char* filename_;
  189. const int line_;
  190. const std::string message_;
  191. };
  192. #endif
  193. // This is at the end of the file instead of the beginning to work around a bug
  194. // in some versions of MSVC.
  195. using std::string;
  196. } // namespace protobuf
  197. } // namespace google
  198. #endif // GOOGLE_PROTOBUF_COMMON_H__