add external dependencies in a pre-built way to avoid incompatibilities

This commit is contained in:
Sven Czarnian
2021-08-09 22:07:00 +02:00
parent 5f702016ad
commit 4cde0f9baf
197 changed files with 74244 additions and 0 deletions

View File

@@ -0,0 +1,351 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// This file declares the ByteSink and ByteSource abstract interfaces. These
// interfaces represent objects that consume (ByteSink) or produce (ByteSource)
// a sequence of bytes. Using these abstract interfaces in your APIs can help
// make your code work with a variety of input and output types.
//
// This file also declares the following commonly used implementations of these
// interfaces.
//
// ByteSink:
// UncheckedArrayByteSink Writes to an array, without bounds checking
// CheckedArrayByteSink Writes to an array, with bounds checking
// GrowingArrayByteSink Allocates and writes to a growable buffer
// StringByteSink Writes to an STL string
// NullByteSink Consumes a never-ending stream of bytes
//
// ByteSource:
// ArrayByteSource Reads from an array or string/StringPiece
// LimitedByteSource Limits the number of bytes read from an
#ifndef GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_
#define GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_
#include <stddef.h>
#include <string>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/stringpiece.h>
#include <google/protobuf/port_def.inc>
class CordByteSink;
namespace google {
namespace protobuf {
namespace strings {
// An abstract interface for an object that consumes a sequence of bytes. This
// interface offers a way to append data as well as a Flush() function.
//
// Example:
//
// string my_data;
// ...
// ByteSink* sink = ...
// sink->Append(my_data.data(), my_data.size());
// sink->Flush();
//
class PROTOBUF_EXPORT ByteSink {
public:
ByteSink() {}
virtual ~ByteSink() {}
// Appends the "n" bytes starting at "bytes".
virtual void Append(const char* bytes, size_t n) = 0;
// Flushes internal buffers. The default implementation does nothing. ByteSink
// subclasses may use internal buffers that require calling Flush() at the end
// of the stream.
virtual void Flush();
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ByteSink);
};
// An abstract interface for an object that produces a fixed-size sequence of
// bytes.
//
// Example:
//
// ByteSource* source = ...
// while (source->Available() > 0) {
// StringPiece data = source->Peek();
// ... do something with "data" ...
// source->Skip(data.length());
// }
//
class PROTOBUF_EXPORT ByteSource {
public:
ByteSource() {}
virtual ~ByteSource() {}
// Returns the number of bytes left to read from the source. Available()
// should decrease by N each time Skip(N) is called. Available() may not
// increase. Available() returning 0 indicates that the ByteSource is
// exhausted.
//
// Note: Size() may have been a more appropriate name as it's more
// indicative of the fixed-size nature of a ByteSource.
virtual size_t Available() const = 0;
// Returns a StringPiece of the next contiguous region of the source. Does not
// reposition the source. The returned region is empty iff Available() == 0.
//
// The returned region is valid until the next call to Skip() or until this
// object is destroyed, whichever occurs first.
//
// The length of the returned StringPiece will be <= Available().
virtual StringPiece Peek() = 0;
// Skips the next n bytes. Invalidates any StringPiece returned by a previous
// call to Peek().
//
// REQUIRES: Available() >= n
virtual void Skip(size_t n) = 0;
// Writes the next n bytes in this ByteSource to the given ByteSink, and
// advances this ByteSource past the copied bytes. The default implementation
// of this method just copies the bytes normally, but subclasses might
// override CopyTo to optimize certain cases.
//
// REQUIRES: Available() >= n
virtual void CopyTo(ByteSink* sink, size_t n);
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ByteSource);
};
//
// Some commonly used implementations of ByteSink
//
// Implementation of ByteSink that writes to an unsized byte array. No
// bounds-checking is performed--it is the caller's responsibility to ensure
// that the destination array is large enough.
//
// Example:
//
// char buf[10];
// UncheckedArrayByteSink sink(buf);
// sink.Append("hi", 2); // OK
// sink.Append(data, 100); // WOOPS! Overflows buf[10].
//
class PROTOBUF_EXPORT UncheckedArrayByteSink : public ByteSink {
public:
explicit UncheckedArrayByteSink(char* dest) : dest_(dest) {}
virtual void Append(const char* data, size_t n) override;
// Returns the current output pointer so that a caller can see how many bytes
// were produced.
//
// Note: this method is not part of the ByteSink interface.
char* CurrentDestination() const { return dest_; }
private:
char* dest_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UncheckedArrayByteSink);
};
// Implementation of ByteSink that writes to a sized byte array. This sink will
// not write more than "capacity" bytes to outbuf. Once "capacity" bytes are
// appended, subsequent bytes will be ignored and Overflowed() will return true.
// Overflowed() does not cause a runtime error (i.e., it does not CHECK fail).
//
// Example:
//
// char buf[10];
// CheckedArrayByteSink sink(buf, 10);
// sink.Append("hi", 2); // OK
// sink.Append(data, 100); // Will only write 8 more bytes
//
class PROTOBUF_EXPORT CheckedArrayByteSink : public ByteSink {
public:
CheckedArrayByteSink(char* outbuf, size_t capacity);
virtual void Append(const char* bytes, size_t n) override;
// Returns the number of bytes actually written to the sink.
size_t NumberOfBytesWritten() const { return size_; }
// Returns true if any bytes were discarded, i.e., if there was an
// attempt to write more than 'capacity' bytes.
bool Overflowed() const { return overflowed_; }
private:
char* outbuf_;
const size_t capacity_;
size_t size_;
bool overflowed_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CheckedArrayByteSink);
};
// Implementation of ByteSink that allocates an internal buffer (a char array)
// and expands it as needed to accommodate appended data (similar to a string),
// and allows the caller to take ownership of the internal buffer via the
// GetBuffer() method. The buffer returned from GetBuffer() must be deleted by
// the caller with delete[]. GetBuffer() also sets the internal buffer to be
// empty, and subsequent appends to the sink will create a new buffer. The
// destructor will free the internal buffer if GetBuffer() was not called.
//
// Example:
//
// GrowingArrayByteSink sink(10);
// sink.Append("hi", 2);
// sink.Append(data, n);
// const char* buf = sink.GetBuffer(); // Ownership transferred
// delete[] buf;
//
class PROTOBUF_EXPORT GrowingArrayByteSink : public strings::ByteSink {
public:
explicit GrowingArrayByteSink(size_t estimated_size);
virtual ~GrowingArrayByteSink();
virtual void Append(const char* bytes, size_t n) override;
// Returns the allocated buffer, and sets nbytes to its size. The caller takes
// ownership of the buffer and must delete it with delete[].
char* GetBuffer(size_t* nbytes);
private:
void Expand(size_t amount);
void ShrinkToFit();
size_t capacity_;
char* buf_;
size_t size_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GrowingArrayByteSink);
};
// Implementation of ByteSink that appends to the given string.
// Existing contents of "dest" are not modified; new data is appended.
//
// Example:
//
// string dest = "Hello ";
// StringByteSink sink(&dest);
// sink.Append("World", 5);
// assert(dest == "Hello World");
//
class PROTOBUF_EXPORT StringByteSink : public ByteSink {
public:
explicit StringByteSink(std::string* dest) : dest_(dest) {}
virtual void Append(const char* data, size_t n) override;
private:
std::string* dest_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringByteSink);
};
// Implementation of ByteSink that discards all data.
//
// Example:
//
// NullByteSink sink;
// sink.Append(data, data.size()); // All data ignored.
//
class PROTOBUF_EXPORT NullByteSink : public ByteSink {
public:
NullByteSink() {}
void Append(const char* /*data*/, size_t /*n*/) override {}
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(NullByteSink);
};
//
// Some commonly used implementations of ByteSource
//
// Implementation of ByteSource that reads from a StringPiece.
//
// Example:
//
// string data = "Hello";
// ArrayByteSource source(data);
// assert(source.Available() == 5);
// assert(source.Peek() == "Hello");
//
class PROTOBUF_EXPORT ArrayByteSource : public ByteSource {
public:
explicit ArrayByteSource(StringPiece s) : input_(s) {}
virtual size_t Available() const override;
virtual StringPiece Peek() override;
virtual void Skip(size_t n) override;
private:
StringPiece input_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayByteSource);
};
// Implementation of ByteSource that wraps another ByteSource, limiting the
// number of bytes returned.
//
// The caller maintains ownership of the underlying source, and may not use the
// underlying source while using the LimitByteSource object. The underlying
// source's pointer is advanced by n bytes every time this LimitByteSource
// object is advanced by n.
//
// Example:
//
// string data = "Hello World";
// ArrayByteSource abs(data);
// assert(abs.Available() == data.size());
//
// LimitByteSource limit(abs, 5);
// assert(limit.Available() == 5);
// assert(limit.Peek() == "Hello");
//
class PROTOBUF_EXPORT LimitByteSource : public ByteSource {
public:
// Returns at most "limit" bytes from "source".
LimitByteSource(ByteSource* source, size_t limit);
virtual size_t Available() const override;
virtual StringPiece Peek() override;
virtual void Skip(size_t n) override;
// We override CopyTo so that we can forward to the underlying source, in
// case it has an efficient implementation of CopyTo.
virtual void CopyTo(ByteSink* sink, size_t n) override;
private:
ByteSource* source_;
size_t limit_;
};
} // namespace strings
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_

View File

@@ -0,0 +1,583 @@
#ifndef GOOGLE_PROTOBUF_STUBS_CALLBACK_H_
#define GOOGLE_PROTOBUF_STUBS_CALLBACK_H_
#include <type_traits>
#include <google/protobuf/stubs/macros.h>
#include <google/protobuf/port_def.inc>
// ===================================================================
// emulates google3/base/callback.h
namespace google {
namespace protobuf {
// Abstract interface for a callback. When calling an RPC, you must provide
// a Closure to call when the procedure completes. See the Service interface
// in service.h.
//
// To automatically construct a Closure which calls a particular function or
// method with a particular set of parameters, use the NewCallback() function.
// Example:
// void FooDone(const FooResponse* response) {
// ...
// }
//
// void CallFoo() {
// ...
// // When done, call FooDone() and pass it a pointer to the response.
// Closure* callback = NewCallback(&FooDone, response);
// // Make the call.
// service->Foo(controller, request, response, callback);
// }
//
// Example that calls a method:
// class Handler {
// public:
// ...
//
// void FooDone(const FooResponse* response) {
// ...
// }
//
// void CallFoo() {
// ...
// // When done, call FooDone() and pass it a pointer to the response.
// Closure* callback = NewCallback(this, &Handler::FooDone, response);
// // Make the call.
// service->Foo(controller, request, response, callback);
// }
// };
//
// Currently NewCallback() supports binding zero, one, or two arguments.
//
// Callbacks created with NewCallback() automatically delete themselves when
// executed. They should be used when a callback is to be called exactly
// once (usually the case with RPC callbacks). If a callback may be called
// a different number of times (including zero), create it with
// NewPermanentCallback() instead. You are then responsible for deleting the
// callback (using the "delete" keyword as normal).
//
// Note that NewCallback() is a bit touchy regarding argument types. Generally,
// the values you provide for the parameter bindings must exactly match the
// types accepted by the callback function. For example:
// void Foo(std::string s);
// NewCallback(&Foo, "foo"); // WON'T WORK: const char* != string
// NewCallback(&Foo, std::string("foo")); // WORKS
// Also note that the arguments cannot be references:
// void Foo(const std::string& s);
// std::string my_str;
// NewCallback(&Foo, my_str); // WON'T WORK: Can't use references.
// However, correctly-typed pointers will work just fine.
class PROTOBUF_EXPORT Closure {
public:
Closure() {}
virtual ~Closure();
virtual void Run() = 0;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Closure);
};
template<typename R>
class ResultCallback {
public:
ResultCallback() {}
virtual ~ResultCallback() {}
virtual R Run() = 0;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback);
};
template <typename R, typename A1>
class PROTOBUF_EXPORT ResultCallback1 {
public:
ResultCallback1() {}
virtual ~ResultCallback1() {}
virtual R Run(A1) = 0;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback1);
};
template <typename R, typename A1, typename A2>
class PROTOBUF_EXPORT ResultCallback2 {
public:
ResultCallback2() {}
virtual ~ResultCallback2() {}
virtual R Run(A1,A2) = 0;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback2);
};
namespace internal {
class PROTOBUF_EXPORT FunctionClosure0 : public Closure {
public:
typedef void (*FunctionType)();
FunctionClosure0(FunctionType function, bool self_deleting)
: function_(function), self_deleting_(self_deleting) {}
~FunctionClosure0();
void Run() override {
bool needs_delete = self_deleting_; // read in case callback deletes
function_();
if (needs_delete) delete this;
}
private:
FunctionType function_;
bool self_deleting_;
};
template <typename Class>
class MethodClosure0 : public Closure {
public:
typedef void (Class::*MethodType)();
MethodClosure0(Class* object, MethodType method, bool self_deleting)
: object_(object), method_(method), self_deleting_(self_deleting) {}
~MethodClosure0() {}
void Run() override {
bool needs_delete = self_deleting_; // read in case callback deletes
(object_->*method_)();
if (needs_delete) delete this;
}
private:
Class* object_;
MethodType method_;
bool self_deleting_;
};
template <typename Arg1>
class FunctionClosure1 : public Closure {
public:
typedef void (*FunctionType)(Arg1 arg1);
FunctionClosure1(FunctionType function, bool self_deleting,
Arg1 arg1)
: function_(function), self_deleting_(self_deleting),
arg1_(arg1) {}
~FunctionClosure1() {}
void Run() override {
bool needs_delete = self_deleting_; // read in case callback deletes
function_(arg1_);
if (needs_delete) delete this;
}
private:
FunctionType function_;
bool self_deleting_;
Arg1 arg1_;
};
template <typename Class, typename Arg1>
class MethodClosure1 : public Closure {
public:
typedef void (Class::*MethodType)(Arg1 arg1);
MethodClosure1(Class* object, MethodType method, bool self_deleting,
Arg1 arg1)
: object_(object), method_(method), self_deleting_(self_deleting),
arg1_(arg1) {}
~MethodClosure1() {}
void Run() override {
bool needs_delete = self_deleting_; // read in case callback deletes
(object_->*method_)(arg1_);
if (needs_delete) delete this;
}
private:
Class* object_;
MethodType method_;
bool self_deleting_;
Arg1 arg1_;
};
template <typename Arg1, typename Arg2>
class FunctionClosure2 : public Closure {
public:
typedef void (*FunctionType)(Arg1 arg1, Arg2 arg2);
FunctionClosure2(FunctionType function, bool self_deleting,
Arg1 arg1, Arg2 arg2)
: function_(function), self_deleting_(self_deleting),
arg1_(arg1), arg2_(arg2) {}
~FunctionClosure2() {}
void Run() override {
bool needs_delete = self_deleting_; // read in case callback deletes
function_(arg1_, arg2_);
if (needs_delete) delete this;
}
private:
FunctionType function_;
bool self_deleting_;
Arg1 arg1_;
Arg2 arg2_;
};
template <typename Class, typename Arg1, typename Arg2>
class MethodClosure2 : public Closure {
public:
typedef void (Class::*MethodType)(Arg1 arg1, Arg2 arg2);
MethodClosure2(Class* object, MethodType method, bool self_deleting,
Arg1 arg1, Arg2 arg2)
: object_(object), method_(method), self_deleting_(self_deleting),
arg1_(arg1), arg2_(arg2) {}
~MethodClosure2() {}
void Run() override {
bool needs_delete = self_deleting_; // read in case callback deletes
(object_->*method_)(arg1_, arg2_);
if (needs_delete) delete this;
}
private:
Class* object_;
MethodType method_;
bool self_deleting_;
Arg1 arg1_;
Arg2 arg2_;
};
template<typename R>
class FunctionResultCallback_0_0 : public ResultCallback<R> {
public:
typedef R (*FunctionType)();
FunctionResultCallback_0_0(FunctionType function, bool self_deleting)
: function_(function), self_deleting_(self_deleting) {}
~FunctionResultCallback_0_0() {}
R Run() override {
bool needs_delete = self_deleting_; // read in case callback deletes
R result = function_();
if (needs_delete) delete this;
return result;
}
private:
FunctionType function_;
bool self_deleting_;
};
template<typename R, typename P1>
class FunctionResultCallback_1_0 : public ResultCallback<R> {
public:
typedef R (*FunctionType)(P1);
FunctionResultCallback_1_0(FunctionType function, bool self_deleting,
P1 p1)
: function_(function), self_deleting_(self_deleting), p1_(p1) {}
~FunctionResultCallback_1_0() {}
R Run() override {
bool needs_delete = self_deleting_; // read in case callback deletes
R result = function_(p1_);
if (needs_delete) delete this;
return result;
}
private:
FunctionType function_;
bool self_deleting_;
P1 p1_;
};
template<typename R, typename Arg1>
class FunctionResultCallback_0_1 : public ResultCallback1<R, Arg1> {
public:
typedef R (*FunctionType)(Arg1 arg1);
FunctionResultCallback_0_1(FunctionType function, bool self_deleting)
: function_(function), self_deleting_(self_deleting) {}
~FunctionResultCallback_0_1() {}
R Run(Arg1 a1) override {
bool needs_delete = self_deleting_; // read in case callback deletes
R result = function_(a1);
if (needs_delete) delete this;
return result;
}
private:
FunctionType function_;
bool self_deleting_;
};
template<typename R, typename P1, typename A1>
class FunctionResultCallback_1_1 : public ResultCallback1<R, A1> {
public:
typedef R (*FunctionType)(P1, A1);
FunctionResultCallback_1_1(FunctionType function, bool self_deleting,
P1 p1)
: function_(function), self_deleting_(self_deleting), p1_(p1) {}
~FunctionResultCallback_1_1() {}
R Run(A1 a1) override {
bool needs_delete = self_deleting_; // read in case callback deletes
R result = function_(p1_, a1);
if (needs_delete) delete this;
return result;
}
private:
FunctionType function_;
bool self_deleting_;
P1 p1_;
};
template <typename T>
struct InternalConstRef {
typedef typename std::remove_reference<T>::type base_type;
typedef const base_type& type;
};
template<typename R, typename T>
class MethodResultCallback_0_0 : public ResultCallback<R> {
public:
typedef R (T::*MethodType)();
MethodResultCallback_0_0(T* object, MethodType method, bool self_deleting)
: object_(object),
method_(method),
self_deleting_(self_deleting) {}
~MethodResultCallback_0_0() {}
R Run() {
bool needs_delete = self_deleting_;
R result = (object_->*method_)();
if (needs_delete) delete this;
return result;
}
private:
T* object_;
MethodType method_;
bool self_deleting_;
};
template <typename R, typename T, typename P1, typename P2, typename P3,
typename P4, typename P5, typename P6, typename A1, typename A2>
class MethodResultCallback_6_2 : public ResultCallback2<R, A1, A2> {
public:
typedef R (T::*MethodType)(P1, P2, P3, P4, P5, P6, A1, A2);
MethodResultCallback_6_2(T* object, MethodType method, bool self_deleting,
P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
: object_(object),
method_(method),
self_deleting_(self_deleting),
p1_(p1),
p2_(p2),
p3_(p3),
p4_(p4),
p5_(p5),
p6_(p6) {}
~MethodResultCallback_6_2() {}
R Run(A1 a1, A2 a2) override {
bool needs_delete = self_deleting_;
R result = (object_->*method_)(p1_, p2_, p3_, p4_, p5_, p6_, a1, a2);
if (needs_delete) delete this;
return result;
}
private:
T* object_;
MethodType method_;
bool self_deleting_;
typename std::remove_reference<P1>::type p1_;
typename std::remove_reference<P2>::type p2_;
typename std::remove_reference<P3>::type p3_;
typename std::remove_reference<P4>::type p4_;
typename std::remove_reference<P5>::type p5_;
typename std::remove_reference<P6>::type p6_;
};
} // namespace internal
// See Closure.
inline Closure* NewCallback(void (*function)()) {
return new internal::FunctionClosure0(function, true);
}
// See Closure.
inline Closure* NewPermanentCallback(void (*function)()) {
return new internal::FunctionClosure0(function, false);
}
// See Closure.
template <typename Class>
inline Closure* NewCallback(Class* object, void (Class::*method)()) {
return new internal::MethodClosure0<Class>(object, method, true);
}
// See Closure.
template <typename Class>
inline Closure* NewPermanentCallback(Class* object, void (Class::*method)()) {
return new internal::MethodClosure0<Class>(object, method, false);
}
// See Closure.
template <typename Arg1>
inline Closure* NewCallback(void (*function)(Arg1),
Arg1 arg1) {
return new internal::FunctionClosure1<Arg1>(function, true, arg1);
}
// See Closure.
template <typename Arg1>
inline Closure* NewPermanentCallback(void (*function)(Arg1),
Arg1 arg1) {
return new internal::FunctionClosure1<Arg1>(function, false, arg1);
}
// See Closure.
template <typename Class, typename Arg1>
inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1),
Arg1 arg1) {
return new internal::MethodClosure1<Class, Arg1>(object, method, true, arg1);
}
// See Closure.
template <typename Class, typename Arg1>
inline Closure* NewPermanentCallback(Class* object, void (Class::*method)(Arg1),
Arg1 arg1) {
return new internal::MethodClosure1<Class, Arg1>(object, method, false, arg1);
}
// See Closure.
template <typename Arg1, typename Arg2>
inline Closure* NewCallback(void (*function)(Arg1, Arg2),
Arg1 arg1, Arg2 arg2) {
return new internal::FunctionClosure2<Arg1, Arg2>(
function, true, arg1, arg2);
}
// See Closure.
template <typename Arg1, typename Arg2>
inline Closure* NewPermanentCallback(void (*function)(Arg1, Arg2),
Arg1 arg1, Arg2 arg2) {
return new internal::FunctionClosure2<Arg1, Arg2>(
function, false, arg1, arg2);
}
// See Closure.
template <typename Class, typename Arg1, typename Arg2>
inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1, Arg2),
Arg1 arg1, Arg2 arg2) {
return new internal::MethodClosure2<Class, Arg1, Arg2>(
object, method, true, arg1, arg2);
}
// See Closure.
template <typename Class, typename Arg1, typename Arg2>
inline Closure* NewPermanentCallback(
Class* object, void (Class::*method)(Arg1, Arg2),
Arg1 arg1, Arg2 arg2) {
return new internal::MethodClosure2<Class, Arg1, Arg2>(
object, method, false, arg1, arg2);
}
// See ResultCallback
template<typename R>
inline ResultCallback<R>* NewCallback(R (*function)()) {
return new internal::FunctionResultCallback_0_0<R>(function, true);
}
// See ResultCallback
template<typename R>
inline ResultCallback<R>* NewPermanentCallback(R (*function)()) {
return new internal::FunctionResultCallback_0_0<R>(function, false);
}
// See ResultCallback
template<typename R, typename P1>
inline ResultCallback<R>* NewCallback(R (*function)(P1), P1 p1) {
return new internal::FunctionResultCallback_1_0<R, P1>(
function, true, p1);
}
// See ResultCallback
template<typename R, typename P1>
inline ResultCallback<R>* NewPermanentCallback(
R (*function)(P1), P1 p1) {
return new internal::FunctionResultCallback_1_0<R, P1>(
function, false, p1);
}
// See ResultCallback1
template<typename R, typename A1>
inline ResultCallback1<R, A1>* NewCallback(R (*function)(A1)) {
return new internal::FunctionResultCallback_0_1<R, A1>(function, true);
}
// See ResultCallback1
template<typename R, typename A1>
inline ResultCallback1<R, A1>* NewPermanentCallback(R (*function)(A1)) {
return new internal::FunctionResultCallback_0_1<R, A1>(function, false);
}
// See ResultCallback1
template<typename R, typename P1, typename A1>
inline ResultCallback1<R, A1>* NewCallback(R (*function)(P1, A1), P1 p1) {
return new internal::FunctionResultCallback_1_1<R, P1, A1>(
function, true, p1);
}
// See ResultCallback1
template<typename R, typename P1, typename A1>
inline ResultCallback1<R, A1>* NewPermanentCallback(
R (*function)(P1, A1), P1 p1) {
return new internal::FunctionResultCallback_1_1<R, P1, A1>(
function, false, p1);
}
// See MethodResultCallback_0_0
template <typename R, typename T1, typename T2>
inline ResultCallback<R>* NewPermanentCallback(
T1* object, R (T2::*function)()) {
return new internal::MethodResultCallback_0_0<R, T1>(object, function, false);
}
// See MethodResultCallback_6_2
template <typename R, typename T, typename P1, typename P2, typename P3,
typename P4, typename P5, typename P6, typename A1, typename A2>
inline ResultCallback2<R, A1, A2>* NewPermanentCallback(
T* object, R (T::*function)(P1, P2, P3, P4, P5, P6, A1, A2),
typename internal::InternalConstRef<P1>::type p1,
typename internal::InternalConstRef<P2>::type p2,
typename internal::InternalConstRef<P3>::type p3,
typename internal::InternalConstRef<P4>::type p4,
typename internal::InternalConstRef<P5>::type p5,
typename internal::InternalConstRef<P6>::type p6) {
return new internal::MethodResultCallback_6_2<R, T, P1, P2, P3, P4, P5, P6,
A1, A2>(object, function, false,
p1, p2, p3, p4, p5, p6);
}
// A function which does nothing. Useful for creating no-op callbacks, e.g.:
// Closure* nothing = NewCallback(&DoNothing);
void PROTOBUF_EXPORT DoNothing();
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_STUBS_CALLBACK_H_

