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 <algorithm>
39 #include <iostream>
40 #include <map>
41 #include <memory>
42 #include <set>
43 #include <string>
44 #include <vector>
45
46 #include <google/protobuf/stubs/port.h>
47 #include <google/protobuf/stubs/macros.h>
48 #include <google/protobuf/stubs/platform_macros.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 #include <google/protobuf/port_def.inc>
72
73 namespace std {}
74
75 namespace google {
76 namespace protobuf {
77 namespace internal {
78
79 // Some of these constants are macros rather than const ints so that they can
80 // be used in #if directives.
81
82 // The current version, represented as a single integer to make comparison
83 // easier: major * 10^6 + minor * 10^3 + micro
84 #define GOOGLE_PROTOBUF_VERSION 3009001
85
86 // A suffix string for alpha, beta or rc releases. Empty for stable releases.
87 #define GOOGLE_PROTOBUF_VERSION_SUFFIX ""
88
89 // The minimum header version which works with the current version of
90 // the library. This constant should only be used by protoc's C++ code
91 // generator.
92 static const int kMinHeaderVersionForLibrary = 3009000;
93
94 // The minimum protoc version which works with the current version of the
95 // headers.
96 #define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 3009000
97
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 = 3009000;
101
102 // Verifies that the headers and libraries are compatible. Use the macro
103 // below to call this.
104 void PROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
105 const char* filename);
106
107 // Converts a numeric version number to a string.
108 std::string PROTOBUF_EXPORT VersionString(int version);
109
110 } // namespace internal
111
112 // Place this macro in your main() function (or somewhere before you attempt
113 // to use the protobuf library) to verify that the version you link against
114 // matches the headers you compiled against. If a version mismatch is
115 // detected, the process will abort.
116 #define GOOGLE_PROTOBUF_VERIFY_VERSION \
117 ::google::protobuf::internal::VerifyVersion( \
118 GOOGLE_PROTOBUF_VERSION, GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION, \
119 __FILE__)
120
121
122 // ===================================================================
123 // from google3/util/utf8/public/unilib.h
124
125 class StringPiece;
126 namespace internal {
127
128 // Checks if the buffer contains structurally-valid UTF-8. Implemented in
129 // structurally_valid.cc.
130 PROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len);
131
IsStructurallyValidUTF8(const std::string & str)132 inline bool IsStructurallyValidUTF8(const std::string& str) {
133 return IsStructurallyValidUTF8(str.data(), static_cast<int>(str.length()));
134 }
135
136 // Returns initial number of bytes of structually valid UTF-8.
137 PROTOBUF_EXPORT int UTF8SpnStructurallyValid(const StringPiece& str);
138
139 // Coerce UTF-8 byte string in src_str to be
140 // a structurally-valid equal-length string by selectively
141 // overwriting illegal bytes with replace_char (typically ' ' or '?').
142 // replace_char must be legal printable 7-bit Ascii 0x20..0x7e.
143 // src_str is read-only.
144 //
145 // Returns pointer to output buffer, src_str.data() if no changes were made,
146 // or idst if some bytes were changed. idst is allocated by the caller
147 // and must be at least as big as src_str
148 //
149 // Optimized for: all structurally valid and no byte copying is done.
150 //
151 PROTOBUF_EXPORT char* UTF8CoerceToStructurallyValid(const StringPiece& str,
152 char* dst,
153 char replace_char);
154
155 } // namespace internal
156
157
158 // ===================================================================
159 // Shutdown support.
160
161 // Shut down the entire protocol buffers library, deleting all static-duration
162 // objects allocated by the library or by generated .pb.cc files.
163 //
164 // There are two reasons you might want to call this:
165 // * You use a draconian definition of "memory leak" in which you expect
166 // every single malloc() to have a corresponding free(), even for objects
167 // which live until program exit.
168 // * You are writing a dynamically-loaded library which needs to clean up
169 // after itself when the library is unloaded.
170 //
171 // It is safe to call this multiple times. However, it is not safe to use
172 // any other part of the protocol buffers library after
173 // ShutdownProtobufLibrary() has been called. Furthermore this call is not
174 // thread safe, user needs to synchronize multiple calls.
175 PROTOBUF_EXPORT void ShutdownProtobufLibrary();
176
177 namespace internal {
178
179 // Register a function to be called when ShutdownProtocolBuffers() is called.
180 PROTOBUF_EXPORT void OnShutdown(void (*func)());
181 // Run an arbitrary function on an arg
182 PROTOBUF_EXPORT void OnShutdownRun(void (*f)(const void*), const void* arg);
183
184 template <typename T>
OnShutdownDelete(T * p)185 T* OnShutdownDelete(T* p) {
186 OnShutdownRun([](const void* pp) { delete static_cast<const T*>(pp); }, p);
187 return p;
188 }
189
190 } // namespace internal
191
192 #if PROTOBUF_USE_EXCEPTIONS
193 class FatalException : public std::exception {
194 public:
FatalException(const char * filename,int line,const std::string & message)195 FatalException(const char* filename, int line, const std::string& message)
196 : filename_(filename), line_(line), message_(message) {}
197 virtual ~FatalException() throw();
198
199 virtual const char* what() const throw();
200
filename()201 const char* filename() const { return filename_; }
line()202 int line() const { return line_; }
message()203 const std::string& message() const { return message_; }
204
205 private:
206 const char* filename_;
207 const int line_;
208 const std::string message_;
209 };
210 #endif
211
212 // This is at the end of the file instead of the beginning to work around a bug
213 // in some versions of MSVC.
214 using std::string;
215
216 } // namespace protobuf
217 } // namespace google
218
219 #include <google/protobuf/port_undef.inc>
220
221 #endif // GOOGLE_PROTOBUF_COMMON_H__
222