diff options
author | Jon Skeet <skeet@pobox.com> | 2008-08-21 21:57:58 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2008-08-21 21:57:58 +0100 |
commit | 0bf2ad145daf4ed7eb0f72c111b496d55233e0b9 (patch) | |
tree | 6c611fe5fb2b26aed50bcc2c4197d7ecc378a6b7 /src | |
parent | ad6903fe33ca0a79e7618a25e83de769438d18ce (diff) | |
download | protobuf-0bf2ad145daf4ed7eb0f72c111b496d55233e0b9.tar.gz protobuf-0bf2ad145daf4ed7eb0f72c111b496d55233e0b9.tar.bz2 protobuf-0bf2ad145daf4ed7eb0f72c111b496d55233e0b9.zip |
Implemented popsicle immutability for lists. Modified MessageStreamIterator to be singly generic.
Diffstat (limited to 'src')
3 files changed, 16 insertions, 42 deletions
diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index a4703be5..94ca2c4e 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc @@ -212,7 +212,8 @@ void MessageGenerator::Generate(io::Printer* printer) { } printer->Indent(); printer->Print( - "private static readonly $classname$ defaultInstance = new $classname$();\r\n" + // Must call Build() to make sure all lists are made read-only + "private static readonly $classname$ defaultInstance = new Builder().BuildPartial();\r\n" "public static $classname$ DefaultInstance {\r\n" " get { return defaultInstance; }\r\n" "}\r\n" diff --git a/src/google/protobuf/compiler/csharp/csharp_message_field.cc b/src/google/protobuf/compiler/csharp/csharp_message_field.cc index 9c3ce498..3981becb 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message_field.cc @@ -175,9 +175,9 @@ RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {} void RepeatedMessageFieldGenerator:: GenerateMembers(io::Printer* printer) const { printer->Print(variables_, - "private scg::IList<$type$> $name$_ = pbc::Lists<$type$>.Empty;\r\n" + "private pbc::PopsicleList<$type$> $name$_ = new pbc::PopsicleList<$type$>();\r\n" "public scg::IList<$type$> $capitalized_name$List {\r\n" - " get { return $name$_; } \r\n" // note: unmodifiable list + " get { return $name$_; } \r\n" // Will be unmodifiable by the time it's exposed "}\r\n" "public int $capitalized_name$Count\r\n" " { get { return $name$_.Count; }\r\n" @@ -190,12 +190,10 @@ GenerateMembers(io::Printer* printer) const { void RepeatedMessageFieldGenerator:: GenerateBuilderMembers(io::Printer* printer) const { printer->Print(variables_, - // Note: We return an unmodifiable list because otherwise the caller - // could hold on to the returned list and modify it after the message - // has been built, thus mutating the message which is supposed to be - // immutable. + // Note: We can return the original list here, because we + // make it read-only when we build. "public scg::IList<$type$> $capitalized_name$List {\r\n" - " get { return pbc::Lists.AsReadOnly(result.$name$_); }\r\n" + " get { return result.$name$_; }\r\n" "}\r\n" "public int $capitalized_name$Count {\r\n" " get { return result.$capitalized_name$Count; }\r\n" @@ -212,28 +210,19 @@ GenerateBuilderMembers(io::Printer* printer) const { " return this;\r\n" "}\r\n" "public Builder Add$capitalized_name$($type$ value) {\r\n" - " if (result.$name$_ == pbc::Lists<$type$>.Empty) {\r\n" - " result.$name$_ = new scg::List<$type$>();\r\n" - " }\r\n" " result.$name$_.Add(value);\r\n" " return this;\r\n" "}\r\n" "public Builder Add$capitalized_name$($type$.Builder builderForValue) {\r\n" - " if (result.$name$_ == pbc::Lists<$type$>.Empty) {\r\n" - " result.$name$_ = new scg::List<$type$>();\r\n" - " }\r\n" " result.$name$_.Add(builderForValue.Build());\r\n" " return this;\r\n" "}\r\n" "public Builder AddRange$capitalized_name$(scg::IEnumerable<$type$> values) {\r\n" - " if (result.$name$_ == pbc::Lists<$type$>.Empty) {\r\n" - " result.$name$_ = new scg::List<$type$>();\r\n" - " }\r\n" " base.AddRange(values, result.$name$_);\r\n" " return this;\r\n" "}\r\n" "public Builder Clear$capitalized_name$() {\r\n" - " result.$name$_ = pbc::Lists<$type$>.Empty;\r\n" + " result.$name$_.Clear();\r\n" " return this;\r\n" "}\r\n"); } @@ -242,9 +231,6 @@ void RepeatedMessageFieldGenerator:: GenerateMergingCode(io::Printer* printer) const { printer->Print(variables_, "if (other.$name$_.Count != 0) {\r\n" - " if (result.$name$_.Count == 0) {\r\n" - " result.$name$_ = new scg::List<$type$>();\r\n" - " }\r\n" " base.AddRange(other.$name$_, result.$name$_);\r\n" "}\r\n"); } @@ -252,9 +238,7 @@ GenerateMergingCode(io::Printer* printer) const { void RepeatedMessageFieldGenerator:: GenerateBuildingCode(io::Printer* printer) const { printer->Print(variables_, - "if (result.$name$_ != pbc::Lists<$type$>.Empty) {\r\n" - " result.$name$_ = pbc::Lists<$type$>.AsReadOnly(result.$name$_);\r\n" - "}\r\n"); + "result.$name$_.MakeReadOnly();\r\n"); } void RepeatedMessageFieldGenerator:: diff --git a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc index 4bd6f499..64b2bb52 100644 --- a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc @@ -239,9 +239,9 @@ RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {} void RepeatedPrimitiveFieldGenerator:: GenerateMembers(io::Printer* printer) const { printer->Print(variables_, - "private scg::IList<$type$> $name$_ = pbc::Lists<$type$>.Empty;\r\n" + "private pbc::PopsicleList<$type$> $name$_ = new pbc::PopsicleList<$type$>();\r\n" "public scg::IList<$type$> $capitalized_name$List {\r\n" - " get { return $name$_; }\r\n" // note: unmodifiable list + " get { return $name$_; } \r\n" // Will be unmodifiable by the time it's exposed "}\r\n" "public int $capitalized_name$Count {\r\n" " get { return $name$_.Count; }\r\n" @@ -254,12 +254,10 @@ GenerateMembers(io::Printer* printer) const { void RepeatedPrimitiveFieldGenerator:: GenerateBuilderMembers(io::Printer* printer) const { printer->Print(variables_, - // Note: We return an unmodifiable list because otherwise the caller - // could hold on to the returned list and modify it after the message - // has been built, thus mutating the message which is supposed to be - // immutable. This unfortunately limits the use for collection initializers... + // Note: We can return the original list here, because we + // make it read-only when we build. "public scg::IList<$type$> $capitalized_name$List {\r\n" - " get { return pbc::Lists<$type$>.AsReadOnly(result.$name$_); }\r\n" + " get { return result.$name$_; }\r\n" "}\r\n" "public int $capitalized_name$Count {\r\n" " get { return result.$capitalized_name$Count; }\r\n" @@ -272,21 +270,15 @@ GenerateBuilderMembers(io::Printer* printer) const { " return this;\r\n" "}\r\n" "public Builder Add$capitalized_name$($type$ value) {\r\n" - " if (result.$name$_.Count == 0) {\r\n" - " result.$name$_ = new scg::List<$type$>();\r\n" - " }\r\n" " result.$name$_.Add(value);\r\n" " return this;\r\n" "}\r\n" "public Builder AddRange$capitalized_name$(scg::IEnumerable<$type$> values) {\r\n" - " if (result.$name$_.Count == 0) {\r\n" - " result.$name$_ = new scg::List<$type$>();\r\n" - " }\r\n" " base.AddRange(values, result.$name$_);\r\n" " return this;\r\n" "}\r\n" "public Builder Clear$capitalized_name$() {\r\n" - " result.$name$_ = pbc::Lists<$type$>.Empty;\r\n" + " result.$name$_.Clear();\r\n" " return this;\r\n" "}\r\n"); } @@ -295,9 +287,6 @@ void RepeatedPrimitiveFieldGenerator:: GenerateMergingCode(io::Printer* printer) const { printer->Print(variables_, "if (other.$name$_.Count != 0) {\r\n" - " if (result.$name$_.Count == 0) {\r\n" - " result.$name$_ = new scg::List<$type$>();\r\n" - " }\r\n" " base.AddRange(other.$name$_, result.$name$_);\r\n" "}\r\n"); } @@ -305,7 +294,7 @@ GenerateMergingCode(io::Printer* printer) const { void RepeatedPrimitiveFieldGenerator:: GenerateBuildingCode(io::Printer* printer) const { printer->Print(variables_, - "result.$name$_ = pbc::Lists<$type$>.AsReadOnly(result.$name$_);\r\n"); + "result.$name$_.MakeReadOnly();\r\n"); } void RepeatedPrimitiveFieldGenerator:: |