View File

@@ -0,0 +1,138 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2014 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef GOOGLE_PROTOBUF_CASTS_H__
#define GOOGLE_PROTOBUF_CASTS_H__
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/port_def.inc>
#include <type_traits>
namespace google {
namespace protobuf {
namespace internal {
// Use implicit_cast as a safe version of static_cast or const_cast
// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
// a const pointer to Foo).
// When you use implicit_cast, the compiler checks that the cast is safe.
// Such explicit implicit_casts are necessary in surprisingly many
// situations where C++ demands an exact type match instead of an
// argument type convertible to a target type.
//
// The From type can be inferred, so the preferred syntax for using
// implicit_cast is the same as for static_cast etc.:
//
// implicit_cast<ToType>(expr)
//
// implicit_cast would have been part of the C++ standard library,
// but the proposal was submitted too late. It will probably make
// its way into the language in the future.
template<typename To, typename From>
inline To implicit_cast(From const &f) {
return f;
}
// When you upcast (that is, cast a pointer from type Foo to type
// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
// always succeed. When you downcast (that is, cast a pointer from
// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
// how do you know the pointer is really of type SubclassOfFoo? It
// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
// when you downcast, you should use this macro. In debug mode, we
// use dynamic_cast<> to double-check the downcast is legal (we die
// if it's not). In normal mode, we do the efficient static_cast<>
// instead. Thus, it's important to test in debug mode to make sure
// the cast is legal!
// This is the only place in the code we should use dynamic_cast<>.
// In particular, you SHOULDN'T be using dynamic_cast<> in order to
// do RTTI (eg code like this:
// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
// You should design the code some other way not to need this.
template<typename To, typename From> // use like this: down_cast<T*>(foo);
inline To down_cast(From* f) { // so we only accept pointers
// Ensures that To is a sub-type of From *. This test is here only
// for compile-time type checking, and has no overhead in an
// optimized build at run-time, as it will be optimized away
// completely.
if (false) {
implicit_cast<From*, To>(0);
}
#if !defined(NDEBUG) && PROTOBUF_RTTI
assert(f == nullptr || dynamic_cast<To>(f) != nullptr); // RTTI: debug mode only!
#endif
return static_cast<To>(f);
}
template<typename To, typename From> // use like this: down_cast<T&>(foo);
inline To down_cast(From& f) {
typedef typename std::remove_reference<To>::type* ToAsPointer;
// Ensures that To is a sub-type of From *. This test is here only
// for compile-time type checking, and has no overhead in an
// optimized build at run-time, as it will be optimized away
// completely.
if (false) {
implicit_cast<From*, ToAsPointer>(0);
}
#if !defined(NDEBUG) && PROTOBUF_RTTI
// RTTI: debug mode only!
assert(dynamic_cast<ToAsPointer>(&f) != nullptr);
#endif
return *static_cast<ToAsPointer>(&f);
}
template<typename To, typename From>
inline To bit_cast(const From& from) {
static_assert(sizeof(From) == sizeof(To), "bit_cast_with_different_sizes");
To dest;
memcpy(&dest, &from, sizeof(dest));
return dest;
}
} // namespace internal
// We made these internal so that they would show up as such in the docs,
// but we don't want to stick "internal::" in front of them everywhere.
using internal::implicit_cast;
using internal::down_cast;
using internal::bit_cast;
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_CASTS_H__

View File

@@ -0,0 +1,201 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: kenton@google.com (Kenton Varda) and others
//
// Contains basic types and utilities used by the rest of the library.
#ifndef GOOGLE_PROTOBUF_COMMON_H__
#define GOOGLE_PROTOBUF_COMMON_H__
#include <algorithm>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include <google/protobuf/stubs/macros.h>
#include <google/protobuf/stubs/platform_macros.h>
#include <google/protobuf/stubs/port.h>
#include <google/protobuf/stubs/stringpiece.h>
#ifndef PROTOBUF_USE_EXCEPTIONS
#if defined(_MSC_VER) && defined(_CPPUNWIND)
#define PROTOBUF_USE_EXCEPTIONS 1
#elif defined(__EXCEPTIONS)
#define PROTOBUF_USE_EXCEPTIONS 1
#else
#define PROTOBUF_USE_EXCEPTIONS 0
#endif
#endif
#if PROTOBUF_USE_EXCEPTIONS
#include <exception>
#endif
#if defined(__APPLE__)
#include <TargetConditionals.h> // for TARGET_OS_IPHONE
#endif
#if defined(__ANDROID__) || defined(GOOGLE_PROTOBUF_OS_ANDROID) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || defined(GOOGLE_PROTOBUF_OS_IPHONE)
#include <pthread.h>
#endif
#include <google/protobuf/port_def.inc>
namespace std {}
namespace google {
namespace protobuf {
namespace internal {
// Some of these constants are macros rather than const ints so that they can
// be used in #if directives.
// The current version, represented as a single integer to make comparison
// easier: major * 10^6 + minor * 10^3 + micro
#define GOOGLE_PROTOBUF_VERSION 3017003
// A suffix string for alpha, beta or rc releases. Empty for stable releases.
#define GOOGLE_PROTOBUF_VERSION_SUFFIX ""
// The minimum header version which works with the current version of
// the library. This constant should only be used by protoc's C++ code
// generator.
static const int kMinHeaderVersionForLibrary = 3017000;
// The minimum protoc version which works with the current version of the
// headers.
#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 3017000
// The minimum header version which works with the current version of
// protoc. This constant should only be used in VerifyVersion().
static const int kMinHeaderVersionForProtoc = 3017000;
// Verifies that the headers and libraries are compatible. Use the macro
// below to call this.
void PROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
const char* filename);
// Converts a numeric version number to a string.
std::string PROTOBUF_EXPORT VersionString(int version);
} // namespace internal
// Place this macro in your main() function (or somewhere before you attempt
// to use the protobuf library) to verify that the version you link against
// matches the headers you compiled against. If a version mismatch is
// detected, the process will abort.
#define GOOGLE_PROTOBUF_VERIFY_VERSION \
::google::protobuf::internal::VerifyVersion( \
GOOGLE_PROTOBUF_VERSION, GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION, \
__FILE__)
// ===================================================================
// from google3/util/utf8/public/unilib.h
namespace internal {
// Checks if the buffer contains structurally-valid UTF-8. Implemented in
// structurally_valid.cc.
PROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len);
inline bool IsStructurallyValidUTF8(StringPiece str) {
return IsStructurallyValidUTF8(str.data(), static_cast<int>(str.length()));
}
// Returns initial number of bytes of structurally valid UTF-8.
PROTOBUF_EXPORT int UTF8SpnStructurallyValid(StringPiece str);
// Coerce UTF-8 byte string in src_str to be
// a structurally-valid equal-length string by selectively
// overwriting illegal bytes with replace_char (typically ' ' or '?').
// replace_char must be legal printable 7-bit Ascii 0x20..0x7e.
// src_str is read-only.
//
// Returns pointer to output buffer, src_str.data() if no changes were made,
// or idst if some bytes were changed. idst is allocated by the caller
// and must be at least as big as src_str
//
// Optimized for: all structurally valid and no byte copying is done.
//
PROTOBUF_EXPORT char* UTF8CoerceToStructurallyValid(StringPiece str, char* dst,
char replace_char);
} // namespace internal
// This lives in message_lite.h now, but we leave this here for any users that
// #include common.h and not message_lite.h.
PROTOBUF_EXPORT void ShutdownProtobufLibrary();
namespace internal {
// Strongly references the given variable such that the linker will be forced
// to pull in this variable's translation unit.
template <typename T>
void StrongReference(const T& var) {
auto volatile unused = &var;
(void)&unused; // Use address to avoid an extra load of "unused".
}
} // namespace internal
#if PROTOBUF_USE_EXCEPTIONS
class FatalException : public std::exception {
public:
FatalException(const char* filename, int line, const std::string& message)
: filename_(filename), line_(line), message_(message) {}
virtual ~FatalException() throw();
virtual const char* what() const throw();
const char* filename() const { return filename_; }
int line() const { return line_; }
const std::string& message() const { return message_; }
private:
const char* filename_;
const int line_;
const std::string message_;
};
#endif
// This is at the end of the file instead of the beginning to work around a bug
// in some versions of MSVC.
using std::string;
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_COMMON_H__

View File

@@ -0,0 +1,114 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: kenton@google.com (Kenton Varda)
#ifndef GOOGLE_PROTOBUF_STUBS_HASH_H__
#define GOOGLE_PROTOBUF_STUBS_HASH_H__
#include <cstring>
#include <string>
#include <unordered_map>
#include <unordered_set>
# define GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START \
namespace google { \
namespace protobuf {
# define GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END }}
namespace google {
namespace protobuf {
template <typename Key>
struct hash : public std::hash<Key> {};
template <typename Key>
struct hash<const Key*> {
inline size_t operator()(const Key* key) const {
return reinterpret_cast<size_t>(key);
}
};
// Unlike the old SGI version, the TR1 "hash" does not special-case char*. So,
// we go ahead and provide our own implementation.
template <>
struct hash<const char*> {
inline size_t operator()(const char* str) const {
size_t result = 0;
for (; *str != '\0'; str++) {
result = 5 * result + static_cast<size_t>(*str);
}
return result;
}
};
template<>
struct hash<bool> {
size_t operator()(bool x) const {
return static_cast<size_t>(x);
}
};
template <>
struct hash<std::string> {
inline size_t operator()(const std::string& key) const {
return hash<const char*>()(key.c_str());
}
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
inline bool operator()(const std::string& a, const std::string& b) const {
return a < b;
}
};
template <typename First, typename Second>
struct hash<std::pair<First, Second> > {
inline size_t operator()(const std::pair<First, Second>& key) const {
size_t first_hash = hash<First>()(key.first);
size_t second_hash = hash<Second>()(key.second);
// FIXME(kenton): What is the best way to compute this hash? I have
// no idea! This seems a bit better than an XOR.
return first_hash * ((1 << 16) - 1) + second_hash;
}
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
inline bool operator()(const std::pair<First, Second>& a,
const std::pair<First, Second>& b) const {
return a < b;
}
};
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_STUBS_HASH_H__

View File

@@ -0,0 +1,239 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef GOOGLE_PROTOBUF_STUBS_LOGGING_H_
#define GOOGLE_PROTOBUF_STUBS_LOGGING_H_
#include <google/protobuf/stubs/macros.h>
#include <google/protobuf/stubs/port.h>
#include <google/protobuf/stubs/status.h>
#include <google/protobuf/stubs/stringpiece.h>
#include <google/protobuf/port_def.inc>
// ===================================================================
// emulates google3/base/logging.h
namespace google {
namespace protobuf {
enum LogLevel {
LOGLEVEL_INFO, // Informational. This is never actually used by
// libprotobuf.
LOGLEVEL_WARNING, // Warns about issues that, although not technically a
// problem now, could cause problems in the future. For
// example, a // warning will be printed when parsing a
// message that is near the message size limit.
LOGLEVEL_ERROR, // An error occurred which should never happen during
// normal use.
LOGLEVEL_FATAL, // An error occurred from which the library cannot
// recover. This usually indicates a programming error
// in the code which calls the library, especially when
// compiled in debug mode.
#ifdef NDEBUG
LOGLEVEL_DFATAL = LOGLEVEL_ERROR
#else
LOGLEVEL_DFATAL = LOGLEVEL_FATAL
#endif
};
class uint128;
namespace internal {
class LogFinisher;
class PROTOBUF_EXPORT LogMessage {
public:
LogMessage(LogLevel level, const char* filename, int line);
~LogMessage();
LogMessage& operator<<(const std::string& value);
LogMessage& operator<<(const char* value);
LogMessage& operator<<(char value);
LogMessage& operator<<(int value);
LogMessage& operator<<(uint value);
LogMessage& operator<<(long value);
LogMessage& operator<<(unsigned long value);
LogMessage& operator<<(long long value);
LogMessage& operator<<(unsigned long long value);
LogMessage& operator<<(double value);
LogMessage& operator<<(void* value);
LogMessage& operator<<(const StringPiece& value);
LogMessage& operator<<(const util::Status& status);
LogMessage& operator<<(const uint128& value);
private:
friend class LogFinisher;
void Finish();
LogLevel level_;
const char* filename_;
int line_;
std::string message_;
};
// Used to make the entire "LOG(BLAH) << etc." expression have a void return
// type and print a newline after each message.
class PROTOBUF_EXPORT LogFinisher {
public:
void operator=(LogMessage& other);
};
template<typename T>
bool IsOk(T status) { return status.ok(); }
template<>
inline bool IsOk(bool status) { return status; }
} // namespace internal
// Undef everything in case we're being mixed with some other Google library
// which already defined them itself. Presumably all Google libraries will
// support the same syntax for these so it should not be a big deal if they
// end up using our definitions instead.
#undef GOOGLE_LOG
#undef GOOGLE_LOG_IF
#undef GOOGLE_CHECK
#undef GOOGLE_CHECK_OK
#undef GOOGLE_CHECK_EQ
#undef GOOGLE_CHECK_NE
#undef GOOGLE_CHECK_LT
#undef GOOGLE_CHECK_LE
#undef GOOGLE_CHECK_GT
#undef GOOGLE_CHECK_GE
#undef GOOGLE_CHECK_NOTNULL
#undef GOOGLE_DLOG
#undef GOOGLE_DCHECK
#undef GOOGLE_DCHECK_OK
#undef GOOGLE_DCHECK_EQ
#undef GOOGLE_DCHECK_NE
#undef GOOGLE_DCHECK_LT
#undef GOOGLE_DCHECK_LE
#undef GOOGLE_DCHECK_GT
#undef GOOGLE_DCHECK_GE
#define GOOGLE_LOG(LEVEL) \
::google::protobuf::internal::LogFinisher() = \
::google::protobuf::internal::LogMessage( \
::google::protobuf::LOGLEVEL_##LEVEL, __FILE__, __LINE__)
#define GOOGLE_LOG_IF(LEVEL, CONDITION) \
!(CONDITION) ? (void)0 : GOOGLE_LOG(LEVEL)
#define GOOGLE_CHECK(EXPRESSION) \
GOOGLE_LOG_IF(FATAL, !(EXPRESSION)) << "CHECK failed: " #EXPRESSION ": "
#define GOOGLE_CHECK_OK(A) GOOGLE_CHECK(::google::protobuf::internal::IsOk(A))
#define GOOGLE_CHECK_EQ(A, B) GOOGLE_CHECK((A) == (B))
#define GOOGLE_CHECK_NE(A, B) GOOGLE_CHECK((A) != (B))
#define GOOGLE_CHECK_LT(A, B) GOOGLE_CHECK((A) < (B))
#define GOOGLE_CHECK_LE(A, B) GOOGLE_CHECK((A) <= (B))
#define GOOGLE_CHECK_GT(A, B) GOOGLE_CHECK((A) > (B))
#define GOOGLE_CHECK_GE(A, B) GOOGLE_CHECK((A) >= (B))
namespace internal {
template<typename T>
T* CheckNotNull(const char* /* file */, int /* line */,
const char* name, T* val) {
if (val == nullptr) {
GOOGLE_LOG(FATAL) << name;
}
return val;
}
} // namespace internal
#define GOOGLE_CHECK_NOTNULL(A) \
::google::protobuf::internal::CheckNotNull( \
__FILE__, __LINE__, "'" #A "' must not be nullptr", (A))
#ifdef NDEBUG
#define GOOGLE_DLOG(LEVEL) GOOGLE_LOG_IF(LEVEL, false)
#define GOOGLE_DCHECK(EXPRESSION) while(false) GOOGLE_CHECK(EXPRESSION)
#define GOOGLE_DCHECK_OK(E) GOOGLE_DCHECK(::google::protobuf::internal::IsOk(E))
#define GOOGLE_DCHECK_EQ(A, B) GOOGLE_DCHECK((A) == (B))
#define GOOGLE_DCHECK_NE(A, B) GOOGLE_DCHECK((A) != (B))
#define GOOGLE_DCHECK_LT(A, B) GOOGLE_DCHECK((A) < (B))
#define GOOGLE_DCHECK_LE(A, B) GOOGLE_DCHECK((A) <= (B))
#define GOOGLE_DCHECK_GT(A, B) GOOGLE_DCHECK((A) > (B))
#define GOOGLE_DCHECK_GE(A, B) GOOGLE_DCHECK((A) >= (B))
#else // NDEBUG
#define GOOGLE_DLOG GOOGLE_LOG
#define GOOGLE_DCHECK GOOGLE_CHECK
#define GOOGLE_DCHECK_OK GOOGLE_CHECK_OK
#define GOOGLE_DCHECK_EQ GOOGLE_CHECK_EQ
#define GOOGLE_DCHECK_NE GOOGLE_CHECK_NE
#define GOOGLE_DCHECK_LT GOOGLE_CHECK_LT
#define GOOGLE_DCHECK_LE GOOGLE_CHECK_LE
#define GOOGLE_DCHECK_GT GOOGLE_CHECK_GT
#define GOOGLE_DCHECK_GE GOOGLE_CHECK_GE
#endif // !NDEBUG
typedef void LogHandler(LogLevel level, const char* filename, int line,
const std::string& message);
// The protobuf library sometimes writes warning and error messages to
// stderr. These messages are primarily useful for developers, but may
// also help end users figure out a problem. If you would prefer that
// these messages be sent somewhere other than stderr, call SetLogHandler()
// to set your own handler. This returns the old handler. Set the handler
// to nullptr to ignore log messages (but see also LogSilencer, below).
//
// Obviously, SetLogHandler is not thread-safe. You should only call it
// at initialization time, and probably not from library code. If you
// simply want to suppress log messages temporarily (e.g. because you
// have some code that tends to trigger them frequently and you know
// the warnings are not important to you), use the LogSilencer class
// below.
PROTOBUF_EXPORT LogHandler* SetLogHandler(LogHandler* new_func);
// Create a LogSilencer if you want to temporarily suppress all log
// messages. As long as any LogSilencer objects exist, non-fatal
// log messages will be discarded (the current LogHandler will *not*
// be called). Constructing a LogSilencer is thread-safe. You may
// accidentally suppress log messages occurring in another thread, but
// since messages are generally for debugging purposes only, this isn't
// a big deal. If you want to intercept log messages, use SetLogHandler().
class PROTOBUF_EXPORT LogSilencer {
public:
LogSilencer();
~LogSilencer();
};
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_STUBS_LOGGING_H_

View File

@@ -0,0 +1,93 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef GOOGLE_PROTOBUF_MACROS_H__
#define GOOGLE_PROTOBUF_MACROS_H__
namespace google {
namespace protobuf {
#undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
TypeName(const TypeName&) = delete; \
void operator=(const TypeName&) = delete
#undef GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS
#define GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
TypeName() = delete; \
TypeName(const TypeName&) = delete; \
void operator=(const TypeName&) = delete
// ===================================================================
// from google3/base/basictypes.h
// The GOOGLE_ARRAYSIZE(arr) macro returns the # of elements in an array arr.
// The expression is a compile-time constant, and therefore can be
// used in defining new arrays, for example.
//
// GOOGLE_ARRAYSIZE catches a few type errors. If you see a compiler error
//
// "warning: division by zero in ..."
//
// when using GOOGLE_ARRAYSIZE, you are (wrongfully) giving it a pointer.
// You should only use GOOGLE_ARRAYSIZE on statically allocated arrays.
//
// The following comments are on the implementation details, and can
// be ignored by the users.
//
// ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in
// the array) and sizeof(*(arr)) (the # of bytes in one array
// element). If the former is divisible by the latter, perhaps arr is
// indeed an array, in which case the division result is the # of
// elements in the array. Otherwise, arr cannot possibly be an array,
// and we generate a compiler error to prevent the code from
// compiling.
//
// Since the size of bool is implementation-defined, we need to cast
// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
// result has type size_t.
//
// This macro is not perfect as it wrongfully accepts certain
// pointers, namely where the pointer size is divisible by the pointee
// size. Since all our code has to go through a 32-bit compiler,
// where a pointer is 4 bytes, this means all pointers to a type whose
// size is 3 or greater than 4 will be (righteously) rejected.
//
// Kudos to Jorg Brown for this simple and elegant implementation.
#undef GOOGLE_ARRAYSIZE
#define GOOGLE_ARRAYSIZE(a) \
((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_MACROS_H__

View File

@@ -0,0 +1,769 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2014 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// from google3/util/gtl/map_util.h
// Author: Anton Carver
#ifndef GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
#define GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
#include <stddef.h>
#include <iterator>
#include <string>
#include <utility>
#include <vector>
#include <google/protobuf/stubs/common.h>
namespace google {
namespace protobuf {
namespace internal {
// Local implementation of RemoveConst to avoid including base/type_traits.h.
template <class T> struct RemoveConst { typedef T type; };
template <class T> struct RemoveConst<const T> : RemoveConst<T> {};
} // namespace internal
//
// Find*()
//
// Returns a const reference to the value associated with the given key if it
// exists. Crashes otherwise.
//
// This is intended as a replacement for operator[] as an rvalue (for reading)
// when the key is guaranteed to exist.
//
// operator[] for lookup is discouraged for several reasons:
// * It has a side-effect of inserting missing keys
// * It is not thread-safe (even when it is not inserting, it can still
// choose to resize the underlying storage)
// * It invalidates iterators (when it chooses to resize)
// * It default constructs a value object even if it doesn't need to
//
// This version assumes the key is printable, and includes it in the fatal log
// message.
template <class Collection>
const typename Collection::value_type::second_type&
FindOrDie(const Collection& collection,
const typename Collection::value_type::first_type& key) {
typename Collection::const_iterator it = collection.find(key);
GOOGLE_CHECK(it != collection.end()) << "Map key not found: " << key;
return it->second;
}
// Same as above, but returns a non-const reference.
template <class Collection>
typename Collection::value_type::second_type&
FindOrDie(Collection& collection, // NOLINT
const typename Collection::value_type::first_type& key) {
typename Collection::iterator it = collection.find(key);
GOOGLE_CHECK(it != collection.end()) << "Map key not found: " << key;
return it->second;
}
// Same as FindOrDie above, but doesn't log the key on failure.
template <class Collection>
const typename Collection::value_type::second_type&
FindOrDieNoPrint(const Collection& collection,
const typename Collection::value_type::first_type& key) {
typename Collection::const_iterator it = collection.find(key);
GOOGLE_CHECK(it != collection.end()) << "Map key not found";
return it->second;
}
// Same as above, but returns a non-const reference.
template <class Collection>
typename Collection::value_type::second_type&
FindOrDieNoPrint(Collection& collection, // NOLINT
const typename Collection::value_type::first_type& key) {
typename Collection::iterator it = collection.find(key);
GOOGLE_CHECK(it != collection.end()) << "Map key not found";
return it->second;
}
// Returns a const reference to the value associated with the given key if it
// exists, otherwise returns a const reference to the provided default value.
//
// WARNING: If a temporary object is passed as the default "value,"
// this function will return a reference to that temporary object,
// which will be destroyed at the end of the statement. A common
// example: if you have a map with string values, and you pass a char*
// as the default "value," either use the returned value immediately
// or store it in a string (not string&).
// Details: http://go/findwithdefault
template <class Collection>
const typename Collection::value_type::second_type&
FindWithDefault(const Collection& collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value) {
typename Collection::const_iterator it = collection.find(key);
if (it == collection.end()) {
return value;
}
return it->second;
}
// Returns a pointer to the const value associated with the given key if it
// exists, or nullptr otherwise.
template <class Collection>
const typename Collection::value_type::second_type*
FindOrNull(const Collection& collection,
const typename Collection::value_type::first_type& key) {
typename Collection::const_iterator it = collection.find(key);
if (it == collection.end()) {
return 0;
}
return &it->second;
}
// Same as above but returns a pointer to the non-const value.
template <class Collection>
typename Collection::value_type::second_type*
FindOrNull(Collection& collection, // NOLINT
const typename Collection::value_type::first_type& key) {
typename Collection::iterator it = collection.find(key);
if (it == collection.end()) {
return 0;
}
return &it->second;
}
// Returns the pointer value associated with the given key. If none is found,
// nullptr is returned. The function is designed to be used with a map of keys to
// pointers.
//
// This function does not distinguish between a missing key and a key mapped
// to nullptr.
template <class Collection>
typename Collection::value_type::second_type
FindPtrOrNull(const Collection& collection,
const typename Collection::value_type::first_type& key) {
typename Collection::const_iterator it = collection.find(key);
if (it == collection.end()) {
return typename Collection::value_type::second_type();
}
return it->second;
}
// Same as above, except takes non-const reference to collection.
//
// This function is needed for containers that propagate constness to the
// pointee, such as boost::ptr_map.
template <class Collection>
typename Collection::value_type::second_type
FindPtrOrNull(Collection& collection, // NOLINT
const typename Collection::value_type::first_type& key) {
typename Collection::iterator it = collection.find(key);
if (it == collection.end()) {
return typename Collection::value_type::second_type();
}
return it->second;
}
// Finds the pointer value associated with the given key in a map whose values
// are linked_ptrs. Returns nullptr if key is not found.
template <class Collection>
typename Collection::value_type::second_type::element_type*
FindLinkedPtrOrNull(const Collection& collection,
const typename Collection::value_type::first_type& key) {
typename Collection::const_iterator it = collection.find(key);
if (it == collection.end()) {
return 0;
}
// Since linked_ptr::get() is a const member returning a non const,
// we do not need a version of this function taking a non const collection.
return it->second.get();
}
// Same as above, but dies if the key is not found.
template <class Collection>
typename Collection::value_type::second_type::element_type&
FindLinkedPtrOrDie(const Collection& collection,
const typename Collection::value_type::first_type& key) {
typename Collection::const_iterator it = collection.find(key);
GOOGLE_CHECK(it != collection.end()) << "key not found: " << key;
// Since linked_ptr::operator*() is a const member returning a non const,
// we do not need a version of this function taking a non const collection.
return *it->second;
}
// Finds the value associated with the given key and copies it to *value (if not
// nullptr). Returns false if the key was not found, true otherwise.
template <class Collection, class Key, class Value>
bool FindCopy(const Collection& collection,
const Key& key,
Value* const value) {
typename Collection::const_iterator it = collection.find(key);
if (it == collection.end()) {
return false;
}
if (value) {
*value = it->second;
}
return true;
}
//
// Contains*()
//
// Returns true if and only if the given collection contains the given key.
template <class Collection, class Key>
bool ContainsKey(const Collection& collection, const Key& key) {
return collection.find(key) != collection.end();
}
// Returns true if and only if the given collection contains the given key-value
// pair.
template <class Collection, class Key, class Value>
bool ContainsKeyValuePair(const Collection& collection,
const Key& key,
const Value& value) {
typedef typename Collection::const_iterator const_iterator;
std::pair<const_iterator, const_iterator> range = collection.equal_range(key);
for (const_iterator it = range.first; it != range.second; ++it) {
if (it->second == value) {
return true;
}
}
return false;
}
//
// Insert*()
//
// Inserts the given key-value pair into the collection. Returns true if and
// only if the key from the given pair didn't previously exist. Otherwise, the
// value in the map is replaced with the value from the given pair.
template <class Collection>
bool InsertOrUpdate(Collection* const collection,
const typename Collection::value_type& vt) {
std::pair<typename Collection::iterator, bool> ret = collection->insert(vt);
if (!ret.second) {
// update
ret.first->second = vt.second;
return false;
}
return true;
}
// Same as above, except that the key and value are passed separately.
template <class Collection>
bool InsertOrUpdate(Collection* const collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value) {
return InsertOrUpdate(
collection, typename Collection::value_type(key, value));
}
// Inserts/updates all the key-value pairs from the range defined by the
// iterators "first" and "last" into the given collection.
template <class Collection, class InputIterator>
void InsertOrUpdateMany(Collection* const collection,
InputIterator first, InputIterator last) {
for (; first != last; ++first) {
InsertOrUpdate(collection, *first);
}
}
// Change the value associated with a particular key in a map or hash_map
// of the form map<Key, Value*> which owns the objects pointed to by the
// value pointers. If there was an existing value for the key, it is deleted.
// True indicates an insert took place, false indicates an update + delete.
template <class Collection>
bool InsertAndDeleteExisting(
Collection* const collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value) {
std::pair<typename Collection::iterator, bool> ret =
collection->insert(typename Collection::value_type(key, value));
if (!ret.second) {
delete ret.first->second;
ret.first->second = value;
return false;
}
return true;
}
// Inserts the given key and value into the given collection if and only if the
// given key did NOT already exist in the collection. If the key previously
// existed in the collection, the value is not changed. Returns true if the
// key-value pair was inserted; returns false if the key was already present.
template <class Collection>
bool InsertIfNotPresent(Collection* const collection,
const typename Collection::value_type& vt) {
return collection->insert(vt).second;
}
// Same as above except the key and value are passed separately.
template <class Collection>
bool InsertIfNotPresent(
Collection* const collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value) {
return InsertIfNotPresent(
collection, typename Collection::value_type(key, value));
}
// Same as above except dies if the key already exists in the collection.
template <class Collection>
void InsertOrDie(Collection* const collection,
const typename Collection::value_type& value) {
GOOGLE_CHECK(InsertIfNotPresent(collection, value))
<< "duplicate value: " << value;
}
// Same as above except doesn't log the value on error.
template <class Collection>
void InsertOrDieNoPrint(Collection* const collection,
const typename Collection::value_type& value) {
GOOGLE_CHECK(InsertIfNotPresent(collection, value)) << "duplicate value.";
}
// Inserts the key-value pair into the collection. Dies if key was already
// present.
template <class Collection>
void InsertOrDie(Collection* const collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& data) {
GOOGLE_CHECK(InsertIfNotPresent(collection, key, data))
<< "duplicate key: " << key;
}
// Same as above except doesn't log the key on error.
template <class Collection>
void InsertOrDieNoPrint(
Collection* const collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& data) {
GOOGLE_CHECK(InsertIfNotPresent(collection, key, data)) << "duplicate key.";
}
// Inserts a new key and default-initialized value. Dies if the key was already
// present. Returns a reference to the value. Example usage:
//
// map<int, SomeProto> m;
// SomeProto& proto = InsertKeyOrDie(&m, 3);
// proto.set_field("foo");
template <class Collection>
typename Collection::value_type::second_type& InsertKeyOrDie(
Collection* const collection,
const typename Collection::value_type::first_type& key) {
typedef typename Collection::value_type value_type;
std::pair<typename Collection::iterator, bool> res =
collection->insert(value_type(key, typename value_type::second_type()));
GOOGLE_CHECK(res.second) << "duplicate key: " << key;
return res.first->second;
}
//
// Lookup*()
//
// Looks up a given key and value pair in a collection and inserts the key-value
// pair if it's not already present. Returns a reference to the value associated
// with the key.
template <class Collection>
typename Collection::value_type::second_type&
LookupOrInsert(Collection* const collection,
const typename Collection::value_type& vt) {
return collection->insert(vt).first->second;
}
// Same as above except the key-value are passed separately.
template <class Collection>
typename Collection::value_type::second_type&
LookupOrInsert(Collection* const collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value) {
return LookupOrInsert(
collection, typename Collection::value_type(key, value));
}
// Counts the number of equivalent elements in the given "sequence", and stores
// the results in "count_map" with element as the key and count as the value.
//
// Example:
// vector<string> v = {"a", "b", "c", "a", "b"};
// map<string, int> m;
// AddTokenCounts(v, 1, &m);
// assert(m["a"] == 2);
// assert(m["b"] == 2);
// assert(m["c"] == 1);
template <typename Sequence, typename Collection>
void AddTokenCounts(
const Sequence& sequence,
const typename Collection::value_type::second_type& increment,
Collection* const count_map) {
for (typename Sequence::const_iterator it = sequence.begin();
it != sequence.end(); ++it) {
typename Collection::value_type::second_type& value =
LookupOrInsert(count_map, *it,
typename Collection::value_type::second_type());
value += increment;
}
}
// Returns a reference to the value associated with key. If not found, a value
// is default constructed on the heap and added to the map.
//
// This function is useful for containers of the form map<Key, Value*>, where
// inserting a new key, value pair involves constructing a new heap-allocated
// Value, and storing a pointer to that in the collection.
template <class Collection>
typename Collection::value_type::second_type&
LookupOrInsertNew(Collection* const collection,
const typename Collection::value_type::first_type& key) {
typedef typename std::iterator_traits<
typename Collection::value_type::second_type>::value_type Element;
std::pair<typename Collection::iterator, bool> ret =
collection->insert(typename Collection::value_type(
key,
static_cast<typename Collection::value_type::second_type>(nullptr)));
if (ret.second) {
ret.first->second = new Element();
}
return ret.first->second;
}
// Same as above but constructs the value using the single-argument constructor
// and the given "arg".
template <class Collection, class Arg>
typename Collection::value_type::second_type&
LookupOrInsertNew(Collection* const collection,
const typename Collection::value_type::first_type& key,
const Arg& arg) {
typedef typename std::iterator_traits<
typename Collection::value_type::second_type>::value_type Element;
std::pair<typename Collection::iterator, bool> ret =
collection->insert(typename Collection::value_type(
key,
static_cast<typename Collection::value_type::second_type>(nullptr)));
if (ret.second) {
ret.first->second = new Element(arg);
}
return ret.first->second;
}
// Lookup of linked/shared pointers is used in two scenarios:
//
// Use LookupOrInsertNewLinkedPtr if the container owns the elements.
// In this case it is fine working with the raw pointer as long as it is
// guaranteed that no other thread can delete/update an accessed element.
// A mutex will need to lock the container operation as well as the use
// of the returned elements. Finding an element may be performed using
// FindLinkedPtr*().
//
// Use LookupOrInsertNewSharedPtr if the container does not own the elements
// for their whole lifetime. This is typically the case when a reader allows
// parallel updates to the container. In this case a Mutex only needs to lock
// container operations, but all element operations must be performed on the
// shared pointer. Finding an element must be performed using FindPtr*() and
// cannot be done with FindLinkedPtr*() even though it compiles.
// Lookup a key in a map or hash_map whose values are linked_ptrs. If it is
// missing, set collection[key].reset(new Value::element_type) and return that.
// Value::element_type must be default constructable.
template <class Collection>
typename Collection::value_type::second_type::element_type*
LookupOrInsertNewLinkedPtr(
Collection* const collection,
const typename Collection::value_type::first_type& key) {
typedef typename Collection::value_type::second_type Value;
std::pair<typename Collection::iterator, bool> ret =
collection->insert(typename Collection::value_type(key, Value()));
if (ret.second) {
ret.first->second.reset(new typename Value::element_type);
}
return ret.first->second.get();
}
// A variant of LookupOrInsertNewLinkedPtr where the value is constructed using
// a single-parameter constructor. Note: the constructor argument is computed
// even if it will not be used, so only values cheap to compute should be passed
// here. On the other hand it does not matter how expensive the construction of
// the actual stored value is, as that only occurs if necessary.
template <class Collection, class Arg>
typename Collection::value_type::second_type::element_type*
LookupOrInsertNewLinkedPtr(
Collection* const collection,
const typename Collection::value_type::first_type& key,
const Arg& arg) {
typedef typename Collection::value_type::second_type Value;
std::pair<typename Collection::iterator, bool> ret =
collection->insert(typename Collection::value_type(key, Value()));
if (ret.second) {
ret.first->second.reset(new typename Value::element_type(arg));
}
return ret.first->second.get();
}
// Lookup a key in a map or hash_map whose values are shared_ptrs. If it is
// missing, set collection[key].reset(new Value::element_type). Unlike
// LookupOrInsertNewLinkedPtr, this function returns the shared_ptr instead of
// the raw pointer. Value::element_type must be default constructable.
template <class Collection>
typename Collection::value_type::second_type&
LookupOrInsertNewSharedPtr(
Collection* const collection,
const typename Collection::value_type::first_type& key) {
typedef typename Collection::value_type::second_type SharedPtr;
typedef typename Collection::value_type::second_type::element_type Element;
std::pair<typename Collection::iterator, bool> ret =
collection->insert(typename Collection::value_type(key, SharedPtr()));
if (ret.second) {
ret.first->second.reset(new Element());
}
return ret.first->second;
}
// A variant of LookupOrInsertNewSharedPtr where the value is constructed using
// a single-parameter constructor. Note: the constructor argument is computed
// even if it will not be used, so only values cheap to compute should be passed
// here. On the other hand it does not matter how expensive the construction of
// the actual stored value is, as that only occurs if necessary.
template <class Collection, class Arg>
typename Collection::value_type::second_type&
LookupOrInsertNewSharedPtr(
Collection* const collection,
const typename Collection::value_type::first_type& key,
const Arg& arg) {
typedef typename Collection::value_type::second_type SharedPtr;
typedef typename Collection::value_type::second_type::element_type Element;
std::pair<typename Collection::iterator, bool> ret =
collection->insert(typename Collection::value_type(key, SharedPtr()));
if (ret.second) {
ret.first->second.reset(new Element(arg));
}
return ret.first->second;
}
//
// Misc Utility Functions
//
// Updates the value associated with the given key. If the key was not already
// present, then the key-value pair are inserted and "previous" is unchanged. If
// the key was already present, the value is updated and "*previous" will
// contain a copy of the old value.
//
// InsertOrReturnExisting has complementary behavior that returns the
// address of an already existing value, rather than updating it.
template <class Collection>
bool UpdateReturnCopy(Collection* const collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value,
typename Collection::value_type::second_type* previous) {
std::pair<typename Collection::iterator, bool> ret =
collection->insert(typename Collection::value_type(key, value));
if (!ret.second) {
// update
if (previous) {
*previous = ret.first->second;
}
ret.first->second = value;
return true;
}
return false;
}
// Same as above except that the key and value are passed as a pair.
template <class Collection>
bool UpdateReturnCopy(Collection* const collection,
const typename Collection::value_type& vt,
typename Collection::value_type::second_type* previous) {
std::pair<typename Collection::iterator, bool> ret = collection->insert(vt);
if (!ret.second) {
// update
if (previous) {
*previous = ret.first->second;
}
ret.first->second = vt.second;
return true;
}
return false;
}
// Tries to insert the given key-value pair into the collection. Returns nullptr if
// the insert succeeds. Otherwise, returns a pointer to the existing value.
//
// This complements UpdateReturnCopy in that it allows to update only after
// verifying the old value and still insert quickly without having to look up
// twice. Unlike UpdateReturnCopy this also does not come with the issue of an
// undefined previous* in case new data was inserted.
template <class Collection>
typename Collection::value_type::second_type* InsertOrReturnExisting(
Collection* const collection, const typename Collection::value_type& vt) {
std::pair<typename Collection::iterator, bool> ret = collection->insert(vt);
if (ret.second) {
return nullptr; // Inserted, no existing previous value.
} else {
return &ret.first->second; // Return address of already existing value.
}
}
// Same as above, except for explicit key and data.
template <class Collection>
typename Collection::value_type::second_type* InsertOrReturnExisting(
Collection* const collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& data) {
return InsertOrReturnExisting(collection,
typename Collection::value_type(key, data));
}
// Erases the collection item identified by the given key, and returns the value
// associated with that key. It is assumed that the value (i.e., the
// mapped_type) is a pointer. Returns nullptr if the key was not found in the
// collection.
//
// Examples:
// map<string, MyType*> my_map;
//
// One line cleanup:
// delete EraseKeyReturnValuePtr(&my_map, "abc");
//
// Use returned value:
// std::unique_ptr<MyType> value_ptr(
// EraseKeyReturnValuePtr(&my_map, "abc"));
// if (value_ptr.get())
// value_ptr->DoSomething();
//
template <class Collection>
typename Collection::value_type::second_type EraseKeyReturnValuePtr(
Collection* const collection,
const typename Collection::value_type::first_type& key) {
typename Collection::iterator it = collection->find(key);
if (it == collection->end()) {
return nullptr;
}
typename Collection::value_type::second_type v = it->second;
collection->erase(it);
return v;
}
// Inserts all the keys from map_container into key_container, which must
// support insert(MapContainer::key_type).
//
// Note: any initial contents of the key_container are not cleared.
template <class MapContainer, class KeyContainer>
void InsertKeysFromMap(const MapContainer& map_container,
KeyContainer* key_container) {
GOOGLE_CHECK(key_container != nullptr);
for (typename MapContainer::const_iterator it = map_container.begin();
it != map_container.end(); ++it) {
key_container->insert(it->first);
}
}
// Appends all the keys from map_container into key_container, which must
// support push_back(MapContainer::key_type).
//
// Note: any initial contents of the key_container are not cleared.
template <class MapContainer, class KeyContainer>
void AppendKeysFromMap(const MapContainer& map_container,
KeyContainer* key_container) {
GOOGLE_CHECK(key_container != nullptr);
for (typename MapContainer::const_iterator it = map_container.begin();
it != map_container.end(); ++it) {
key_container->push_back(it->first);
}
}
// A more specialized overload of AppendKeysFromMap to optimize reallocations
// for the common case in which we're appending keys to a vector and hence can
// (and sometimes should) call reserve() first.
//
// (It would be possible to play SFINAE games to call reserve() for any
// container that supports it, but this seems to get us 99% of what we need
// without the complexity of a SFINAE-based solution.)
template <class MapContainer, class KeyType>
void AppendKeysFromMap(const MapContainer& map_container,
std::vector<KeyType>* key_container) {
GOOGLE_CHECK(key_container != nullptr);
// We now have the opportunity to call reserve(). Calling reserve() every
// time is a bad idea for some use cases: libstdc++'s implementation of
// vector<>::reserve() resizes the vector's backing store to exactly the
// given size (unless it's already at least that big). Because of this,
// the use case that involves appending a lot of small maps (total size
// N) one by one to a vector would be O(N^2). But never calling reserve()
// loses the opportunity to improve the use case of adding from a large
// map to an empty vector (this improves performance by up to 33%). A
// number of heuristics are possible; see the discussion in
// cl/34081696. Here we use the simplest one.
if (key_container->empty()) {
key_container->reserve(map_container.size());
}
for (typename MapContainer::const_iterator it = map_container.begin();
it != map_container.end(); ++it) {
key_container->push_back(it->first);
}
}
// Inserts all the values from map_container into value_container, which must
// support push_back(MapContainer::mapped_type).
//
// Note: any initial contents of the value_container are not cleared.
template <class MapContainer, class ValueContainer>
void AppendValuesFromMap(const MapContainer& map_container,
ValueContainer* value_container) {
GOOGLE_CHECK(value_container != nullptr);
for (typename MapContainer::const_iterator it = map_container.begin();
it != map_container.end(); ++it) {
value_container->push_back(it->second);
}
}
// A more specialized overload of AppendValuesFromMap to optimize reallocations
// for the common case in which we're appending values to a vector and hence
// can (and sometimes should) call reserve() first.
//
// (It would be possible to play SFINAE games to call reserve() for any
// container that supports it, but this seems to get us 99% of what we need
// without the complexity of a SFINAE-based solution.)
template <class MapContainer, class ValueType>
void AppendValuesFromMap(const MapContainer& map_container,
std::vector<ValueType>* value_container) {
GOOGLE_CHECK(value_container != nullptr);
// See AppendKeysFromMap for why this is done.
if (value_container->empty()) {
value_container->reserve(map_container.size());
}
for (typename MapContainer::const_iterator it = map_container.begin();
it != map_container.end(); ++it) {
value_container->push_back(it->second);
}
}
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__

View File

@@ -0,0 +1,213 @@
// Copyright (c) 2006, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef GOOGLE_PROTOBUF_STUBS_MUTEX_H_
#define GOOGLE_PROTOBUF_STUBS_MUTEX_H_
#include <mutex>
#ifdef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
#include <windows.h>
// GetMessage conflicts with GeneratedMessageReflection::GetMessage().
#ifdef GetMessage
#undef GetMessage
#endif
#endif
#include <google/protobuf/stubs/macros.h>
// Define thread-safety annotations for use below, if we are building with
// Clang.
#if defined(__clang__) && !defined(SWIG)
#define GOOGLE_PROTOBUF_ACQUIRE(...) \
__attribute__((acquire_capability(__VA_ARGS__)))
#define GOOGLE_PROTOBUF_RELEASE(...) \
__attribute__((release_capability(__VA_ARGS__)))
#define GOOGLE_PROTOBUF_CAPABILITY(x) __attribute__((capability(x)))
#else
#define GOOGLE_PROTOBUF_ACQUIRE(...)
#define GOOGLE_PROTOBUF_RELEASE(...)
#define GOOGLE_PROTOBUF_CAPABILITY(x)
#endif
#include <google/protobuf/port_def.inc>
// ===================================================================
// emulates google3/base/mutex.h
namespace google {
namespace protobuf {
namespace internal {
#define GOOGLE_PROTOBUF_LINKER_INITIALIZED
#ifdef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP
// This class is a lightweight replacement for std::mutex on Windows platforms.
// std::mutex does not work on Windows XP SP2 with the latest VC++ libraries,
// because it utilizes the Concurrency Runtime that is only supported on Windows
// XP SP3 and above.
class PROTOBUF_EXPORT CriticalSectionLock {
public:
CriticalSectionLock() { InitializeCriticalSection(&critical_section_); }
~CriticalSectionLock() { DeleteCriticalSection(&critical_section_); }
void lock() { EnterCriticalSection(&critical_section_); }
void unlock() { LeaveCriticalSection(&critical_section_); }
private:
CRITICAL_SECTION critical_section_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CriticalSectionLock);
};
#endif
// In MSVC std::mutex does not have a constexpr constructor.
// This wrapper makes the constructor constexpr.
template <typename T>
class CallOnceInitializedMutex {
public:
constexpr CallOnceInitializedMutex() : flag_{}, buf_{} {}
~CallOnceInitializedMutex() { get().~T(); }
void lock() { get().lock(); }
void unlock() { get().unlock(); }
private:
T& get() {
std::call_once(flag_, [&] { ::new (static_cast<void*>(&buf_)) T(); });
return reinterpret_cast<T&>(buf_);
}
std::once_flag flag_;
alignas(T) char buf_[sizeof(T)];
};
// Mutex is a natural type to wrap. As both google and other organization have
// specialized mutexes. gRPC also provides an injection mechanism for custom
// mutexes.
class GOOGLE_PROTOBUF_CAPABILITY("mutex") PROTOBUF_EXPORT WrappedMutex {
public:
#if defined(__QNX__)
constexpr WrappedMutex() = default;
#else
constexpr WrappedMutex() {}
#endif
void Lock() GOOGLE_PROTOBUF_ACQUIRE() { mu_.lock(); }
void Unlock() GOOGLE_PROTOBUF_RELEASE() { mu_.unlock(); }
// Crash if this Mutex is not held exclusively by this thread.
// May fail to crash when it should; will never crash when it should not.
void AssertHeld() const {}
private:
#if defined(GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP)
CallOnceInitializedMutex<CriticalSectionLock> mu_{};
#elif defined(_WIN32)
CallOnceInitializedMutex<std::mutex> mu_{};
#else
std::mutex mu_{};
#endif
};
using Mutex = WrappedMutex;
// MutexLock(mu) acquires mu when constructed and releases it when destroyed.
class PROTOBUF_EXPORT MutexLock {
public:
explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
~MutexLock() { this->mu_->Unlock(); }
private:
Mutex *const mu_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLock);
};
// TODO(kenton): Implement these? Hard to implement portably.
typedef MutexLock ReaderMutexLock;
typedef MutexLock WriterMutexLock;
// MutexLockMaybe is like MutexLock, but is a no-op when mu is nullptr.
class PROTOBUF_EXPORT MutexLockMaybe {
public:
explicit MutexLockMaybe(Mutex *mu) :
mu_(mu) { if (this->mu_ != nullptr) { this->mu_->Lock(); } }
~MutexLockMaybe() { if (this->mu_ != nullptr) { this->mu_->Unlock(); } }
private:
Mutex *const mu_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLockMaybe);
};
#if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
template<typename T>
class ThreadLocalStorage {
public:
ThreadLocalStorage() {
pthread_key_create(&key_, &ThreadLocalStorage::Delete);
}
~ThreadLocalStorage() {
pthread_key_delete(key_);
}
T* Get() {
T* result = static_cast<T*>(pthread_getspecific(key_));
if (result == nullptr) {
result = new T();
pthread_setspecific(key_, result);
}
return result;
}
private:
static void Delete(void* value) {
delete static_cast<T*>(value);
}
pthread_key_t key_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ThreadLocalStorage);
};
#endif
} // namespace internal
// We made these internal so that they would show up as such in the docs,
// but we don't want to stick "internal::" in front of them everywhere.
using internal::Mutex;
using internal::MutexLock;
using internal::ReaderMutexLock;
using internal::WriterMutexLock;
using internal::MutexLockMaybe;
} // namespace protobuf
} // namespace google
#undef GOOGLE_PROTOBUF_ACQUIRE
#undef GOOGLE_PROTOBUF_RELEASE
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_STUBS_MUTEX_H_

View File

@@ -0,0 +1,55 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef GOOGLE_PROTOBUF_STUBS_ONCE_H__
#define GOOGLE_PROTOBUF_STUBS_ONCE_H__
#include <mutex>
#include <utility>
#include <google/protobuf/port_def.inc>
namespace google {
namespace protobuf {
namespace internal {
using once_flag = std::once_flag;
template <typename... Args>
void call_once(Args&&... args ) {
std::call_once(std::forward<Args>(args)...);
}
} // namespace internal
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_STUBS_ONCE_H__

View File

@@ -0,0 +1,138 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2012 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef GOOGLE_PROTOBUF_PLATFORM_MACROS_H_
#define GOOGLE_PROTOBUF_PLATFORM_MACROS_H_
#define GOOGLE_PROTOBUF_PLATFORM_ERROR \
#error "Host platform was not detected as supported by protobuf"
// Processor architecture detection. For more info on what's defined, see:
// http://msdn.microsoft.com/en-us/library/b0084kay.aspx
// http://www.agner.org/optimize/calling_conventions.pdf
// or with gcc, run: "echo | gcc -E -dM -"
#if defined(_M_X64) || defined(__x86_64__)
#define GOOGLE_PROTOBUF_ARCH_X64 1
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
#elif defined(_M_IX86) || defined(__i386__)
#define GOOGLE_PROTOBUF_ARCH_IA32 1
#define GOOGLE_PROTOBUF_ARCH_32_BIT 1
#elif defined(__QNX__)
#define GOOGLE_PROTOBUF_ARCH_ARM_QNX 1
#if defined(__aarch64__)
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
#else
#define GOOGLE_PROTOBUF_ARCH_32_BIT 1
#endif
#elif defined(_M_ARM) || defined(__ARMEL__)
#define GOOGLE_PROTOBUF_ARCH_ARM 1
#define GOOGLE_PROTOBUF_ARCH_32_BIT 1
#elif defined(_M_ARM64)
#define GOOGLE_PROTOBUF_ARCH_ARM 1
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
#elif defined(__aarch64__)
#define GOOGLE_PROTOBUF_ARCH_AARCH64 1
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
#elif defined(__mips__)
#if defined(__LP64__)
#define GOOGLE_PROTOBUF_ARCH_MIPS64 1
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
#else
#define GOOGLE_PROTOBUF_ARCH_MIPS 1
#define GOOGLE_PROTOBUF_ARCH_32_BIT 1
#endif
#elif defined(__pnacl__)
#define GOOGLE_PROTOBUF_ARCH_32_BIT 1
#elif defined(sparc)
#define GOOGLE_PROTOBUF_ARCH_SPARC 1
#if defined(__sparc_v9__) || defined(__sparcv9) || defined(__arch64__)
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
#else
#define GOOGLE_PROTOBUF_ARCH_32_BIT 1
#endif
#elif defined(_POWER) || defined(__powerpc64__) || defined(__PPC64__)
#define GOOGLE_PROTOBUF_ARCH_POWER 1
#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
#elif defined(__PPC__)
#define GOOGLE_PROTOBUF_ARCH_PPC 1
#define GOOGLE_PROTOBUF_ARCH_32_BIT 1
#elif defined(__GNUC__)
# if (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4))
// We fallback to the generic Clang/GCC >= 4.7 implementation in atomicops.h
# elif defined(__clang__)
# if !__has_extension(c_atomic)
GOOGLE_PROTOBUF_PLATFORM_ERROR
# endif
// We fallback to the generic Clang/GCC >= 4.7 implementation in atomicops.h
# endif
# if __LP64__
# define GOOGLE_PROTOBUF_ARCH_64_BIT 1
# else
# define GOOGLE_PROTOBUF_ARCH_32_BIT 1
# endif
#else
GOOGLE_PROTOBUF_PLATFORM_ERROR
#endif
#if defined(__APPLE__)
#define GOOGLE_PROTOBUF_OS_APPLE
#include <Availability.h>
#include <TargetConditionals.h>
#if TARGET_OS_IPHONE
#define GOOGLE_PROTOBUF_OS_IPHONE
#endif
#elif defined(__EMSCRIPTEN__)
#define GOOGLE_PROTOBUF_OS_EMSCRIPTEN
#elif defined(__native_client__)
#define GOOGLE_PROTOBUF_OS_NACL
#elif defined(sun)
#define GOOGLE_PROTOBUF_OS_SOLARIS
#elif defined(_AIX)
#define GOOGLE_PROTOBUF_OS_AIX
#elif defined(__ANDROID__)
#define GOOGLE_PROTOBUF_OS_ANDROID
#endif
#undef GOOGLE_PROTOBUF_PLATFORM_ERROR
#if defined(GOOGLE_PROTOBUF_OS_ANDROID) || defined(GOOGLE_PROTOBUF_OS_IPHONE) || defined(__OpenBSD__)
// Android ndk does not support the __thread keyword very well yet. Here
// we use pthread_key_create()/pthread_getspecific()/... methods for
// TLS support on android.
// iOS and OpenBSD also do not support the __thread keyword.
#define GOOGLE_PROTOBUF_NO_THREADLOCAL
#endif
#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 1070
// __thread keyword requires at least 10.7
#define GOOGLE_PROTOBUF_NO_THREADLOCAL
#endif
#endif // GOOGLE_PROTOBUF_PLATFORM_MACROS_H_

View File

@@ -0,0 +1,410 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef GOOGLE_PROTOBUF_STUBS_PORT_H_
#define GOOGLE_PROTOBUF_STUBS_PORT_H_
#include <assert.h>
#include <cstdint>
#include <stdlib.h>
#include <cstddef>
#include <string>
#include <string.h>
#include <google/protobuf/stubs/platform_macros.h>
#include <google/protobuf/port_def.inc>
#undef PROTOBUF_LITTLE_ENDIAN
#ifdef _WIN32
// Assuming windows is always little-endian.
// TODO(xiaofeng): The PROTOBUF_LITTLE_ENDIAN is not only used for
// optimization but also for correctness. We should define an
// different macro to test the big-endian code path in coded_stream.
#if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
#define PROTOBUF_LITTLE_ENDIAN 1
#endif
#if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
// If MSVC has "/RTCc" set, it will complain about truncating casts at
// runtime. This file contains some intentional truncating casts.
#pragma runtime_checks("c", off)
#endif
#else
#ifdef __APPLE__
#include <machine/endian.h> // __BYTE_ORDER
#elif defined(__FreeBSD__)
#include <sys/endian.h> // __BYTE_ORDER
#else
#if !defined(__QNX__)
#include <endian.h> // __BYTE_ORDER
#endif
#endif
#if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
(defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
(defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN)) && \
!defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
#define PROTOBUF_LITTLE_ENDIAN 1
#endif
#endif
// These #includes are for the byte swap functions declared later on.
#ifdef _MSC_VER
#include <stdlib.h> // NOLINT(build/include)
#include <intrin.h>
#elif defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#elif defined(__linux__) || defined(__ANDROID__) || defined(__CYGWIN__)
#include <byteswap.h> // IWYU pragma: export
#endif
// Legacy: some users reference these (internal-only) macros even though we
// don't need them any more.
#if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS)
#ifdef LIBPROTOBUF_EXPORTS
#define LIBPROTOBUF_EXPORT __declspec(dllexport)
#else
#define LIBPROTOBUF_EXPORT __declspec(dllimport)
#endif
#ifdef LIBPROTOC_EXPORTS
#define LIBPROTOC_EXPORT __declspec(dllexport)
#else
#define LIBPROTOC_EXPORT __declspec(dllimport)
#endif
#else
#define LIBPROTOBUF_EXPORT
#define LIBPROTOC_EXPORT
#endif
#define PROTOBUF_RUNTIME_DEPRECATED(message) PROTOBUF_DEPRECATED_MSG(message)
#define GOOGLE_PROTOBUF_RUNTIME_DEPRECATED(message) \
PROTOBUF_DEPRECATED_MSG(message)
// ===================================================================
// from google3/base/port.h
#if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \
(defined(_MSC_VER) && _MSC_VER >= 1900))
// Define this to 1 if the code is compiled in C++11 mode; leave it
// undefined otherwise. Do NOT define it to 0 -- that causes
// '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'.
#define LANG_CXX11 1
#else
#error "Protobuf requires at least C++11."
#endif
namespace google {
namespace protobuf {
using ConstStringParam = const std::string &;
typedef unsigned int uint;
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
static const int32 kint32max = 0x7FFFFFFF;
static const int32 kint32min = -kint32max - 1;
static const int64 kint64max = int64_t{0x7FFFFFFFFFFFFFFF};
static const int64 kint64min = -kint64max - 1;
static const uint32 kuint32max = 0xFFFFFFFFu;
static const uint64 kuint64max = uint64_t{0xFFFFFFFFFFFFFFFFu};
#if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\
defined(MEMORY_SANITIZER)
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
uint16_t __sanitizer_unaligned_load16(const void *p);
uint32_t __sanitizer_unaligned_load32(const void *p);
uint64_t __sanitizer_unaligned_load64(const void *p);
void __sanitizer_unaligned_store16(void *p, uint16_t v);
void __sanitizer_unaligned_store32(void *p, uint32_t v);
void __sanitizer_unaligned_store64(void *p, uint64_t v);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
return __sanitizer_unaligned_load16(p);
}
inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
return __sanitizer_unaligned_load32(p);
}
inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
return __sanitizer_unaligned_load64(p);
}
inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
__sanitizer_unaligned_store16(p, v);
}
inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
__sanitizer_unaligned_store32(p, v);
}
inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
__sanitizer_unaligned_store64(p, v);
}
#elif defined(GOOGLE_PROTOBUF_USE_UNALIGNED) && GOOGLE_PROTOBUF_USE_UNALIGNED
#define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
#define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
#define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
#define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
#define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
#define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
#else
inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
uint16 t;
memcpy(&t, p, sizeof t);
return t;
}
inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
uint32 t;
memcpy(&t, p, sizeof t);
return t;
}
inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
uint64 t;
memcpy(&t, p, sizeof t);
return t;
}
inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
memcpy(p, &v, sizeof v);
}
inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
memcpy(p, &v, sizeof v);
}
inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
memcpy(p, &v, sizeof v);
}
#endif
#if defined(GOOGLE_PROTOBUF_OS_NACL) \
|| (defined(__ANDROID__) && defined(__clang__) \
&& (__clang_major__ == 3 && __clang_minor__ == 8) \
&& (__clang_patchlevel__ < 275480))
# define GOOGLE_PROTOBUF_USE_PORTABLE_LOG2
#endif
// The following guarantees declaration of the byte swap functions.
#ifdef _MSC_VER
#define bswap_16(x) _byteswap_ushort(x)
#define bswap_32(x) _byteswap_ulong(x)
#define bswap_64(x) _byteswap_uint64(x)
#elif defined(__APPLE__)
// Mac OS X / Darwin features
#define bswap_16(x) OSSwapInt16(x)
#define bswap_32(x) OSSwapInt32(x)
#define bswap_64(x) OSSwapInt64(x)
#elif !defined(__linux__) && !defined(__ANDROID__) && !defined(__CYGWIN__)
#ifndef bswap_16
static inline uint16 bswap_16(uint16 x) {
return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
}
#define bswap_16(x) bswap_16(x)
#endif
#ifndef bswap_32
static inline uint32 bswap_32(uint32 x) {
return (((x & 0xFF) << 24) |
((x & 0xFF00) << 8) |
((x & 0xFF0000) >> 8) |
((x & 0xFF000000) >> 24));
}
#define bswap_32(x) bswap_32(x)
#endif
#ifndef bswap_64
static inline uint64 bswap_64(uint64 x) {
return (((x & uint64_t{0xFFu}) << 56) |
((x & uint64_t{0xFF00u}) << 40) |
((x & uint64_t{0xFF0000u}) << 24) |
((x & uint64_t{0xFF000000u}) << 8) |
((x & uint64_t{0xFF00000000u}) >> 8) |
((x & uint64_t{0xFF0000000000u}) >> 24) |
((x & uint64_t{0xFF000000000000u}) >> 40) |
((x & uint64_t{0xFF00000000000000u}) >> 56));
}
#define bswap_64(x) bswap_64(x)
#endif
#endif
// ===================================================================
// from google3/util/bits/bits.h
class Bits {
public:
static uint32 Log2FloorNonZero(uint32 n) {
#if defined(__GNUC__)
return 31 ^ static_cast<uint32>(__builtin_clz(n));
#elif defined(_MSC_VER)
unsigned long where;
_BitScanReverse(&where, n);
return where;
#else
return Log2FloorNonZero_Portable(n);
#endif
}
static uint32 Log2FloorNonZero64(uint64 n) {
// Older versions of clang run into an instruction-selection failure when
// it encounters __builtin_clzll:
// https://bugs.chromium.org/p/nativeclient/issues/detail?id=4395
// This includes arm-nacl-clang and clang in older Android NDK versions.
// To work around this, when we build with those we use the portable
// implementation instead.
#if defined(__GNUC__) && !defined(GOOGLE_PROTOBUF_USE_PORTABLE_LOG2)
return 63 ^ static_cast<uint32>(__builtin_clzll(n));
#elif defined(_MSC_VER) && defined(_M_X64)
unsigned long where;
_BitScanReverse64(&where, n);
return where;
#else
return Log2FloorNonZero64_Portable(n);
#endif
}
private:
static int Log2FloorNonZero_Portable(uint32 n) {
if (n == 0)
return -1;
int log = 0;
uint32 value = n;
for (int i = 4; i >= 0; --i) {
int shift = (1 << i);
uint32 x = value >> shift;
if (x != 0) {
value = x;
log += shift;
}
}
assert(value == 1);
return log;
}
static int Log2FloorNonZero64_Portable(uint64 n) {
const uint32 topbits = static_cast<uint32>(n >> 32);
if (topbits == 0) {
// Top bits are zero, so scan in bottom bits
return static_cast<int>(Log2FloorNonZero(static_cast<uint32>(n)));
} else {
return 32 + static_cast<int>(Log2FloorNonZero(topbits));
}
}
};
// ===================================================================
// from google3/util/endian/endian.h
PROTOBUF_EXPORT uint32 ghtonl(uint32 x);
class BigEndian {
public:
#ifdef PROTOBUF_LITTLE_ENDIAN
static uint16 FromHost16(uint16 x) { return bswap_16(x); }
static uint16 ToHost16(uint16 x) { return bswap_16(x); }
static uint32 FromHost32(uint32 x) { return bswap_32(x); }
static uint32 ToHost32(uint32 x) { return bswap_32(x); }
static uint64 FromHost64(uint64 x) { return bswap_64(x); }
static uint64 ToHost64(uint64 x) { return bswap_64(x); }
static bool IsLittleEndian() { return true; }
#else
static uint16 FromHost16(uint16 x) { return x; }
static uint16 ToHost16(uint16 x) { return x; }
static uint32 FromHost32(uint32 x) { return x; }
static uint32 ToHost32(uint32 x) { return x; }
static uint64 FromHost64(uint64 x) { return x; }
static uint64 ToHost64(uint64 x) { return x; }
static bool IsLittleEndian() { return false; }
#endif /* ENDIAN */
// Functions to do unaligned loads and stores in big-endian order.
static uint16 Load16(const void *p) {
return ToHost16(GOOGLE_UNALIGNED_LOAD16(p));
}
static void Store16(void *p, uint16 v) {
GOOGLE_UNALIGNED_STORE16(p, FromHost16(v));
}
static uint32 Load32(const void *p) {
return ToHost32(GOOGLE_UNALIGNED_LOAD32(p));
}
static void Store32(void *p, uint32 v) {
GOOGLE_UNALIGNED_STORE32(p, FromHost32(v));
}
static uint64 Load64(const void *p) {
return ToHost64(GOOGLE_UNALIGNED_LOAD64(p));
}
static void Store64(void *p, uint64 v) {
GOOGLE_UNALIGNED_STORE64(p, FromHost64(v));
}
};
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_STUBS_PORT_H_

View File

@@ -0,0 +1,196 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef GOOGLE_PROTOBUF_STUBS_STATUS_H_
#define GOOGLE_PROTOBUF_STUBS_STATUS_H_
#include <string>
#include <google/protobuf/stubs/stringpiece.h>
#include <google/protobuf/port_def.inc>
namespace google {
namespace protobuf {
namespace util {
namespace status_internal {
// These values must match error codes defined in google/rpc/code.proto.
enum class StatusCode : int {
kOk = 0,
kCancelled = 1,
kUnknown = 2,
kInvalidArgument = 3,
kDeadlineExceeded = 4,
kNotFound = 5,
kAlreadyExists = 6,
kPermissionDenied = 7,
kUnauthenticated = 16,
kResourceExhausted = 8,
kFailedPrecondition = 9,
kAborted = 10,
kOutOfRange = 11,
kUnimplemented = 12,
kInternal = 13,
kUnavailable = 14,
kDataLoss = 15,
};
class PROTOBUF_EXPORT Status {
public:
// Creates a "successful" status.
Status();
// Create a status in the canonical error space with the specified
// code, and error message. If "code == 0", error_message is
// ignored and a Status object identical to Status::kOk is
// constructed.
Status(StatusCode error_code, StringPiece error_message);
Status(const Status&);
Status& operator=(const Status& x);
~Status() {}
// Accessor
bool ok() const { return error_code_ == StatusCode::kOk; }
StatusCode code() const { return error_code_; }
StringPiece message() const {
return error_message_;
}
bool operator==(const Status& x) const;
bool operator!=(const Status& x) const {
return !operator==(x);
}
// Return a combination of the error code name and message.
std::string ToString() const;
private:
StatusCode error_code_;
std::string error_message_;
};
// Returns an OK status, equivalent to a default constructed instance. Prefer
// usage of `OkStatus()` when constructing such an OK status.
PROTOBUF_EXPORT Status OkStatus();
// Prints a human-readable representation of 'x' to 'os'.
PROTOBUF_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x);
// These convenience functions return `true` if a given status matches the
// `StatusCode` error code of its associated function.
PROTOBUF_EXPORT bool IsAborted(const Status& status);
PROTOBUF_EXPORT bool IsAlreadyExists(const Status& status);
PROTOBUF_EXPORT bool IsCancelled(const Status& status);
PROTOBUF_EXPORT bool IsDataLoss(const Status& status);
PROTOBUF_EXPORT bool IsDeadlineExceeded(const Status& status);
PROTOBUF_EXPORT bool IsFailedPrecondition(const Status& status);
PROTOBUF_EXPORT bool IsInternal(const Status& status);
PROTOBUF_EXPORT bool IsInvalidArgument(const Status& status);
PROTOBUF_EXPORT bool IsNotFound(const Status& status);
PROTOBUF_EXPORT bool IsOutOfRange(const Status& status);
PROTOBUF_EXPORT bool IsPermissionDenied(const Status& status);
PROTOBUF_EXPORT bool IsResourceExhausted(const Status& status);
PROTOBUF_EXPORT bool IsUnauthenticated(const Status& status);
PROTOBUF_EXPORT bool IsUnavailable(const Status& status);
PROTOBUF_EXPORT bool IsUnimplemented(const Status& status);
PROTOBUF_EXPORT bool IsUnknown(const Status& status);
// These convenience functions create an `Status` object with an error code as
// indicated by the associated function name, using the error message passed in
// `message`.
//
// These functions are intentionally named `*Error` rather than `*Status` to
// match the names from Abseil:
// https://github.com/abseil/abseil-cpp/blob/2e9532cc6c701a8323d0cffb468999ab804095ab/absl/status/status.h#L716
PROTOBUF_EXPORT Status AbortedError(StringPiece message);
PROTOBUF_EXPORT Status AlreadyExistsError(StringPiece message);
PROTOBUF_EXPORT Status CancelledError(StringPiece message);
PROTOBUF_EXPORT Status DataLossError(StringPiece message);
PROTOBUF_EXPORT Status DeadlineExceededError(StringPiece message);
PROTOBUF_EXPORT Status FailedPreconditionError(StringPiece message);
PROTOBUF_EXPORT Status InternalError(StringPiece message);
PROTOBUF_EXPORT Status InvalidArgumentError(StringPiece message);
PROTOBUF_EXPORT Status NotFoundError(StringPiece message);
PROTOBUF_EXPORT Status OutOfRangeError(StringPiece message);
PROTOBUF_EXPORT Status PermissionDeniedError(StringPiece message);
PROTOBUF_EXPORT Status ResourceExhaustedError(StringPiece message);
PROTOBUF_EXPORT Status UnauthenticatedError(StringPiece message);
PROTOBUF_EXPORT Status UnavailableError(StringPiece message);
PROTOBUF_EXPORT Status UnimplementedError(StringPiece message);
PROTOBUF_EXPORT Status UnknownError(StringPiece message);
} // namespace status_internal
using ::google::protobuf::util::status_internal::Status;
using ::google::protobuf::util::status_internal::StatusCode;
using ::google::protobuf::util::status_internal::IsAborted;
using ::google::protobuf::util::status_internal::IsAlreadyExists;
using ::google::protobuf::util::status_internal::IsCancelled;
using ::google::protobuf::util::status_internal::IsDataLoss;
using ::google::protobuf::util::status_internal::IsDeadlineExceeded;
using ::google::protobuf::util::status_internal::IsFailedPrecondition;
using ::google::protobuf::util::status_internal::IsInternal;
using ::google::protobuf::util::status_internal::IsInvalidArgument;
using ::google::protobuf::util::status_internal::IsNotFound;
using ::google::protobuf::util::status_internal::IsOutOfRange;
using ::google::protobuf::util::status_internal::IsPermissionDenied;
using ::google::protobuf::util::status_internal::IsResourceExhausted;
using ::google::protobuf::util::status_internal::IsUnauthenticated;
using ::google::protobuf::util::status_internal::IsUnavailable;
using ::google::protobuf::util::status_internal::IsUnimplemented;
using ::google::protobuf::util::status_internal::IsUnknown;
using ::google::protobuf::util::status_internal::AbortedError;
using ::google::protobuf::util::status_internal::AlreadyExistsError;
using ::google::protobuf::util::status_internal::CancelledError;
using ::google::protobuf::util::status_internal::DataLossError;
using ::google::protobuf::util::status_internal::DeadlineExceededError;
using ::google::protobuf::util::status_internal::FailedPreconditionError;
using ::google::protobuf::util::status_internal::InternalError;
using ::google::protobuf::util::status_internal::InvalidArgumentError;
using ::google::protobuf::util::status_internal::NotFoundError;
using ::google::protobuf::util::status_internal::OkStatus;
using ::google::protobuf::util::status_internal::OutOfRangeError;
using ::google::protobuf::util::status_internal::PermissionDeniedError;
using ::google::protobuf::util::status_internal::ResourceExhaustedError;
using ::google::protobuf::util::status_internal::UnauthenticatedError;
using ::google::protobuf::util::status_internal::UnavailableError;
using ::google::protobuf::util::status_internal::UnimplementedError;
using ::google::protobuf::util::status_internal::UnknownError;
} // namespace util
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_

View File

@@ -0,0 +1,71 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// from google3/util/gtl/stl_util.h
#ifndef GOOGLE_PROTOBUF_STUBS_STL_UTIL_H__
#define GOOGLE_PROTOBUF_STUBS_STL_UTIL_H__
#include <google/protobuf/stubs/common.h>
namespace google {
namespace protobuf {
// Inside Google, this function implements a horrible, disgusting hack in which
// we reach into the string's private implementation and resize it without
// initializing the new bytes. In some cases doing this can significantly
// improve performance. However, since it's totally non-portable it has no
// place in open source code. Feel free to fill this function in with your
// own disgusting hack if you want the perf boost.
inline void STLStringResizeUninitialized(std::string* s, size_t new_size) {
s->resize(new_size);
}
// Return a mutable char* pointing to a string's internal buffer,
// which may not be null-terminated. Writing through this pointer will
// modify the string.
//
// string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
// next call to a string method that invalidates iterators.
//
// As of 2006-04, there is no standard-blessed way of getting a
// mutable reference to a string's internal buffer. However, issue 530
// (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530)
// proposes this as the method. According to Matt Austern, this should
// already work on all current implementations.
inline char* string_as_array(std::string* str) {
// DO NOT USE const_cast<char*>(str->data())! See the unittest for why.
return str->empty() ? nullptr : &*str->begin();
}
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_STUBS_STL_UTIL_H__

View File

@@ -0,0 +1,390 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A StringPiece points to part or all of a string, Cord, double-quoted string
// literal, or other string-like object. A StringPiece does *not* own the
// string to which it points. A StringPiece is not null-terminated.
//
// You can use StringPiece as a function or method parameter. A StringPiece
// parameter can receive a double-quoted string literal argument, a "const
// char*" argument, a string argument, or a StringPiece argument with no data
// copying. Systematic use of StringPiece for arguments reduces data
// copies and strlen() calls.
//
// Prefer passing StringPieces by value:
// void MyFunction(StringPiece arg);
// If circumstances require, you may also pass by const reference:
// void MyFunction(const StringPiece& arg); // not preferred
// Both of these have the same lifetime semantics. Passing by value
// generates slightly smaller code. For more discussion, see the thread
// go/stringpiecebyvalue on c-users.
//
// StringPiece is also suitable for local variables if you know that
// the lifetime of the underlying object is longer than the lifetime
// of your StringPiece variable.
//
// Beware of binding a StringPiece to a temporary:
// StringPiece sp = obj.MethodReturningString(); // BAD: lifetime problem
//
// This code is okay:
// string str = obj.MethodReturningString(); // str owns its contents
// StringPiece sp(str); // GOOD, because str outlives sp
//
// StringPiece is sometimes a poor choice for a return value and usually a poor
// choice for a data member. If you do use a StringPiece this way, it is your
// responsibility to ensure that the object pointed to by the StringPiece
// outlives the StringPiece.
//
// A StringPiece may represent just part of a string; thus the name "Piece".
// For example, when splitting a string, vector<StringPiece> is a natural data
// type for the output. For another example, a Cord is a non-contiguous,
// potentially very long string-like object. The Cord class has an interface
// that iteratively provides StringPiece objects that point to the
// successive pieces of a Cord object.
//
// A StringPiece is not null-terminated. If you write code that scans a
// StringPiece, you must check its length before reading any characters.
// Common idioms that work on null-terminated strings do not work on
// StringPiece objects.
//
// There are several ways to create a null StringPiece:
// StringPiece()
// StringPiece(nullptr)
// StringPiece(nullptr, 0)
// For all of the above, sp.data() == nullptr, sp.length() == 0,
// and sp.empty() == true. Also, if you create a StringPiece with
// a non-null pointer then sp.data() != nullptr. Once created,
// sp.data() will stay either nullptr or not-nullptr, except if you call
// sp.clear() or sp.set().
//
// Thus, you can use StringPiece(nullptr) to signal an out-of-band value
// that is different from other StringPiece values. This is similar
// to the way that const char* p1 = nullptr; is different from
// const char* p2 = "";.
//
// There are many ways to create an empty StringPiece:
// StringPiece()
// StringPiece(nullptr)
// StringPiece(nullptr, 0)
// StringPiece("")
// StringPiece("", 0)
// StringPiece("abcdef", 0)
// StringPiece("abcdef"+6, 0)
// For all of the above, sp.length() will be 0 and sp.empty() will be true.
// For some empty StringPiece values, sp.data() will be nullptr.
// For some empty StringPiece values, sp.data() will not be nullptr.
//
// Be careful not to confuse: null StringPiece and empty StringPiece.
// The set of empty StringPieces properly includes the set of null StringPieces.
// That is, every null StringPiece is an empty StringPiece,
// but some non-null StringPieces are empty Stringpieces too.
//
// All empty StringPiece values compare equal to each other.
// Even a null StringPieces compares equal to a non-null empty StringPiece:
// StringPiece() == StringPiece("", 0)
// StringPiece(nullptr) == StringPiece("abc", 0)
// StringPiece(nullptr, 0) == StringPiece("abcdef"+6, 0)
//
// Look carefully at this example:
// StringPiece("") == nullptr
// True or false? TRUE, because StringPiece::operator== converts
// the right-hand side from nullptr to StringPiece(nullptr),
// and then compares two zero-length spans of characters.
// However, we are working to make this example produce a compile error.
//
// Suppose you want to write:
// bool TestWhat?(StringPiece sp) { return sp == nullptr; } // BAD
// Do not do that. Write one of these instead:
// bool TestNull(StringPiece sp) { return sp.data() == nullptr; }
// bool TestEmpty(StringPiece sp) { return sp.empty(); }
// The intent of TestWhat? is unclear. Did you mean TestNull or TestEmpty?
// Right now, TestWhat? behaves likes TestEmpty.
// We are working to make TestWhat? produce a compile error.
// TestNull is good to test for an out-of-band signal.
// TestEmpty is good to test for an empty StringPiece.
//
// Caveats (again):
// (1) The lifetime of the pointed-to string (or piece of a string)
// must be longer than the lifetime of the StringPiece.
// (2) There may or may not be a '\0' character after the end of
// StringPiece data.
// (3) A null StringPiece is empty.
// An empty StringPiece may or may not be a null StringPiece.
#ifndef GOOGLE_PROTOBUF_STUBS_STRINGPIECE_H_
#define GOOGLE_PROTOBUF_STUBS_STRINGPIECE_H_
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <iosfwd>
#include <limits>
#include <string>
#include <google/protobuf/stubs/hash.h>
#include <google/protobuf/port_def.inc>
namespace google {
namespace protobuf {
namespace stringpiece_internal {
class PROTOBUF_EXPORT StringPiece {
public:
using traits_type = std::char_traits<char>;
using value_type = char;
using pointer = char*;
using const_pointer = const char*;
using reference = char&;
using const_reference = const char&;
using const_iterator = const char*;
using iterator = const_iterator;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using reverse_iterator = const_reverse_iterator;
using size_type = size_t;
using difference_type = std::ptrdiff_t;
private:
const char* ptr_;
size_type length_;
static constexpr size_type kMaxSize =
(std::numeric_limits<difference_type>::max)();
static size_type CheckSize(size_type size) {
#if !defined(NDEBUG) || defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
if (PROTOBUF_PREDICT_FALSE(size > kMaxSize)) {
// Some people grep for this message in logs
// so take care if you ever change it.
LogFatalSizeTooBig(size, "string length exceeds max size");
}
#endif
return size;
}
// Out-of-line error path.
static void LogFatalSizeTooBig(size_type size, const char* details);
public:
// We provide non-explicit singleton constructors so users can pass
// in a "const char*" or a "string" wherever a "StringPiece" is
// expected.
//
// Style guide exception granted:
// http://goto/style-guide-exception-20978288
StringPiece() : ptr_(nullptr), length_(0) {}
StringPiece(const char* str) // NOLINT(runtime/explicit)
: ptr_(str), length_(0) {
if (str != nullptr) {
length_ = CheckSize(strlen(str));
}
}
template <class Allocator>
StringPiece( // NOLINT(runtime/explicit)
const std::basic_string<char, std::char_traits<char>, Allocator>& str)
: ptr_(str.data()), length_(0) {
length_ = CheckSize(str.size());
}
StringPiece(const char* offset, size_type len)
: ptr_(offset), length_(CheckSize(len)) {}
// data() may return a pointer to a buffer with embedded NULs, and the
// returned buffer may or may not be null terminated. Therefore it is
// typically a mistake to pass data() to a routine that expects a NUL
// terminated string.
const_pointer data() const { return ptr_; }
size_type size() const { return length_; }
size_type length() const { return length_; }
bool empty() const { return length_ == 0; }
char operator[](size_type i) const {
assert(i < length_);
return ptr_[i];
}
void remove_prefix(size_type n) {
assert(length_ >= n);
ptr_ += n;
length_ -= n;
}
void remove_suffix(size_type n) {
assert(length_ >= n);
length_ -= n;
}
// returns {-1, 0, 1}
int compare(StringPiece x) const {
size_type min_size = length_ < x.length_ ? length_ : x.length_;
int r = memcmp(ptr_, x.ptr_, static_cast<size_t>(min_size));
if (r < 0) return -1;
if (r > 0) return 1;
if (length_ < x.length_) return -1;
if (length_ > x.length_) return 1;
return 0;
}
std::string as_string() const { return ToString(); }
// We also define ToString() here, since many other string-like
// interfaces name the routine that converts to a C++ string
// "ToString", and it's confusing to have the method that does that
// for a StringPiece be called "as_string()". We also leave the
// "as_string()" method defined here for existing code.
std::string ToString() const {
if (ptr_ == nullptr) return "";
return std::string(data(), static_cast<size_type>(size()));
}
explicit operator std::string() const { return ToString(); }
void CopyToString(std::string* target) const;
void AppendToString(std::string* target) const;
bool starts_with(StringPiece x) const {
return (length_ >= x.length_) &&
(memcmp(ptr_, x.ptr_, static_cast<size_t>(x.length_)) == 0);
}
bool ends_with(StringPiece x) const {
return ((length_ >= x.length_) &&
(memcmp(ptr_ + (length_-x.length_), x.ptr_,
static_cast<size_t>(x.length_)) == 0));
}
// Checks whether StringPiece starts with x and if so advances the beginning
// of it to past the match. It's basically a shortcut for starts_with
// followed by remove_prefix.
bool Consume(StringPiece x);
// Like above but for the end of the string.
bool ConsumeFromEnd(StringPiece x);
// standard STL container boilerplate
static const size_type npos;
const_iterator begin() const { return ptr_; }
const_iterator end() const { return ptr_ + length_; }
const_reverse_iterator rbegin() const {
return const_reverse_iterator(ptr_ + length_);
}
const_reverse_iterator rend() const {
return const_reverse_iterator(ptr_);
}
size_type max_size() const { return length_; }
size_type capacity() const { return length_; }
// cpplint.py emits a false positive [build/include_what_you_use]
size_type copy(char* buf, size_type n, size_type pos = 0) const; // NOLINT
bool contains(StringPiece s) const;
size_type find(StringPiece s, size_type pos = 0) const;
size_type find(char c, size_type pos = 0) const;
size_type rfind(StringPiece s, size_type pos = npos) const;
size_type rfind(char c, size_type pos = npos) const;
size_type find_first_of(StringPiece s, size_type pos = 0) const;
size_type find_first_of(char c, size_type pos = 0) const {
return find(c, pos);
}
size_type find_first_not_of(StringPiece s, size_type pos = 0) const;
size_type find_first_not_of(char c, size_type pos = 0) const;
size_type find_last_of(StringPiece s, size_type pos = npos) const;
size_type find_last_of(char c, size_type pos = npos) const {
return rfind(c, pos);
}
size_type find_last_not_of(StringPiece s, size_type pos = npos) const;
size_type find_last_not_of(char c, size_type pos = npos) const;
StringPiece substr(size_type pos, size_type n = npos) const;
};
// This large function is defined inline so that in a fairly common case where
// one of the arguments is a literal, the compiler can elide a lot of the
// following comparisons.
inline bool operator==(StringPiece x, StringPiece y) {
StringPiece::size_type len = x.size();
if (len != y.size()) {
return false;
}
return x.data() == y.data() || len <= 0 ||
memcmp(x.data(), y.data(), static_cast<size_t>(len)) == 0;
}
inline bool operator!=(StringPiece x, StringPiece y) {
return !(x == y);
}
inline bool operator<(StringPiece x, StringPiece y) {
const StringPiece::size_type min_size =
x.size() < y.size() ? x.size() : y.size();
const int r = memcmp(x.data(), y.data(), static_cast<size_t>(min_size));
return (r < 0) || (r == 0 && x.size() < y.size());
}
inline bool operator>(StringPiece x, StringPiece y) {
return y < x;
}
inline bool operator<=(StringPiece x, StringPiece y) {
return !(x > y);
}
inline bool operator>=(StringPiece x, StringPiece y) {
return !(x < y);
}
// allow StringPiece to be logged
extern std::ostream& operator<<(std::ostream& o, StringPiece piece);
} // namespace stringpiece_internal
using ::google::protobuf::stringpiece_internal::StringPiece;
} // namespace protobuf
} // namespace google
GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START
template<> struct hash<StringPiece> {
size_t operator()(const StringPiece& s) const {
size_t result = 0;
for (const char *str = s.data(), *end = str + s.size(); str < end; str++) {
result = 5 * result + static_cast<size_t>(*str);
}
return result;
}
};
GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END
#include <google/protobuf/port_undef.inc>
#endif // STRINGS_STRINGPIECE_H_

View File

@@ -0,0 +1,950 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// from google3/strings/strutil.h
#ifndef GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
#define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/stringpiece.h>
#include <stdlib.h>
#include <cstring>
#include <google/protobuf/port_def.inc>
#include <vector>
namespace google {
namespace protobuf {
#if defined(_MSC_VER) && _MSC_VER < 1800
#define strtoll _strtoi64
#define strtoull _strtoui64
#elif defined(__DECCXX) && defined(__osf__)
// HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit.
#define strtoll strtol
#define strtoull strtoul
#endif
// ----------------------------------------------------------------------
// ascii_isalnum()
// Check if an ASCII character is alphanumeric. We can't use ctype's
// isalnum() because it is affected by locale. This function is applied
// to identifiers in the protocol buffer language, not to natural-language
// strings, so locale should not be taken into account.
// ascii_isdigit()
// Like above, but only accepts digits.
// ascii_isspace()
// Check if the character is a space character.
// ----------------------------------------------------------------------
inline bool ascii_isalnum(char c) {
return ('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9');
}
inline bool ascii_isdigit(char c) {
return ('0' <= c && c <= '9');
}
inline bool ascii_isspace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' ||
c == '\r';
}
inline bool ascii_isupper(char c) {
return c >= 'A' && c <= 'Z';
}
inline bool ascii_islower(char c) {
return c >= 'a' && c <= 'z';
}
inline char ascii_toupper(char c) {
return ascii_islower(c) ? c - ('a' - 'A') : c;
}
inline char ascii_tolower(char c) {
return ascii_isupper(c) ? c + ('a' - 'A') : c;
}
inline int hex_digit_to_int(char c) {
/* Assume ASCII. */
int x = static_cast<unsigned char>(c);
if (x > '9') {
x += 9;
}
return x & 0xf;
}
// ----------------------------------------------------------------------
// HasPrefixString()
// Check if a string begins with a given prefix.
// StripPrefixString()
// Given a string and a putative prefix, returns the string minus the
// prefix string if the prefix matches, otherwise the original
// string.
// ----------------------------------------------------------------------
inline bool HasPrefixString(StringPiece str, StringPiece prefix) {
return str.size() >= prefix.size() &&
memcmp(str.data(), prefix.data(), prefix.size()) == 0;
}
inline std::string StripPrefixString(const std::string& str,
const std::string& prefix) {
if (HasPrefixString(str, prefix)) {
return str.substr(prefix.size());
} else {
return str;
}
}
// ----------------------------------------------------------------------
// HasSuffixString()
// Return true if str ends in suffix.
// StripSuffixString()
// Given a string and a putative suffix, returns the string minus the
// suffix string if the suffix matches, otherwise the original
// string.
// ----------------------------------------------------------------------
inline bool HasSuffixString(StringPiece str, StringPiece suffix) {
return str.size() >= suffix.size() &&
memcmp(str.data() + str.size() - suffix.size(), suffix.data(),
suffix.size()) == 0;
}
inline std::string StripSuffixString(const std::string& str,
const std::string& suffix) {
if (HasSuffixString(str, suffix)) {
return str.substr(0, str.size() - suffix.size());
} else {
return str;
}
}
// ----------------------------------------------------------------------
// ReplaceCharacters
// Replaces any occurrence of the character 'remove' (or the characters
// in 'remove') with the character 'replacewith'.
// Good for keeping html characters or protocol characters (\t) out
// of places where they might cause a problem.
// StripWhitespace
// Removes whitespaces from both ends of the given string.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT void ReplaceCharacters(std::string* s, const char* remove,
char replacewith);
PROTOBUF_EXPORT void StripWhitespace(std::string* s);
// ----------------------------------------------------------------------
// LowerString()
// UpperString()
// ToUpper()
// Convert the characters in "s" to lowercase or uppercase. ASCII-only:
// these functions intentionally ignore locale because they are applied to
// identifiers used in the Protocol Buffer language, not to natural-language
// strings.
// ----------------------------------------------------------------------
inline void LowerString(std::string* s) {
std::string::iterator end = s->end();
for (std::string::iterator i = s->begin(); i != end; ++i) {
// tolower() changes based on locale. We don't want this!
if ('A' <= *i && *i <= 'Z') *i += 'a' - 'A';
}
}
inline void UpperString(std::string* s) {
std::string::iterator end = s->end();
for (std::string::iterator i = s->begin(); i != end; ++i) {
// toupper() changes based on locale. We don't want this!
if ('a' <= *i && *i <= 'z') *i += 'A' - 'a';
}
}
inline void ToUpper(std::string* s) { UpperString(s); }
inline std::string ToUpper(const std::string& s) {
std::string out = s;
UpperString(&out);
return out;
}
// ----------------------------------------------------------------------
// StringReplace()
// Give me a string and two patterns "old" and "new", and I replace
// the first instance of "old" in the string with "new", if it
// exists. RETURN a new string, regardless of whether the replacement
// happened or not.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT std::string StringReplace(const std::string& s,
const std::string& oldsub,
const std::string& newsub,
bool replace_all);
// ----------------------------------------------------------------------
// SplitStringUsing()
// Split a string using a character delimiter. Append the components
// to 'result'. If there are consecutive delimiters, this function skips
// over all of them.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT void SplitStringUsing(StringPiece full, const char* delim,
std::vector<std::string>* res);
// Split a string using one or more byte delimiters, presented
// as a nul-terminated c string. Append the components to 'result'.
// If there are consecutive delimiters, this function will return
// corresponding empty strings. If you want to drop the empty
// strings, try SplitStringUsing().
//
// If "full" is the empty string, yields an empty string as the only value.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT void SplitStringAllowEmpty(StringPiece full, const char* delim,
std::vector<std::string>* result);
// ----------------------------------------------------------------------
// Split()
// Split a string using a character delimiter.
// ----------------------------------------------------------------------
inline std::vector<std::string> Split(StringPiece full, const char* delim,
bool skip_empty = true) {
std::vector<std::string> result;
if (skip_empty) {
SplitStringUsing(full, delim, &result);
} else {
SplitStringAllowEmpty(full, delim, &result);
}
return result;
}
// ----------------------------------------------------------------------
// JoinStrings()
// These methods concatenate a vector of strings into a C++ string, using
// the C-string "delim" as a separator between components. There are two
// flavors of the function, one flavor returns the concatenated string,
// another takes a pointer to the target string. In the latter case the
// target string is cleared and overwritten.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT void JoinStrings(const std::vector<std::string>& components,
const char* delim, std::string* result);
inline std::string JoinStrings(const std::vector<std::string>& components,
const char* delim) {
std::string result;
JoinStrings(components, delim, &result);
return result;
}
// ----------------------------------------------------------------------
// UnescapeCEscapeSequences()
// Copies "source" to "dest", rewriting C-style escape sequences
// -- '\n', '\r', '\\', '\ooo', etc -- to their ASCII
// equivalents. "dest" must be sufficiently large to hold all
// the characters in the rewritten string (i.e. at least as large
// as strlen(source) + 1 should be safe, since the replacements
// are always shorter than the original escaped sequences). It's
// safe for source and dest to be the same. RETURNS the length
// of dest.
//
// It allows hex sequences \xhh, or generally \xhhhhh with an
// arbitrary number of hex digits, but all of them together must
// specify a value of a single byte (e.g. \x0045 is equivalent
// to \x45, and \x1234 is erroneous).
//
// It also allows escape sequences of the form \uhhhh (exactly four
// hex digits, upper or lower case) or \Uhhhhhhhh (exactly eight
// hex digits, upper or lower case) to specify a Unicode code
// point. The dest array will contain the UTF8-encoded version of
// that code-point (e.g., if source contains \u2019, then dest will
// contain the three bytes 0xE2, 0x80, and 0x99).
//
// Errors: In the first form of the call, errors are reported with
// LOG(ERROR). The same is true for the second form of the call if
// the pointer to the string std::vector is nullptr; otherwise, error
// messages are stored in the std::vector. In either case, the effect on
// the dest array is not defined, but rest of the source will be
// processed.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest);
PROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest,
std::vector<std::string>* errors);
// ----------------------------------------------------------------------
// UnescapeCEscapeString()
// This does the same thing as UnescapeCEscapeSequences, but creates
// a new string. The caller does not need to worry about allocating
// a dest buffer. This should be used for non performance critical
// tasks such as printing debug messages. It is safe for src and dest
// to be the same.
//
// The second call stores its errors in a supplied string vector.
// If the string vector pointer is nullptr, it reports the errors with LOG().
//
// In the first and second calls, the length of dest is returned. In the
// the third call, the new string is returned.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT int UnescapeCEscapeString(const std::string& src,
std::string* dest);
PROTOBUF_EXPORT int UnescapeCEscapeString(const std::string& src,
std::string* dest,
std::vector<std::string>* errors);
PROTOBUF_EXPORT std::string UnescapeCEscapeString(const std::string& src);
// ----------------------------------------------------------------------
// CEscape()
// Escapes 'src' using C-style escape sequences and returns the resulting
// string.
//
// Escaped chars: \n, \r, \t, ", ', \, and !isprint().
// ----------------------------------------------------------------------
PROTOBUF_EXPORT std::string CEscape(const std::string& src);
// ----------------------------------------------------------------------
// CEscapeAndAppend()
// Escapes 'src' using C-style escape sequences, and appends the escaped
// string to 'dest'.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT void CEscapeAndAppend(StringPiece src, std::string* dest);
namespace strings {
// Like CEscape() but does not escape bytes with the upper bit set.
PROTOBUF_EXPORT std::string Utf8SafeCEscape(const std::string& src);
// Like CEscape() but uses hex (\x) escapes instead of octals.
PROTOBUF_EXPORT std::string CHexEscape(const std::string& src);
} // namespace strings
// ----------------------------------------------------------------------
// strto32()
// strtou32()
// strto64()
// strtou64()
// Architecture-neutral plug compatible replacements for strtol() and
// strtoul(). Long's have different lengths on ILP-32 and LP-64
// platforms, so using these is safer, from the point of view of
// overflow behavior, than using the standard libc functions.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT int32 strto32_adaptor(const char* nptr, char** endptr,
int base);
PROTOBUF_EXPORT uint32 strtou32_adaptor(const char* nptr, char** endptr,
int base);
inline int32 strto32(const char *nptr, char **endptr, int base) {
if (sizeof(int32) == sizeof(long))
return strtol(nptr, endptr, base);
else
return strto32_adaptor(nptr, endptr, base);
}
inline uint32 strtou32(const char *nptr, char **endptr, int base) {
if (sizeof(uint32) == sizeof(unsigned long))
return strtoul(nptr, endptr, base);
else
return strtou32_adaptor(nptr, endptr, base);
}
// For now, long long is 64-bit on all the platforms we care about, so these
// functions can simply pass the call to strto[u]ll.
inline int64 strto64(const char *nptr, char **endptr, int base) {
static_assert(sizeof(int64) == sizeof(long long),
"sizeof_int64_is_not_sizeof_long_long");
return strtoll(nptr, endptr, base);
}
inline uint64 strtou64(const char *nptr, char **endptr, int base) {
static_assert(sizeof(uint64) == sizeof(unsigned long long),
"sizeof_uint64_is_not_sizeof_long_long");
return strtoull(nptr, endptr, base);
}
// ----------------------------------------------------------------------
// safe_strtob()
// safe_strto32()
// safe_strtou32()
// safe_strto64()
// safe_strtou64()
// safe_strtof()
// safe_strtod()
// ----------------------------------------------------------------------
PROTOBUF_EXPORT bool safe_strtob(StringPiece str, bool* value);
PROTOBUF_EXPORT bool safe_strto32(const std::string& str, int32* value);
PROTOBUF_EXPORT bool safe_strtou32(const std::string& str, uint32* value);
inline bool safe_strto32(const char* str, int32* value) {
return safe_strto32(std::string(str), value);
}
inline bool safe_strto32(StringPiece str, int32* value) {
return safe_strto32(str.ToString(), value);
}
inline bool safe_strtou32(const char* str, uint32* value) {
return safe_strtou32(std::string(str), value);
}
inline bool safe_strtou32(StringPiece str, uint32* value) {
return safe_strtou32(str.ToString(), value);
}
PROTOBUF_EXPORT bool safe_strto64(const std::string& str, int64* value);
PROTOBUF_EXPORT bool safe_strtou64(const std::string& str, uint64* value);
inline bool safe_strto64(const char* str, int64* value) {
return safe_strto64(std::string(str), value);
}
inline bool safe_strto64(StringPiece str, int64* value) {
return safe_strto64(str.ToString(), value);
}
inline bool safe_strtou64(const char* str, uint64* value) {
return safe_strtou64(std::string(str), value);
}
inline bool safe_strtou64(StringPiece str, uint64* value) {
return safe_strtou64(str.ToString(), value);
}
PROTOBUF_EXPORT bool safe_strtof(const char* str, float* value);
PROTOBUF_EXPORT bool safe_strtod(const char* str, double* value);
inline bool safe_strtof(const std::string& str, float* value) {
return safe_strtof(str.c_str(), value);
}
inline bool safe_strtod(const std::string& str, double* value) {
return safe_strtod(str.c_str(), value);
}
inline bool safe_strtof(StringPiece str, float* value) {
return safe_strtof(str.ToString(), value);
}
inline bool safe_strtod(StringPiece str, double* value) {
return safe_strtod(str.ToString(), value);
}
// ----------------------------------------------------------------------
// FastIntToBuffer()
// FastHexToBuffer()
// FastHex64ToBuffer()
// FastHex32ToBuffer()
// FastTimeToBuffer()
// These are intended for speed. FastIntToBuffer() assumes the
// integer is non-negative. FastHexToBuffer() puts output in
// hex rather than decimal. FastTimeToBuffer() puts the output
// into RFC822 format.
//
// FastHex64ToBuffer() puts a 64-bit unsigned value in hex-format,
// padded to exactly 16 bytes (plus one byte for '\0')
//
// FastHex32ToBuffer() puts a 32-bit unsigned value in hex-format,
// padded to exactly 8 bytes (plus one byte for '\0')
//
// All functions take the output buffer as an arg.
// They all return a pointer to the beginning of the output,
// which may not be the beginning of the input buffer.
// ----------------------------------------------------------------------
// Suggested buffer size for FastToBuffer functions. Also works with
// DoubleToBuffer() and FloatToBuffer().
static const int kFastToBufferSize = 32;
PROTOBUF_EXPORT char* FastInt32ToBuffer(int32 i, char* buffer);
PROTOBUF_EXPORT char* FastInt64ToBuffer(int64 i, char* buffer);
char* FastUInt32ToBuffer(uint32 i, char* buffer); // inline below
char* FastUInt64ToBuffer(uint64 i, char* buffer); // inline below
PROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer);
PROTOBUF_EXPORT char* FastHex64ToBuffer(uint64 i, char* buffer);
PROTOBUF_EXPORT char* FastHex32ToBuffer(uint32 i, char* buffer);
// at least 22 bytes long
inline char* FastIntToBuffer(int i, char* buffer) {
return (sizeof(i) == 4 ?
FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
}
inline char* FastUIntToBuffer(unsigned int i, char* buffer) {
return (sizeof(i) == 4 ?
FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
}
inline char* FastLongToBuffer(long i, char* buffer) {
return (sizeof(i) == 4 ?
FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
}
inline char* FastULongToBuffer(unsigned long i, char* buffer) {
return (sizeof(i) == 4 ?
FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
}
// ----------------------------------------------------------------------
// FastInt32ToBufferLeft()
// FastUInt32ToBufferLeft()
// FastInt64ToBufferLeft()
// FastUInt64ToBufferLeft()
//
// Like the Fast*ToBuffer() functions above, these are intended for speed.
// Unlike the Fast*ToBuffer() functions, however, these functions write
// their output to the beginning of the buffer (hence the name, as the
// output is left-aligned). The caller is responsible for ensuring that
// the buffer has enough space to hold the output.
//
// Returns a pointer to the end of the string (i.e. the null character
// terminating the string).
// ----------------------------------------------------------------------
PROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32 i, char* buffer);
PROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32 i, char* buffer);
PROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64 i, char* buffer);
PROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64 i, char* buffer);
// Just define these in terms of the above.
inline char* FastUInt32ToBuffer(uint32 i, char* buffer) {
FastUInt32ToBufferLeft(i, buffer);
return buffer;
}
inline char* FastUInt64ToBuffer(uint64 i, char* buffer) {
FastUInt64ToBufferLeft(i, buffer);
return buffer;
}
inline std::string SimpleBtoa(bool value) { return value ? "true" : "false"; }
// ----------------------------------------------------------------------
// SimpleItoa()
// Description: converts an integer to a string.
//
// Return value: string
// ----------------------------------------------------------------------
PROTOBUF_EXPORT std::string SimpleItoa(int i);
PROTOBUF_EXPORT std::string SimpleItoa(unsigned int i);
PROTOBUF_EXPORT std::string SimpleItoa(long i);
PROTOBUF_EXPORT std::string SimpleItoa(unsigned long i);
PROTOBUF_EXPORT std::string SimpleItoa(long long i);
PROTOBUF_EXPORT std::string SimpleItoa(unsigned long long i);
// ----------------------------------------------------------------------
// SimpleDtoa()
// SimpleFtoa()
// DoubleToBuffer()
// FloatToBuffer()
// Description: converts a double or float to a string which, if
// passed to NoLocaleStrtod(), will produce the exact same original double
// (except in case of NaN; all NaNs are considered the same value).
// We try to keep the string short but it's not guaranteed to be as
// short as possible.
//
// DoubleToBuffer() and FloatToBuffer() write the text to the given
// buffer and return it. The buffer must be at least
// kDoubleToBufferSize bytes for doubles and kFloatToBufferSize
// bytes for floats. kFastToBufferSize is also guaranteed to be large
// enough to hold either.
//
// Return value: string
// ----------------------------------------------------------------------
PROTOBUF_EXPORT std::string SimpleDtoa(double value);
PROTOBUF_EXPORT std::string SimpleFtoa(float value);
PROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer);
PROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer);
// In practice, doubles should never need more than 24 bytes and floats
// should never need more than 14 (including null terminators), but we
// overestimate to be safe.
static const int kDoubleToBufferSize = 32;
static const int kFloatToBufferSize = 24;
namespace strings {
enum PadSpec {
NO_PAD = 1,
ZERO_PAD_2,
ZERO_PAD_3,
ZERO_PAD_4,
ZERO_PAD_5,
ZERO_PAD_6,
ZERO_PAD_7,
ZERO_PAD_8,
ZERO_PAD_9,
ZERO_PAD_10,
ZERO_PAD_11,
ZERO_PAD_12,
ZERO_PAD_13,
ZERO_PAD_14,
ZERO_PAD_15,
ZERO_PAD_16,
};
struct Hex {
uint64 value;
enum PadSpec spec;
template <class Int>
explicit Hex(Int v, PadSpec s = NO_PAD)
: spec(s) {
// Prevent sign-extension by casting integers to
// their unsigned counterparts.
#ifdef LANG_CXX11
static_assert(
sizeof(v) == 1 || sizeof(v) == 2 || sizeof(v) == 4 || sizeof(v) == 8,
"Unknown integer type");
#endif
value = sizeof(v) == 1 ? static_cast<uint8>(v)
: sizeof(v) == 2 ? static_cast<uint16>(v)
: sizeof(v) == 4 ? static_cast<uint32>(v)
: static_cast<uint64>(v);
}
};
struct PROTOBUF_EXPORT AlphaNum {
const char *piece_data_; // move these to string_ref eventually
size_t piece_size_; // move these to string_ref eventually
char digits[kFastToBufferSize];
// No bool ctor -- bools convert to an integral type.
// A bool ctor would also convert incoming pointers (bletch).
AlphaNum(int i32)
: piece_data_(digits),
piece_size_(FastInt32ToBufferLeft(i32, digits) - &digits[0]) {}
AlphaNum(unsigned int u32)
: piece_data_(digits),
piece_size_(FastUInt32ToBufferLeft(u32, digits) - &digits[0]) {}
AlphaNum(long long i64)
: piece_data_(digits),
piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {}
AlphaNum(unsigned long long u64)
: piece_data_(digits),
piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {}
// Note: on some architectures, "long" is only 32 bits, not 64, but the
// performance hit of using FastInt64ToBufferLeft to handle 32-bit values
// is quite minor.
AlphaNum(long i64)
: piece_data_(digits),
piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {}
AlphaNum(unsigned long u64)
: piece_data_(digits),
piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {}
AlphaNum(float f)
: piece_data_(digits), piece_size_(strlen(FloatToBuffer(f, digits))) {}
AlphaNum(double f)
: piece_data_(digits), piece_size_(strlen(DoubleToBuffer(f, digits))) {}
AlphaNum(Hex hex);
AlphaNum(const char* c_str)
: piece_data_(c_str), piece_size_(strlen(c_str)) {}
// TODO: Add a string_ref constructor, eventually
// AlphaNum(const StringPiece &pc) : piece(pc) {}
AlphaNum(const std::string& str)
: piece_data_(str.data()), piece_size_(str.size()) {}
AlphaNum(StringPiece str)
: piece_data_(str.data()), piece_size_(str.size()) {}
size_t size() const { return piece_size_; }
const char *data() const { return piece_data_; }
private:
// Use ":" not ':'
AlphaNum(char c); // NOLINT(runtime/explicit)
// Disallow copy and assign.
AlphaNum(const AlphaNum&);
void operator=(const AlphaNum&);
};
} // namespace strings
using strings::AlphaNum;
// ----------------------------------------------------------------------
// StrCat()
// This merges the given strings or numbers, with no delimiter. This
// is designed to be the fastest possible way to construct a string out
// of a mix of raw C strings, strings, bool values,
// and numeric values.
//
// Don't use this for user-visible strings. The localization process
// works poorly on strings built up out of fragments.
//
// For clarity and performance, don't use StrCat when appending to a
// string. In particular, avoid using any of these (anti-)patterns:
// str.append(StrCat(...)
// str += StrCat(...)
// str = StrCat(str, ...)
// where the last is the worse, with the potential to change a loop
// from a linear time operation with O(1) dynamic allocations into a
// quadratic time operation with O(n) dynamic allocations. StrAppend
// is a better choice than any of the above, subject to the restriction
// of StrAppend(&str, a, b, c, ...) that none of the a, b, c, ... may
// be a reference into str.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b);
PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b,
const AlphaNum& c);
PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b,
const AlphaNum& c, const AlphaNum& d);
PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b,
const AlphaNum& c, const AlphaNum& d,
const AlphaNum& e);
PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b,
const AlphaNum& c, const AlphaNum& d,
const AlphaNum& e, const AlphaNum& f);
PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b,
const AlphaNum& c, const AlphaNum& d,
const AlphaNum& e, const AlphaNum& f,
const AlphaNum& g);
PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b,
const AlphaNum& c, const AlphaNum& d,
const AlphaNum& e, const AlphaNum& f,
const AlphaNum& g, const AlphaNum& h);
PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b,
const AlphaNum& c, const AlphaNum& d,
const AlphaNum& e, const AlphaNum& f,
const AlphaNum& g, const AlphaNum& h,
const AlphaNum& i);
inline std::string StrCat(const AlphaNum& a) {
return std::string(a.data(), a.size());
}
// ----------------------------------------------------------------------
// StrAppend()
// Same as above, but adds the output to the given string.
// WARNING: For speed, StrAppend does not try to check each of its input
// arguments to be sure that they are not a subset of the string being
// appended to. That is, while this will work:
//
// string s = "foo";
// s += s;
//
// This will not (necessarily) work:
//
// string s = "foo";
// StrAppend(&s, s);
//
// Note: while StrCat supports appending up to 9 arguments, StrAppend
// is currently limited to 4. That's rarely an issue except when
// automatically transforming StrCat to StrAppend, and can easily be
// worked around as consecutive calls to StrAppend are quite efficient.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a);
PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a,
const AlphaNum& b);
PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a,
const AlphaNum& b, const AlphaNum& c);
PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a,
const AlphaNum& b, const AlphaNum& c,
const AlphaNum& d);
// ----------------------------------------------------------------------
// Join()
// These methods concatenate a range of components into a C++ string, using
// the C-string "delim" as a separator between components.
// ----------------------------------------------------------------------
template <typename Iterator>
void Join(Iterator start, Iterator end, const char* delim,
std::string* result) {
for (Iterator it = start; it != end; ++it) {
if (it != start) {
result->append(delim);
}
StrAppend(result, *it);
}
}
template <typename Range>
std::string Join(const Range& components, const char* delim) {
std::string result;
Join(components.begin(), components.end(), delim, &result);
return result;
}
// ----------------------------------------------------------------------
// ToHex()
// Return a lower-case hex string representation of the given integer.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT std::string ToHex(uint64 num);
// ----------------------------------------------------------------------
// GlobalReplaceSubstring()
// Replaces all instances of a substring in a string. Does nothing
// if 'substring' is empty. Returns the number of replacements.
//
// NOTE: The string pieces must not overlap s.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT int GlobalReplaceSubstring(const std::string& substring,
const std::string& replacement,
std::string* s);
// ----------------------------------------------------------------------
// Base64Unescape()
// Converts "src" which is encoded in Base64 to its binary equivalent and
// writes it to "dest". If src contains invalid characters, dest is cleared
// and the function returns false. Returns true on success.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT bool Base64Unescape(StringPiece src, std::string* dest);
// ----------------------------------------------------------------------
// WebSafeBase64Unescape()
// This is a variation of Base64Unescape which uses '-' instead of '+', and
// '_' instead of '/'. src is not null terminated, instead specify len. I
// recommend that slen<szdest, but we honor szdest anyway.
// RETURNS the length of dest, or -1 if src contains invalid chars.
// The variation that stores into a string clears the string first, and
// returns false (with dest empty) if src contains invalid chars; for
// this version src and dest must be different strings.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT int WebSafeBase64Unescape(const char* src, int slen, char* dest,
int szdest);
PROTOBUF_EXPORT bool WebSafeBase64Unescape(StringPiece src, std::string* dest);
// Return the length to use for the output buffer given to the base64 escape
// routines. Make sure to use the same value for do_padding in both.
// This function may return incorrect results if given input_len values that
// are extremely high, which should happen rarely.
PROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len, bool do_padding);
// Use this version when calling Base64Escape without a do_padding arg.
PROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len);
// ----------------------------------------------------------------------
// Base64Escape()
// WebSafeBase64Escape()
// Encode "src" to "dest" using base64 encoding.
// src is not null terminated, instead specify len.
// 'dest' should have at least CalculateBase64EscapedLen() length.
// RETURNS the length of dest.
// The WebSafe variation use '-' instead of '+' and '_' instead of '/'
// so that we can place the out in the URL or cookies without having
// to escape them. It also has an extra parameter "do_padding",
// which when set to false will prevent padding with "=".
// ----------------------------------------------------------------------
PROTOBUF_EXPORT int Base64Escape(const unsigned char* src, int slen, char* dest,
int szdest);
PROTOBUF_EXPORT int WebSafeBase64Escape(const unsigned char* src, int slen,
char* dest, int szdest,
bool do_padding);
// Encode src into dest with padding.
PROTOBUF_EXPORT void Base64Escape(StringPiece src, std::string* dest);
// Encode src into dest web-safely without padding.
PROTOBUF_EXPORT void WebSafeBase64Escape(StringPiece src, std::string* dest);
// Encode src into dest web-safely with padding.
PROTOBUF_EXPORT void WebSafeBase64EscapeWithPadding(StringPiece src,
std::string* dest);
PROTOBUF_EXPORT void Base64Escape(const unsigned char* src, int szsrc,
std::string* dest, bool do_padding);
PROTOBUF_EXPORT void WebSafeBase64Escape(const unsigned char* src, int szsrc,
std::string* dest, bool do_padding);
inline bool IsValidCodePoint(uint32 code_point) {
return code_point < 0xD800 ||
(code_point >= 0xE000 && code_point <= 0x10FFFF);
}
static const int UTFmax = 4;
// ----------------------------------------------------------------------
// EncodeAsUTF8Char()
// Helper to append a Unicode code point to a string as UTF8, without bringing
// in any external dependencies. The output buffer must be as least 4 bytes
// large.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT int EncodeAsUTF8Char(uint32 code_point, char* output);
// ----------------------------------------------------------------------
// UTF8FirstLetterNumBytes()
// Length of the first UTF-8 character.
// ----------------------------------------------------------------------
PROTOBUF_EXPORT int UTF8FirstLetterNumBytes(const char* src, int len);
// From google3/third_party/absl/strings/escaping.h
// ----------------------------------------------------------------------
// CleanStringLineEndings()
// Clean up a multi-line string to conform to Unix line endings.
// Reads from src and appends to dst, so usually dst should be empty.
//
// If there is no line ending at the end of a non-empty string, it can
// be added automatically.
//
// Four different types of input are correctly handled:
//
// - Unix/Linux files: line ending is LF: pass through unchanged
//
// - DOS/Windows files: line ending is CRLF: convert to LF
//
// - Legacy Mac files: line ending is CR: convert to LF
//
// - Garbled files: random line endings: convert gracefully
// lonely CR, lonely LF, CRLF: convert to LF
//
// @param src The multi-line string to convert
// @param dst The converted string is appended to this string
// @param auto_end_last_line Automatically terminate the last line
//
// Limitations:
//
// This does not do the right thing for CRCRLF files created by
// broken programs that do another Unix->DOS conversion on files
// that are already in CRLF format. For this, a two-pass approach
// brute-force would be needed that
//
// (1) determines the presence of LF (first one is ok)
// (2) if yes, removes any CR, else convert every CR to LF
PROTOBUF_EXPORT void CleanStringLineEndings(const std::string& src,
std::string* dst,
bool auto_end_last_line);
// Same as above, but transforms the argument in place.
PROTOBUF_EXPORT void CleanStringLineEndings(std::string* str,
bool auto_end_last_line);
namespace strings {
inline bool EndsWith(StringPiece text, StringPiece suffix) {
return suffix.empty() ||
(text.size() >= suffix.size() &&
memcmp(text.data() + (text.size() - suffix.size()), suffix.data(),
suffix.size()) == 0);
}
} // namespace strings
namespace internal {
// A locale-independent version of the standard strtod(), which always
// uses a dot as the decimal separator.
double NoLocaleStrtod(const char* str, char** endptr);
} // namespace internal
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_STUBS_STRUTIL_H__

