Commit 37c7426b authored by xiaofeng@google.com's avatar xiaofeng@google.com

Replace kEmptyString wth OnceInit initialized string*

parent f0b6a5cf
...@@ -53,7 +53,7 @@ void SetStringVariables(const FieldDescriptor* descriptor, ...@@ -53,7 +53,7 @@ void SetStringVariables(const FieldDescriptor* descriptor,
(*variables)["default_length"] = (*variables)["default_length"] =
SimpleItoa(descriptor->default_value_string().length()); SimpleItoa(descriptor->default_value_string().length());
(*variables)["default_variable"] = descriptor->default_value_string().empty() (*variables)["default_variable"] = descriptor->default_value_string().empty()
? "&::google::protobuf::internal::kEmptyString" ? "&::google::protobuf::internal::GetEmptyString()"
: "_default_" + FieldName(descriptor) + "_"; : "_default_" + FieldName(descriptor) + "_";
(*variables)["pointer_type"] = (*variables)["pointer_type"] =
descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char"; descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char";
......
...@@ -184,7 +184,7 @@ CodeGeneratorRequest::CodeGeneratorRequest(const CodeGeneratorRequest& from) ...@@ -184,7 +184,7 @@ CodeGeneratorRequest::CodeGeneratorRequest(const CodeGeneratorRequest& from)
void CodeGeneratorRequest::SharedCtor() { void CodeGeneratorRequest::SharedCtor() {
_cached_size_ = 0; _cached_size_ = 0;
parameter_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); parameter_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyString());
::memset(_has_bits_, 0, sizeof(_has_bits_)); ::memset(_has_bits_, 0, sizeof(_has_bits_));
} }
...@@ -193,7 +193,7 @@ CodeGeneratorRequest::~CodeGeneratorRequest() { ...@@ -193,7 +193,7 @@ CodeGeneratorRequest::~CodeGeneratorRequest() {
} }
void CodeGeneratorRequest::SharedDtor() { void CodeGeneratorRequest::SharedDtor() {
if (parameter_ != &::google::protobuf::internal::kEmptyString) { if (parameter_ != &::google::protobuf::internal::GetEmptyString()) {
delete parameter_; delete parameter_;
} }
if (this != default_instance_) { if (this != default_instance_) {
...@@ -224,7 +224,7 @@ CodeGeneratorRequest* CodeGeneratorRequest::New() const { ...@@ -224,7 +224,7 @@ CodeGeneratorRequest* CodeGeneratorRequest::New() const {
void CodeGeneratorRequest::Clear() { void CodeGeneratorRequest::Clear() {
if (_has_bits_[1 / 32] & (0xffu << (1 % 32))) { if (_has_bits_[1 / 32] & (0xffu << (1 % 32))) {
if (has_parameter()) { if (has_parameter()) {
if (parameter_ != &::google::protobuf::internal::kEmptyString) { if (parameter_ != &::google::protobuf::internal::GetEmptyString()) {
parameter_->clear(); parameter_->clear();
} }
} }
...@@ -501,9 +501,9 @@ CodeGeneratorResponse_File::CodeGeneratorResponse_File(const CodeGeneratorRespon ...@@ -501,9 +501,9 @@ CodeGeneratorResponse_File::CodeGeneratorResponse_File(const CodeGeneratorRespon
void CodeGeneratorResponse_File::SharedCtor() { void CodeGeneratorResponse_File::SharedCtor() {
_cached_size_ = 0; _cached_size_ = 0;
name_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyString());
insertion_point_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); insertion_point_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyString());
content_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); content_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyString());
::memset(_has_bits_, 0, sizeof(_has_bits_)); ::memset(_has_bits_, 0, sizeof(_has_bits_));
} }
...@@ -512,13 +512,13 @@ CodeGeneratorResponse_File::~CodeGeneratorResponse_File() { ...@@ -512,13 +512,13 @@ CodeGeneratorResponse_File::~CodeGeneratorResponse_File() {
} }
void CodeGeneratorResponse_File::SharedDtor() { void CodeGeneratorResponse_File::SharedDtor() {
if (name_ != &::google::protobuf::internal::kEmptyString) { if (name_ != &::google::protobuf::internal::GetEmptyString()) {
delete name_; delete name_;
} }
if (insertion_point_ != &::google::protobuf::internal::kEmptyString) { if (insertion_point_ != &::google::protobuf::internal::GetEmptyString()) {
delete insertion_point_; delete insertion_point_;
} }
if (content_ != &::google::protobuf::internal::kEmptyString) { if (content_ != &::google::protobuf::internal::GetEmptyString()) {
delete content_; delete content_;
} }
if (this != default_instance_) { if (this != default_instance_) {
...@@ -549,17 +549,17 @@ CodeGeneratorResponse_File* CodeGeneratorResponse_File::New() const { ...@@ -549,17 +549,17 @@ CodeGeneratorResponse_File* CodeGeneratorResponse_File::New() const {
void CodeGeneratorResponse_File::Clear() { void CodeGeneratorResponse_File::Clear() {
if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (has_name()) { if (has_name()) {
if (name_ != &::google::protobuf::internal::kEmptyString) { if (name_ != &::google::protobuf::internal::GetEmptyString()) {
name_->clear(); name_->clear();
} }
} }
if (has_insertion_point()) { if (has_insertion_point()) {
if (insertion_point_ != &::google::protobuf::internal::kEmptyString) { if (insertion_point_ != &::google::protobuf::internal::GetEmptyString()) {
insertion_point_->clear(); insertion_point_->clear();
} }
} }
if (has_content()) { if (has_content()) {
if (content_ != &::google::protobuf::internal::kEmptyString) { if (content_ != &::google::protobuf::internal::GetEmptyString()) {
content_->clear(); content_->clear();
} }
} }
...@@ -839,7 +839,7 @@ CodeGeneratorResponse::CodeGeneratorResponse(const CodeGeneratorResponse& from) ...@@ -839,7 +839,7 @@ CodeGeneratorResponse::CodeGeneratorResponse(const CodeGeneratorResponse& from)
void CodeGeneratorResponse::SharedCtor() { void CodeGeneratorResponse::SharedCtor() {
_cached_size_ = 0; _cached_size_ = 0;
error_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); error_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyString());
::memset(_has_bits_, 0, sizeof(_has_bits_)); ::memset(_has_bits_, 0, sizeof(_has_bits_));
} }
...@@ -848,7 +848,7 @@ CodeGeneratorResponse::~CodeGeneratorResponse() { ...@@ -848,7 +848,7 @@ CodeGeneratorResponse::~CodeGeneratorResponse() {
} }
void CodeGeneratorResponse::SharedDtor() { void CodeGeneratorResponse::SharedDtor() {
if (error_ != &::google::protobuf::internal::kEmptyString) { if (error_ != &::google::protobuf::internal::GetEmptyString()) {
delete error_; delete error_;
} }
if (this != default_instance_) { if (this != default_instance_) {
...@@ -879,7 +879,7 @@ CodeGeneratorResponse* CodeGeneratorResponse::New() const { ...@@ -879,7 +879,7 @@ CodeGeneratorResponse* CodeGeneratorResponse::New() const {
void CodeGeneratorResponse::Clear() { void CodeGeneratorResponse::Clear() {
if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (has_error()) { if (has_error()) {
if (error_ != &::google::protobuf::internal::kEmptyString) { if (error_ != &::google::protobuf::internal::GetEmptyString()) {
error_->clear(); error_->clear();
} }
} }
......
This diff is collapsed.
...@@ -139,8 +139,6 @@ const int FieldDescriptor::kLastReservedNumber; ...@@ -139,8 +139,6 @@ const int FieldDescriptor::kLastReservedNumber;
namespace { namespace {
const string kEmptyString;
string ToCamelCase(const string& input) { string ToCamelCase(const string& input) {
bool capitalize_next = false; bool capitalize_next = false;
string result; string result;
...@@ -2757,7 +2755,7 @@ Symbol DescriptorBuilder::NewPlaceholder(const string& name, ...@@ -2757,7 +2755,7 @@ Symbol DescriptorBuilder::NewPlaceholder(const string& name,
placeholder_name = tables_->AllocateString( placeholder_name = tables_->AllocateString(
placeholder_full_name->substr(dotpos + 1)); placeholder_full_name->substr(dotpos + 1));
} else { } else {
placeholder_package = &kEmptyString; placeholder_package = &::google::protobuf::internal::GetEmptyString();
placeholder_name = placeholder_full_name; placeholder_name = placeholder_full_name;
} }
...@@ -2843,7 +2841,7 @@ const FileDescriptor* DescriptorBuilder::NewPlaceholderFile( ...@@ -2843,7 +2841,7 @@ const FileDescriptor* DescriptorBuilder::NewPlaceholderFile(
memset(placeholder, 0, sizeof(*placeholder)); memset(placeholder, 0, sizeof(*placeholder));
placeholder->name_ = tables_->AllocateString(name); placeholder->name_ = tables_->AllocateString(name);
placeholder->package_ = &kEmptyString; placeholder->package_ = &::google::protobuf::internal::GetEmptyString();
placeholder->pool_ = pool_; placeholder->pool_ = pool_;
placeholder->options_ = &FileOptions::default_instance(); placeholder->options_ = &FileOptions::default_instance();
placeholder->tables_ = &FileDescriptorTables::kEmpty; placeholder->tables_ = &FileDescriptorTables::kEmpty;
...@@ -3485,7 +3483,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, ...@@ -3485,7 +3483,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,
result->default_value_enum_ = NULL; result->default_value_enum_ = NULL;
break; break;
case FieldDescriptor::CPPTYPE_STRING: case FieldDescriptor::CPPTYPE_STRING:
result->default_value_string_ = &kEmptyString; result->default_value_string_ = &::google::protobuf::internal::GetEmptyString();
break; break;
case FieldDescriptor::CPPTYPE_MESSAGE: case FieldDescriptor::CPPTYPE_MESSAGE:
break; break;
......
This diff is collapsed.
This diff is collapsed.
...@@ -68,7 +68,7 @@ bool ParseNamedEnum(const EnumDescriptor* descriptor, ...@@ -68,7 +68,7 @@ bool ParseNamedEnum(const EnumDescriptor* descriptor,
const string& NameOfEnum(const EnumDescriptor* descriptor, int value) { const string& NameOfEnum(const EnumDescriptor* descriptor, int value) {
const EnumValueDescriptor* d = descriptor->FindValueByNumber(value); const EnumValueDescriptor* d = descriptor->FindValueByNumber(value);
return (d == NULL ? kEmptyString : d->name()); return (d == NULL ? GetEmptyString() : d->name());
} }
// =================================================================== // ===================================================================
...@@ -771,7 +771,7 @@ string GeneratedMessageReflection::GetString( ...@@ -771,7 +771,7 @@ string GeneratedMessageReflection::GetString(
} }
GOOGLE_LOG(FATAL) << "Can't get here."; GOOGLE_LOG(FATAL) << "Can't get here.";
return kEmptyString; // Make compiler happy. return GetEmptyString(); // Make compiler happy.
} }
} }
...@@ -790,7 +790,7 @@ const string& GeneratedMessageReflection::GetStringReference( ...@@ -790,7 +790,7 @@ const string& GeneratedMessageReflection::GetStringReference(
} }
GOOGLE_LOG(FATAL) << "Can't get here."; GOOGLE_LOG(FATAL) << "Can't get here.";
return kEmptyString; // Make compiler happy. return GetEmptyString(); // Make compiler happy.
} }
} }
...@@ -832,7 +832,7 @@ string GeneratedMessageReflection::GetRepeatedString( ...@@ -832,7 +832,7 @@ string GeneratedMessageReflection::GetRepeatedString(
} }
GOOGLE_LOG(FATAL) << "Can't get here."; GOOGLE_LOG(FATAL) << "Can't get here.";
return kEmptyString; // Make compiler happy. return GetEmptyString(); // Make compiler happy.
} }
} }
...@@ -850,7 +850,7 @@ const string& GeneratedMessageReflection::GetRepeatedStringReference( ...@@ -850,7 +850,7 @@ const string& GeneratedMessageReflection::GetRepeatedStringReference(
} }
GOOGLE_LOG(FATAL) << "Can't get here."; GOOGLE_LOG(FATAL) << "Can't get here.";
return kEmptyString; // Make compiler happy. return GetEmptyString(); // Make compiler happy.
} }
} }
......
...@@ -47,7 +47,12 @@ double NaN() { ...@@ -47,7 +47,12 @@ double NaN() {
return std::numeric_limits<double>::quiet_NaN(); return std::numeric_limits<double>::quiet_NaN();
} }
const ::std::string kEmptyString; const ::std::string* empty_string_;
ProtobufOnceType empty_string_once_init_;
void InitEmptyString() {
empty_string_ = new std::string;
}
} // namespace internal } // namespace internal
} // namespace protobuf } // namespace protobuf
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include <string> #include <string>
#include <google/protobuf/stubs/common.h> #include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/once.h>
namespace google { namespace google {
namespace protobuf { namespace protobuf {
namespace internal { namespace internal {
...@@ -59,8 +60,17 @@ namespace internal { ...@@ -59,8 +60,17 @@ namespace internal {
LIBPROTOBUF_EXPORT double Infinity(); LIBPROTOBUF_EXPORT double Infinity();
LIBPROTOBUF_EXPORT double NaN(); LIBPROTOBUF_EXPORT double NaN();
// Constant used for empty default strings. // Default empty string object. Don't use the pointer directly. Instead, call
LIBPROTOBUF_EXPORT extern const ::std::string kEmptyString; // GetEmptyString() to get the reference.
LIBPROTOBUF_EXPORT extern const ::std::string* empty_string_;
LIBPROTOBUF_EXPORT extern ProtobufOnceType empty_string_once_init_;
LIBPROTOBUF_EXPORT void InitEmptyString();
LIBPROTOBUF_EXPORT inline const ::std::string& GetEmptyString() {
GoogleOnceInit(&empty_string_once_init_, &InitEmptyString);
return *empty_string_;
}
// Defined in generated_message_reflection.cc -- not actually part of the lite // Defined in generated_message_reflection.cc -- not actually part of the lite
// library. // library.
......
...@@ -383,7 +383,7 @@ class LIBPROTOBUF_EXPORT StringTypeHandlerBase { ...@@ -383,7 +383,7 @@ class LIBPROTOBUF_EXPORT StringTypeHandlerBase {
static void Clear(string* value) { value->clear(); } static void Clear(string* value) { value->clear(); }
static void Merge(const string& from, string* to) { *to = from; } static void Merge(const string& from, string* to) { *to = from; }
static const Type& default_instance() { static const Type& default_instance() {
return ::google::protobuf::internal::kEmptyString; return ::google::protobuf::internal::GetEmptyString();
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment