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