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,
(*variables)["default_length"] =
SimpleItoa(descriptor->default_value_string().length());
(*variables)["default_variable"] = descriptor->default_value_string().empty()
? "&::google::protobuf::internal::kEmptyString"
? "&::google::protobuf::internal::GetEmptyString()"
: "_default_" + FieldName(descriptor) + "_";
(*variables)["pointer_type"] =
descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char";
......
......@@ -184,7 +184,7 @@ CodeGeneratorRequest::CodeGeneratorRequest(const CodeGeneratorRequest& from)
void CodeGeneratorRequest::SharedCtor() {
_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_));
}
......@@ -193,7 +193,7 @@ CodeGeneratorRequest::~CodeGeneratorRequest() {
}
void CodeGeneratorRequest::SharedDtor() {
if (parameter_ != &::google::protobuf::internal::kEmptyString) {
if (parameter_ != &::google::protobuf::internal::GetEmptyString()) {
delete parameter_;
}
if (this != default_instance_) {
......@@ -224,7 +224,7 @@ CodeGeneratorRequest* CodeGeneratorRequest::New() const {
void CodeGeneratorRequest::Clear() {
if (_has_bits_[1 / 32] & (0xffu << (1 % 32))) {
if (has_parameter()) {
if (parameter_ != &::google::protobuf::internal::kEmptyString) {
if (parameter_ != &::google::protobuf::internal::GetEmptyString()) {
parameter_->clear();
}
}
......@@ -501,9 +501,9 @@ CodeGeneratorResponse_File::CodeGeneratorResponse_File(const CodeGeneratorRespon
void CodeGeneratorResponse_File::SharedCtor() {
_cached_size_ = 0;
name_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString);
insertion_point_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString);
content_ = 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::GetEmptyString());
content_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyString());
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
......@@ -512,13 +512,13 @@ CodeGeneratorResponse_File::~CodeGeneratorResponse_File() {
}
void CodeGeneratorResponse_File::SharedDtor() {
if (name_ != &::google::protobuf::internal::kEmptyString) {
if (name_ != &::google::protobuf::internal::GetEmptyString()) {
delete name_;
}
if (insertion_point_ != &::google::protobuf::internal::kEmptyString) {
if (insertion_point_ != &::google::protobuf::internal::GetEmptyString()) {
delete insertion_point_;
}
if (content_ != &::google::protobuf::internal::kEmptyString) {
if (content_ != &::google::protobuf::internal::GetEmptyString()) {
delete content_;
}
if (this != default_instance_) {
......@@ -549,17 +549,17 @@ CodeGeneratorResponse_File* CodeGeneratorResponse_File::New() const {
void CodeGeneratorResponse_File::Clear() {
if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (has_name()) {
if (name_ != &::google::protobuf::internal::kEmptyString) {
if (name_ != &::google::protobuf::internal::GetEmptyString()) {
name_->clear();
}
}
if (has_insertion_point()) {
if (insertion_point_ != &::google::protobuf::internal::kEmptyString) {
if (insertion_point_ != &::google::protobuf::internal::GetEmptyString()) {
insertion_point_->clear();
}
}
if (has_content()) {
if (content_ != &::google::protobuf::internal::kEmptyString) {
if (content_ != &::google::protobuf::internal::GetEmptyString()) {
content_->clear();
}
}
......@@ -839,7 +839,7 @@ CodeGeneratorResponse::CodeGeneratorResponse(const CodeGeneratorResponse& from)
void CodeGeneratorResponse::SharedCtor() {
_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_));
}
......@@ -848,7 +848,7 @@ CodeGeneratorResponse::~CodeGeneratorResponse() {
}
void CodeGeneratorResponse::SharedDtor() {
if (error_ != &::google::protobuf::internal::kEmptyString) {
if (error_ != &::google::protobuf::internal::GetEmptyString()) {
delete error_;
}
if (this != default_instance_) {
......@@ -879,7 +879,7 @@ CodeGeneratorResponse* CodeGeneratorResponse::New() const {
void CodeGeneratorResponse::Clear() {
if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (has_error()) {
if (error_ != &::google::protobuf::internal::kEmptyString) {
if (error_ != &::google::protobuf::internal::GetEmptyString()) {
error_->clear();
}
}
......
This diff is collapsed.
......@@ -139,8 +139,6 @@ const int FieldDescriptor::kLastReservedNumber;
namespace {
const string kEmptyString;
string ToCamelCase(const string& input) {
bool capitalize_next = false;
string result;
......@@ -2757,7 +2755,7 @@ Symbol DescriptorBuilder::NewPlaceholder(const string& name,
placeholder_name = tables_->AllocateString(
placeholder_full_name->substr(dotpos + 1));
} else {
placeholder_package = &kEmptyString;
placeholder_package = &::google::protobuf::internal::GetEmptyString();
placeholder_name = placeholder_full_name;
}
......@@ -2843,7 +2841,7 @@ const FileDescriptor* DescriptorBuilder::NewPlaceholderFile(
memset(placeholder, 0, sizeof(*placeholder));
placeholder->name_ = tables_->AllocateString(name);
placeholder->package_ = &kEmptyString;
placeholder->package_ = &::google::protobuf::internal::GetEmptyString();
placeholder->pool_ = pool_;
placeholder->options_ = &FileOptions::default_instance();
placeholder->tables_ = &FileDescriptorTables::kEmpty;
......@@ -3485,7 +3483,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,
result->default_value_enum_ = NULL;
break;
case FieldDescriptor::CPPTYPE_STRING:
result->default_value_string_ = &kEmptyString;
result->default_value_string_ = &::google::protobuf::internal::GetEmptyString();
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
break;
......
This diff is collapsed.
This diff is collapsed.
......@@ -68,7 +68,7 @@ bool ParseNamedEnum(const EnumDescriptor* descriptor,
const string& NameOfEnum(const EnumDescriptor* descriptor, int 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(
}
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(
}
GOOGLE_LOG(FATAL) << "Can't get here.";
return kEmptyString; // Make compiler happy.
return GetEmptyString(); // Make compiler happy.
}
}
......@@ -832,7 +832,7 @@ string GeneratedMessageReflection::GetRepeatedString(
}
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(
}
GOOGLE_LOG(FATAL) << "Can't get here.";
return kEmptyString; // Make compiler happy.
return GetEmptyString(); // Make compiler happy.
}
}
......
......@@ -47,7 +47,12 @@ double 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 protobuf
......
......@@ -41,6 +41,7 @@
#include <string>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/once.h>
namespace google {
namespace protobuf {
namespace internal {
......@@ -59,8 +60,17 @@ namespace internal {
LIBPROTOBUF_EXPORT double Infinity();
LIBPROTOBUF_EXPORT double NaN();
// Constant used for empty default strings.
LIBPROTOBUF_EXPORT extern const ::std::string kEmptyString;
// Default empty string object. Don't use the pointer directly. Instead, call
// 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
// library.
......
......@@ -383,7 +383,7 @@ class LIBPROTOBUF_EXPORT StringTypeHandlerBase {
static void Clear(string* value) { value->clear(); }
static void Merge(const string& from, string* to) { *to = from; }
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