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