View File

@@ -0,0 +1,138 @@
// Copyright 2005 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ----
// Author: lar@google.com (Laramie Leavitt)
//
// Template metaprogramming utility functions.
//
// This code is compiled directly on many platforms, including client
// platforms like Windows, Mac, and embedded systems. Before making
// any changes here, make sure that you're not breaking any platforms.
//
//
// The names chosen here reflect those used in tr1 and the boost::mpl
// library, there are similar operations used in the Loki library as
// well. I prefer the boost names for 2 reasons:
// 1. I think that portions of the Boost libraries are more likely to
// be included in the c++ standard.
// 2. It is not impossible that some of the boost libraries will be
// included in our own build in the future.
// Both of these outcomes means that we may be able to directly replace
// some of these with boost equivalents.
//
#ifndef GOOGLE_PROTOBUF_TEMPLATE_UTIL_H_
#define GOOGLE_PROTOBUF_TEMPLATE_UTIL_H_
namespace google {
namespace protobuf {
namespace internal {
// Types small_ and big_ are guaranteed such that sizeof(small_) <
// sizeof(big_)
typedef char small_;
struct big_ {
char dummy[2];
};
// Identity metafunction.
template <class T>
struct identity_ {
typedef T type;
};
// integral_constant, defined in tr1, is a wrapper for an integer
// value. We don't really need this generality; we could get away
// with hardcoding the integer type to bool. We use the fully
// general integer_constant for compatibility with tr1.
template<class T, T v>
struct integral_constant {
static const T value = v;
typedef T value_type;
typedef integral_constant<T, v> type;
};
template <class T, T v> const T integral_constant<T, v>::value;
// Abbreviations: true_type and false_type are structs that represent boolean
// true and false values. Also define the boost::mpl versions of those names,
// true_ and false_.
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
typedef true_type true_;
typedef false_type false_;
// if_ is a templatized conditional statement.
// if_<cond, A, B> is a compile time evaluation of cond.
// if_<>::type contains A if cond is true, B otherwise.
template<bool cond, typename A, typename B>
struct if_{
typedef A type;
};
template<typename A, typename B>
struct if_<false, A, B> {
typedef B type;
};
// type_equals_ is a template type comparator, similar to Loki IsSameType.
// type_equals_<A, B>::value is true iff "A" is the same type as "B".
//
// New code should prefer base::is_same, defined in base/type_traits.h.
// It is functionally identical, but is_same is the standard spelling.
template<typename A, typename B>
struct type_equals_ : public false_ {
};
template<typename A>
struct type_equals_<A, A> : public true_ {
};
// and_ is a template && operator.
// and_<A, B>::value evaluates "A::value && B::value".
template<typename A, typename B>
struct and_ : public integral_constant<bool, (A::value && B::value)> {
};
// or_ is a template || operator.
// or_<A, B>::value evaluates "A::value || B::value".
template<typename A, typename B>
struct or_ : public integral_constant<bool, (A::value || B::value)> {
};
} // namespace internal
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_TEMPLATE_UTIL_H_