common.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <string>
  36. #include <google/protobuf/config.h>
  37. #include <google/protobuf/stubs/port.h>
  38. #include <google/protobuf/stubs/macros.h>
  39. #include <google/protobuf/stubs/platform_macros.h>
  40. // TODO(liujisi): Remove the following includes after the include clean-up.
  41. #include <google/protobuf/stubs/logging.h>
  42. #include <google/protobuf/stubs/scoped_ptr.h>
  43. #include <google/protobuf/stubs/mutex.h>
  44. #include <google/protobuf/stubs/callback.h>
  45. #ifndef PROTOBUF_USE_EXCEPTIONS
  46. #if defined(_MSC_VER) && defined(_CPPUNWIND)
  47. #define PROTOBUF_USE_EXCEPTIONS 1
  48. #elif defined(__EXCEPTIONS)
  49. #define PROTOBUF_USE_EXCEPTIONS 1
  50. #else
  51. #define PROTOBUF_USE_EXCEPTIONS 0
  52. #endif
  53. #endif
  54. #if PROTOBUF_USE_EXCEPTIONS
  55. #include <exception>
  56. #endif
  57. #if defined(__APPLE__)
  58. #include <TargetConditionals.h> // for TARGET_OS_IPHONE
  59. #endif
  60. #if defined(__ANDROID__) || defined(GOOGLE_PROTOBUF_OS_ANDROID) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || defined(GOOGLE_PROTOBUF_OS_IPHONE)
  61. #include <pthread.h>
  62. #endif
  63. #if defined(_WIN32) && defined(GetMessage)
  64. // Allow GetMessage to be used as a valid method name in protobuf classes.
  65. // windows.h defines GetMessage() as a macro. Let's re-define it as an inline
  66. // function. The inline function should be equivalent for C++ users.
  67. inline BOOL GetMessage_Win32(
  68. LPMSG lpMsg, HWND hWnd,
  69. UINT wMsgFilterMin, UINT wMsgFilterMax) {
  70. return GetMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
  71. }
  72. #undef GetMessage
  73. inline BOOL GetMessage(
  74. LPMSG lpMsg, HWND hWnd,
  75. UINT wMsgFilterMin, UINT wMsgFilterMax) {
  76. return GetMessage_Win32(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
  77. }
  78. #endif
  79. namespace std {}
  80. namespace google {
  81. namespace protobuf {
  82. namespace internal {
  83. // Some of these constants are macros rather than const ints so that they can
  84. // be used in #if directives.
  85. // The current version, represented as a single integer to make comparison
  86. // easier: major * 10^6 + minor * 10^3 + micro
  87. #define GOOGLE_PROTOBUF_VERSION 3000000
  88. // The minimum library version which works with the current version of the
  89. // headers.
  90. #define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3000000
  91. // The minimum header version which works with the current version of
  92. // the library. This constant should only be used by protoc's C++ code
  93. // generator.
  94. static const int kMinHeaderVersionForLibrary = 3000000;
  95. // The minimum protoc version which works with the current version of the
  96. // headers.
  97. #define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 3000000
  98. // The minimum header version which works with the current version of
  99. // protoc. This constant should only be used in VerifyVersion().
  100. static const int kMinHeaderVersionForProtoc = 3000000;
  101. // Verifies that the headers and libraries are compatible. Use the macro
  102. // below to call this.
  103. void LIBPROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
  104. const char* filename);
  105. // Converts a numeric version number to a string.
  106. std::string LIBPROTOBUF_EXPORT VersionString(int version);
  107. } // namespace internal
  108. // Place this macro in your main() function (or somewhere before you attempt
  109. // to use the protobuf library) to verify that the version you link against
  110. // matches the headers you compiled against. If a version mismatch is
  111. // detected, the process will abort.
  112. #define GOOGLE_PROTOBUF_VERIFY_VERSION \
  113. ::google::protobuf::internal::VerifyVersion( \
  114. GOOGLE_PROTOBUF_VERSION, GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION, \
  115. __FILE__)
  116. // ===================================================================
  117. // from google3/util/utf8/public/unilib.h
  118. class StringPiece;
  119. namespace internal {
  120. // Checks if the buffer contains structurally-valid UTF-8. Implemented in
  121. // structurally_valid.cc.
  122. LIBPROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len);
  123. inline bool IsStructurallyValidUTF8(const std::string& str) {
  124. return IsStructurallyValidUTF8(str.data(), str.length());
  125. }
  126. // Returns initial number of bytes of structually valid UTF-8.
  127. LIBPROTOBUF_EXPORT int UTF8SpnStructurallyValid(const StringPiece& str);
  128. // Coerce UTF-8 byte string in src_str to be
  129. // a structurally-valid equal-length string by selectively
  130. // overwriting illegal bytes with replace_char (typically ' ' or '?').
  131. // replace_char must be legal printable 7-bit Ascii 0x20..0x7e.
  132. // src_str is read-only.
  133. //
  134. // Returns pointer to output buffer, src_str.data() if no changes were made,
  135. // or idst if some bytes were changed. idst is allocated by the caller
  136. // and must be at least as big as src_str
  137. //
  138. // Optimized for: all structurally valid and no byte copying is done.
  139. //
  140. LIBPROTOBUF_EXPORT char* UTF8CoerceToStructurallyValid(
  141. const StringPiece& str, char* dst, char replace_char);
  142. } // namespace internal
  143. // ===================================================================
  144. // Shutdown support.
  145. // Shut down the entire protocol buffers library, deleting all static-duration
  146. // objects allocated by the library or by generated .pb.cc files.
  147. //
  148. // There are two reasons you might want to call this:
  149. // * You use a draconian definition of "memory leak" in which you expect
  150. // every single malloc() to have a corresponding free(), even for objects
  151. // which live until program exit.
  152. // * You are writing a dynamically-loaded library which needs to clean up
  153. // after itself when the library is unloaded.
  154. //
  155. // It is safe to call this multiple times. However, it is not safe to use
  156. // any other part of the protocol buffers library after
  157. // ShutdownProtobufLibrary() has been called.
  158. LIBPROTOBUF_EXPORT void ShutdownProtobufLibrary();
  159. namespace internal {
  160. // Register a function to be called when ShutdownProtocolBuffers() is called.
  161. LIBPROTOBUF_EXPORT void OnShutdown(void (*func)());
  162. } // namespace internal
  163. #if PROTOBUF_USE_EXCEPTIONS
  164. class FatalException : public std::exception {
  165. public:
  166. FatalException(const char* filename, int line, const std::string& message)
  167. : filename_(filename), line_(line), message_(message) {}
  168. virtual ~FatalException() throw();
  169. virtual const char* what() const throw();
  170. const char* filename() const { return filename_; }
  171. int line() const { return line_; }
  172. const std::string& message() const { return message_; }
  173. private:
  174. const char* filename_;
  175. const int line_;
  176. const std::string message_;
  177. };
  178. #endif
  179. // This is at the end of the file instead of the beginning to work around a bug
  180. // in some versions of MSVC.
  181. using namespace std; // Don't do this at home, kids.
  182. } // namespace protobuf
  183. } // namespace google
  184. #endif // GOOGLE_PROTOBUF_COMMON_H__