Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
804b6d84
Commit
804b6d84
authored
Nov 07, 2010
by
csharptest
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implementation work for Lite runtime and generator
parent
64bfac28
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
284 additions
and
74 deletions
+284
-74
Helpers.cs
src/ProtoGen/Helpers.cs
+0
-6
MessageGenerator.cs
src/ProtoGen/MessageGenerator.cs
+38
-31
SourceGeneratorBase.cs
src/ProtoGen/SourceGeneratorBase.cs
+8
-0
UmbrellaClassGenerator.cs
src/ProtoGen/UmbrellaClassGenerator.cs
+29
-2
CodedInputStream.cs
src/ProtocolBuffers/CodedInputStream.cs
+1
-4
CodedOutputStream.cs
src/ProtocolBuffers/CodedOutputStream.cs
+13
-18
EnumDescriptor.cs
src/ProtocolBuffers/Descriptors/EnumDescriptor.cs
+9
-2
EnumValueDescriptor.cs
src/ProtocolBuffers/Descriptors/EnumValueDescriptor.cs
+1
-1
FieldDescriptor.cs
src/ProtocolBuffers/Descriptors/FieldDescriptor.cs
+19
-2
DynamicMessage.cs
src/ProtocolBuffers/DynamicMessage.cs
+4
-4
EnumLite.cs
src/ProtocolBuffers/EnumLite.cs
+31
-0
ExtendableMessage.cs
src/ProtocolBuffers/ExtendableMessage.cs
+4
-4
FieldSet.cs
src/ProtocolBuffers/FieldSet.cs
+0
-0
GeneratedExtensionLite.cs
src/ProtocolBuffers/GeneratedExtensionLite.cs
+109
-0
ProtocolBuffers.csproj
src/ProtocolBuffers/ProtocolBuffers.csproj
+1
-0
ProtocolBuffersLite.csproj
src/ProtocolBuffers/ProtocolBuffersLite.csproj
+13
-0
ProtocolBuffersLite.Test.csproj
src/ProtocolBuffersLite.Test/ProtocolBuffersLite.Test.csproj
+4
-0
No files found.
src/ProtoGen/Helpers.cs
View file @
804b6d84
...
...
@@ -38,11 +38,5 @@ namespace Google.ProtocolBuffers.ProtoGen {
/// Helpers to resolve class names etc.
/// </summary>
internal
static
class
Helpers
{
internal
static
void
WriteNamespaces
(
TextGenerator
writer
)
{
writer
.
WriteLine
(
"using pb = global::Google.ProtocolBuffers;"
);
writer
.
WriteLine
(
"using pbc = global::Google.ProtocolBuffers.Collections;"
);
writer
.
WriteLine
(
"using pbd = global::Google.ProtocolBuffers.Descriptors;"
);
writer
.
WriteLine
(
"using scg = global::System.Collections.Generic;"
);
}
}
}
src/ProtoGen/MessageGenerator.cs
View file @
804b6d84
...
...
@@ -69,12 +69,13 @@ namespace Google.ProtocolBuffers.ProtoGen {
string
identifier
=
GetUniqueFileScopeIdentifier
(
Descriptor
);
// The descriptor for this type.
string
access
=
Descriptor
.
File
.
CSharpOptions
.
NestClasses
?
"private"
:
"internal"
;
writer
.
WriteLine
(
"{0} static pbd::MessageDescriptor internal__{1}__Descriptor;"
,
access
,
identifier
);
writer
.
WriteLine
(
"{0} static pb::FieldAccess.FieldAccessorTable<{1}, {1}.Builder> internal__{2}__FieldAccessorTable;"
,
access
,
FullClassName
,
identifier
);
if
(!
UseLiteRuntime
)
{
// The descriptor for this type.
string
access
=
Descriptor
.
File
.
CSharpOptions
.
NestClasses
?
"private"
:
"internal"
;
writer
.
WriteLine
(
"{0} static pbd::MessageDescriptor internal__{1}__Descriptor;"
,
access
,
identifier
);
writer
.
WriteLine
(
"{0} static pb::FieldAccess.FieldAccessorTable<{1}, {1}.Builder> internal__{2}__FieldAccessorTable;"
,
access
,
FullClassName
,
identifier
);
}
// Generate static members for all nested types.
foreach
(
MessageDescriptor
nestedMessage
in
Descriptor
.
NestedTypes
)
{
new
MessageGenerator
(
nestedMessage
).
GenerateStaticVariables
(
writer
);
...
...
@@ -84,21 +85,23 @@ namespace Google.ProtocolBuffers.ProtoGen {
internal
void
GenerateStaticVariableInitializers
(
TextGenerator
writer
)
{
string
identifier
=
GetUniqueFileScopeIdentifier
(
Descriptor
);
writer
.
Write
(
"internal__{0}__Descriptor = "
,
identifier
);
if
(
Descriptor
.
ContainingType
==
null
)
{
writer
.
WriteLine
(
"Descriptor.MessageTypes[{0}];"
,
Descriptor
.
Index
);
}
else
{
writer
.
WriteLine
(
"internal__{0}__Descriptor.NestedTypes[{1}];"
,
GetUniqueFileScopeIdentifier
(
Descriptor
.
ContainingType
),
Descriptor
.
Index
);
}
if
(!
UseLiteRuntime
)
{
writer
.
Write
(
"internal__{0}__Descriptor = "
,
identifier
);
if
(
Descriptor
.
ContainingType
==
null
)
{
writer
.
WriteLine
(
"Descriptor.MessageTypes[{0}];"
,
Descriptor
.
Index
);
}
else
{
writer
.
WriteLine
(
"internal__{0}__Descriptor.NestedTypes[{1}];"
,
GetUniqueFileScopeIdentifier
(
Descriptor
.
ContainingType
),
Descriptor
.
Index
);
}
writer
.
WriteLine
(
"internal__{0}__FieldAccessorTable = "
,
identifier
);
writer
.
WriteLine
(
" new pb::FieldAccess.FieldAccessorTable<{1}, {1}.Builder>(internal__{0}__Descriptor,"
,
identifier
,
FullClassName
);
writer
.
Print
(
" new string[] { "
);
foreach
(
FieldDescriptor
field
in
Descriptor
.
Fields
)
{
writer
.
Write
(
"\"{0}\", "
,
field
.
CSharpOptions
.
PropertyName
);
writer
.
WriteLine
(
"internal__{0}__FieldAccessorTable = "
,
identifier
);
writer
.
WriteLine
(
" new pb::FieldAccess.FieldAccessorTable<{1}, {1}.Builder>(internal__{0}__Descriptor,"
,
identifier
,
FullClassName
);
writer
.
Print
(
" new string[] { "
);
foreach
(
FieldDescriptor
field
in
Descriptor
.
Fields
)
{
writer
.
Write
(
"\"{0}\", "
,
field
.
CSharpOptions
.
PropertyName
);
}
writer
.
WriteLine
(
"});"
);
}
writer
.
WriteLine
(
"});"
);
// Generate static member initializers for all nested types.
foreach
(
MessageDescriptor
nestedMessage
in
Descriptor
.
NestedTypes
)
{
...
...
@@ -111,8 +114,10 @@ namespace Google.ProtocolBuffers.ProtoGen {
}
public
void
Generate
(
TextGenerator
writer
)
{
writer
.
WriteLine
(
"{0} sealed partial class {1} : pb::{2}Message<{1}, {1}.Builder> {{"
,
ClassAccessLevel
,
ClassName
,
Descriptor
.
Proto
.
ExtensionRangeCount
>
0
?
"Extendable"
:
"Generated"
);
writer
.
WriteLine
(
"{0} sealed partial class {1} : pb::{2}Message{3}<{1}, {1}.Builder> {{"
,
ClassAccessLevel
,
ClassName
,
Descriptor
.
Proto
.
ExtensionRangeCount
>
0
?
"Extendable"
:
"Generated"
,
UseLiteRuntime
?
"Lite"
:
""
);
writer
.
Indent
();
// Must call BuildPartial() to make sure all lists are made read-only
writer
.
WriteLine
(
"private static readonly {0} defaultInstance = new Builder().BuildPartial();"
,
ClassName
);
...
...
@@ -128,16 +133,18 @@ namespace Google.ProtocolBuffers.ProtoGen {
writer
.
WriteLine
(
" get { return this; }"
);
writer
.
WriteLine
(
"}"
);
writer
.
WriteLine
();
writer
.
WriteLine
(
"public static pbd::MessageDescriptor Descriptor {"
);
writer
.
WriteLine
(
" get {{ return {0}.internal__{1}__Descriptor; }}"
,
DescriptorUtil
.
GetFullUmbrellaClassName
(
Descriptor
),
GetUniqueFileScopeIdentifier
(
Descriptor
));
writer
.
WriteLine
(
"}"
);
writer
.
WriteLine
();
writer
.
WriteLine
(
"protected override pb::FieldAccess.FieldAccessorTable<{0}, {0}.Builder> InternalFieldAccessors {{"
,
ClassName
);
writer
.
WriteLine
(
" get {{ return {0}.internal__{1}__FieldAccessorTable; }}"
,
DescriptorUtil
.
GetFullUmbrellaClassName
(
Descriptor
),
GetUniqueFileScopeIdentifier
(
Descriptor
));
writer
.
WriteLine
(
"}"
);
writer
.
WriteLine
();
if
(!
UseLiteRuntime
)
{
writer
.
WriteLine
(
"public static pbd::MessageDescriptor Descriptor {"
);
writer
.
WriteLine
(
" get {{ return {0}.internal__{1}__Descriptor; }}"
,
DescriptorUtil
.
GetFullUmbrellaClassName
(
Descriptor
),
GetUniqueFileScopeIdentifier
(
Descriptor
));
writer
.
WriteLine
(
"}"
);
writer
.
WriteLine
();
writer
.
WriteLine
(
"protected override pb::FieldAccess.FieldAccessorTable<{0}, {0}.Builder> InternalFieldAccessors {{"
,
ClassName
);
writer
.
WriteLine
(
" get {{ return {0}.internal__{1}__FieldAccessorTable; }}"
,
DescriptorUtil
.
GetFullUmbrellaClassName
(
Descriptor
),
GetUniqueFileScopeIdentifier
(
Descriptor
));
writer
.
WriteLine
(
"}"
);
writer
.
WriteLine
();
}
// Extensions don't need to go in an extra nested type
WriteChildren
(
writer
,
null
,
Descriptor
.
Extensions
);
...
...
src/ProtoGen/SourceGeneratorBase.cs
View file @
804b6d84
...
...
@@ -40,8 +40,16 @@ namespace Google.ProtocolBuffers.ProtoGen {
private
readonly
T
descriptor
;
protected
readonly
bool
OptimizeSpeed
;
protected
readonly
bool
OptimizeSize
;
protected
readonly
bool
UseLiteRuntime
;
protected
SourceGeneratorBase
(
T
descriptor
)
{
this
.
descriptor
=
descriptor
;
OptimizeSize
=
descriptor
.
File
.
Options
.
OptimizeFor
==
Google
.
ProtocolBuffers
.
DescriptorProtos
.
FileOptions
.
Types
.
OptimizeMode
.
CODE_SIZE
;
OptimizeSpeed
=
descriptor
.
File
.
Options
.
OptimizeFor
==
Google
.
ProtocolBuffers
.
DescriptorProtos
.
FileOptions
.
Types
.
OptimizeMode
.
SPEED
;
UseLiteRuntime
=
descriptor
.
File
.
Options
.
OptimizeFor
==
Google
.
ProtocolBuffers
.
DescriptorProtos
.
FileOptions
.
Types
.
OptimizeMode
.
LITE_RUNTIME
;
}
protected
T
Descriptor
{
...
...
src/ProtoGen/UmbrellaClassGenerator.cs
View file @
804b6d84
...
...
@@ -83,7 +83,11 @@ namespace Google.ProtocolBuffers.ProtoGen {
new
MessageGenerator
(
message
).
GenerateStaticVariables
(
writer
);
}
writer
.
WriteLine
(
"#endregion"
);
WriteDescriptor
(
writer
);
if
(!
UseLiteRuntime
)
{
WriteDescriptor
(
writer
);
}
else
{
WriteLiteExtensions
(
writer
);
}
// The class declaration either gets closed before or after the children are written.
if
(!
Descriptor
.
CSharpOptions
.
NestClasses
)
{
writer
.
Outdent
();
...
...
@@ -111,7 +115,12 @@ namespace Google.ProtocolBuffers.ProtoGen {
private
void
WriteIntroduction
(
TextGenerator
writer
)
{
writer
.
WriteLine
(
"// Generated by the protocol buffer compiler. DO NOT EDIT!"
);
writer
.
WriteLine
();
Helpers
.
WriteNamespaces
(
writer
);
writer
.
WriteLine
(
"using pb = global::Google.ProtocolBuffers;"
);
if
(!
UseLiteRuntime
)
{
writer
.
WriteLine
(
"using pbc = global::Google.ProtocolBuffers.Collections;"
);
writer
.
WriteLine
(
"using pbd = global::Google.ProtocolBuffers.Descriptors;"
);
}
writer
.
WriteLine
(
"using scg = global::System.Collections.Generic;"
);
if
(
Descriptor
.
CSharpOptions
.
Namespace
!=
""
)
{
writer
.
WriteLine
(
"namespace {0} {{"
,
Descriptor
.
CSharpOptions
.
Namespace
);
...
...
@@ -211,5 +220,23 @@ namespace Google.ProtocolBuffers.ProtoGen {
writer
.
WriteLine
(
"#endregion"
);
writer
.
WriteLine
();
}
private
void
WriteLiteExtensions
(
TextGenerator
writer
)
{
writer
.
WriteLine
(
"#region Extensions"
);
writer
.
WriteLine
(
"static {0}() {{"
,
Descriptor
.
CSharpOptions
.
UmbrellaClassname
);
writer
.
Indent
();
foreach
(
MessageDescriptor
message
in
Descriptor
.
MessageTypes
)
{
new
MessageGenerator
(
message
).
GenerateStaticVariableInitializers
(
writer
);
}
foreach
(
FieldDescriptor
extension
in
Descriptor
.
Extensions
)
{
new
ExtensionGenerator
(
extension
).
GenerateStaticVariableInitializers
(
writer
);
}
writer
.
Outdent
();
writer
.
WriteLine
(
"}"
);
writer
.
WriteLine
(
"#endregion"
);
writer
.
WriteLine
();
}
}
}
src/ProtocolBuffers/CodedInputStream.cs
View file @
804b6d84
...
...
@@ -36,9 +36,7 @@ using System;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Text
;
#if !LITE
using
Google.ProtocolBuffers.Descriptors
;
#endif
namespace
Google.ProtocolBuffers
{
...
...
@@ -363,7 +361,7 @@ namespace Google.ProtocolBuffers {
public
long
ReadSInt64
()
{
return
DecodeZigZag64
(
ReadRawVarint64
());
}
#if !LITE
/// <summary>
/// Reads a field of any primitive type. Enums, groups and embedded
/// messages are not handled by this method.
...
...
@@ -397,7 +395,6 @@ namespace Google.ProtocolBuffers {
throw
new
ArgumentOutOfRangeException
(
"Invalid field type "
+
fieldType
);
}
}
#endif
#
endregion
#
region
Underlying
reading
primitives
...
...
src/ProtocolBuffers/CodedOutputStream.cs
View file @
804b6d84
...
...
@@ -35,9 +35,8 @@
using
System
;
using
System.IO
;
using
System.Text
;
#if !LITE
using
Google.ProtocolBuffers.Descriptors
;
#endif
namespace
Google.ProtocolBuffers
{
/// <summary>
...
...
@@ -279,7 +278,6 @@ namespace Google.ProtocolBuffers {
WriteTag
(
WireFormat
.
MessageSetField
.
Item
,
WireFormat
.
WireType
.
EndGroup
);
}
#if !LITE
public
void
WriteField
(
FieldType
fieldType
,
int
fieldNumber
,
object
value
)
{
switch
(
fieldType
)
{
case
FieldType
.
Double
:
WriteDouble
(
fieldNumber
,
(
double
)
value
);
break
;
...
...
@@ -291,15 +289,15 @@ namespace Google.ProtocolBuffers {
case
FieldType
.
Fixed32
:
WriteFixed32
(
fieldNumber
,
(
uint
)
value
);
break
;
case
FieldType
.
Bool
:
WriteBool
(
fieldNumber
,
(
bool
)
value
);
break
;
case
FieldType
.
String
:
WriteString
(
fieldNumber
,
(
string
)
value
);
break
;
case
FieldType
.
Group
:
WriteGroup
(
fieldNumber
,
(
IMessage
)
value
);
break
;
case
FieldType
.
Message
:
WriteMessage
(
fieldNumber
,
(
IMessage
)
value
);
break
;
case
FieldType
.
Group
:
WriteGroup
(
fieldNumber
,
(
IMessage
Lite
)
value
);
break
;
case
FieldType
.
Message
:
WriteMessage
(
fieldNumber
,
(
IMessage
Lite
)
value
);
break
;
case
FieldType
.
Bytes
:
WriteBytes
(
fieldNumber
,
(
ByteString
)
value
);
break
;
case
FieldType
.
UInt32
:
WriteUInt32
(
fieldNumber
,
(
uint
)
value
);
break
;
case
FieldType
.
SFixed32
:
WriteSFixed32
(
fieldNumber
,
(
int
)
value
);
break
;
case
FieldType
.
SFixed64
:
WriteSFixed64
(
fieldNumber
,
(
long
)
value
);
break
;
case
FieldType
.
SInt32
:
WriteSInt32
(
fieldNumber
,
(
int
)
value
);
break
;
case
FieldType
.
SInt64
:
WriteSInt64
(
fieldNumber
,
(
long
)
value
);
break
;
case
FieldType
.
Enum
:
WriteEnum
(
fieldNumber
,
((
EnumValueDescriptor
)
value
).
Number
);
case
FieldType
.
Enum
:
WriteEnum
(
fieldNumber
,
((
IEnumLite
)
value
).
Number
);
break
;
}
}
...
...
@@ -315,19 +313,18 @@ namespace Google.ProtocolBuffers {
case
FieldType
.
Fixed32
:
WriteFixed32NoTag
((
uint
)
value
);
break
;
case
FieldType
.
Bool
:
WriteBoolNoTag
((
bool
)
value
);
break
;
case
FieldType
.
String
:
WriteStringNoTag
((
string
)
value
);
break
;
case
FieldType
.
Group
:
WriteGroupNoTag
((
IMessage
)
value
);
break
;
case
FieldType
.
Message
:
WriteMessageNoTag
((
IMessage
)
value
);
break
;
case
FieldType
.
Group
:
WriteGroupNoTag
((
IMessage
Lite
)
value
);
break
;
case
FieldType
.
Message
:
WriteMessageNoTag
((
IMessage
Lite
)
value
);
break
;
case
FieldType
.
Bytes
:
WriteBytesNoTag
((
ByteString
)
value
);
break
;
case
FieldType
.
UInt32
:
WriteUInt32NoTag
((
uint
)
value
);
break
;
case
FieldType
.
SFixed32
:
WriteSFixed32NoTag
((
int
)
value
);
break
;
case
FieldType
.
SFixed64
:
WriteSFixed64NoTag
((
long
)
value
);
break
;
case
FieldType
.
SInt32
:
WriteSInt32NoTag
((
int
)
value
);
break
;
case
FieldType
.
SInt64
:
WriteSInt64NoTag
((
long
)
value
);
break
;
case
FieldType
.
Enum
:
WriteEnumNoTag
(((
EnumValueDescriptor
)
value
).
Number
);
case
FieldType
.
Enum
:
WriteEnumNoTag
(((
IEnumLite
)
value
).
Number
);
break
;
}
}
#endif
#
endregion
#
region
Writing
of
values
without
tags
...
...
@@ -995,7 +992,6 @@ namespace Google.ProtocolBuffers {
return
10
;
}
#if !LITE
/// <summary>
/// Compute the number of bytes that would be needed to encode a
/// field of arbitrary type, including the tag, to the stream.
...
...
@@ -1011,15 +1007,15 @@ namespace Google.ProtocolBuffers {
case
FieldType
.
Fixed32
:
return
ComputeFixed32Size
(
fieldNumber
,
(
uint
)
value
);
case
FieldType
.
Bool
:
return
ComputeBoolSize
(
fieldNumber
,
(
bool
)
value
);
case
FieldType
.
String
:
return
ComputeStringSize
(
fieldNumber
,
(
string
)
value
);
case
FieldType
.
Group
:
return
ComputeGroupSize
(
fieldNumber
,
(
IMessage
)
value
);
case
FieldType
.
Message
:
return
ComputeMessageSize
(
fieldNumber
,
(
IMessage
)
value
);
case
FieldType
.
Group
:
return
ComputeGroupSize
(
fieldNumber
,
(
IMessage
Lite
)
value
);
case
FieldType
.
Message
:
return
ComputeMessageSize
(
fieldNumber
,
(
IMessage
Lite
)
value
);
case
FieldType
.
Bytes
:
return
ComputeBytesSize
(
fieldNumber
,
(
ByteString
)
value
);
case
FieldType
.
UInt32
:
return
ComputeUInt32Size
(
fieldNumber
,
(
uint
)
value
);
case
FieldType
.
SFixed32
:
return
ComputeSFixed32Size
(
fieldNumber
,
(
int
)
value
);
case
FieldType
.
SFixed64
:
return
ComputeSFixed64Size
(
fieldNumber
,
(
long
)
value
);
case
FieldType
.
SInt32
:
return
ComputeSInt32Size
(
fieldNumber
,
(
int
)
value
);
case
FieldType
.
SInt64
:
return
ComputeSInt64Size
(
fieldNumber
,
(
long
)
value
);
case
FieldType
.
Enum
:
return
ComputeEnumSize
(
fieldNumber
,
((
EnumValueDescriptor
)
value
).
Number
);
case
FieldType
.
Enum
:
return
ComputeEnumSize
(
fieldNumber
,
((
IEnumLite
)
value
).
Number
);
default
:
throw
new
ArgumentOutOfRangeException
(
"Invalid field type "
+
fieldType
);
}
...
...
@@ -1040,20 +1036,19 @@ namespace Google.ProtocolBuffers {
case
FieldType
.
Fixed32
:
return
ComputeFixed32SizeNoTag
((
uint
)
value
);
case
FieldType
.
Bool
:
return
ComputeBoolSizeNoTag
((
bool
)
value
);
case
FieldType
.
String
:
return
ComputeStringSizeNoTag
((
string
)
value
);
case
FieldType
.
Group
:
return
ComputeGroupSizeNoTag
((
IMessage
)
value
);
case
FieldType
.
Message
:
return
ComputeMessageSizeNoTag
((
IMessage
)
value
);
case
FieldType
.
Group
:
return
ComputeGroupSizeNoTag
((
IMessage
Lite
)
value
);
case
FieldType
.
Message
:
return
ComputeMessageSizeNoTag
((
IMessage
Lite
)
value
);
case
FieldType
.
Bytes
:
return
ComputeBytesSizeNoTag
((
ByteString
)
value
);
case
FieldType
.
UInt32
:
return
ComputeUInt32SizeNoTag
((
uint
)
value
);
case
FieldType
.
SFixed32
:
return
ComputeSFixed32SizeNoTag
((
int
)
value
);
case
FieldType
.
SFixed64
:
return
ComputeSFixed64SizeNoTag
((
long
)
value
);
case
FieldType
.
SInt32
:
return
ComputeSInt32SizeNoTag
((
int
)
value
);
case
FieldType
.
SInt64
:
return
ComputeSInt64SizeNoTag
((
long
)
value
);
case
FieldType
.
Enum
:
return
ComputeEnumSizeNoTag
(((
EnumValueDescriptor
)
value
).
Number
);
case
FieldType
.
Enum
:
return
ComputeEnumSizeNoTag
(((
IEnumLite
)
value
).
Number
);
default
:
throw
new
ArgumentOutOfRangeException
(
"Invalid field type "
+
fieldType
);
}
}
#endif
/// <summary>
/// Compute the number of bytes that would be needed to encode a tag.
...
...
src/ProtocolBuffers/Descriptors/EnumDescriptor.cs
View file @
804b6d84
...
...
@@ -37,7 +37,7 @@ namespace Google.ProtocolBuffers.Descriptors {
/// <summary>
/// Descriptor for an enum type in a .proto file.
/// </summary>
public
sealed
class
EnumDescriptor
:
IndexedDescriptorBase
<
EnumDescriptorProto
,
EnumOptions
>
{
public
sealed
class
EnumDescriptor
:
IndexedDescriptorBase
<
EnumDescriptorProto
,
EnumOptions
>
,
IEnumLiteMap
<
EnumValueDescriptor
>
{
private
readonly
MessageDescriptor
containingType
;
private
readonly
IList
<
EnumValueDescriptor
>
values
;
...
...
@@ -72,11 +72,18 @@ namespace Google.ProtocolBuffers.Descriptors {
get
{
return
values
;
}
}
/// <summary>
/// Logic moved from FieldSet to continue current behavior
/// </summary>
public
bool
IsValidValue
(
IEnumLite
value
)
{
return
value
is
EnumValueDescriptor
&&
((
EnumValueDescriptor
)
value
).
EnumDescriptor
==
this
;
}
/// <summary>
/// Finds an enum value by number. If multiple enum values have the
/// same number, this returns the first defined value with that number.
/// </summary>
internal
EnumValueDescriptor
FindValueByNumber
(
int
number
)
{
public
EnumValueDescriptor
FindValueByNumber
(
int
number
)
{
return
File
.
DescriptorPool
.
FindEnumValueByNumber
(
this
,
number
);
}
...
...
src/ProtocolBuffers/Descriptors/EnumValueDescriptor.cs
View file @
804b6d84
...
...
@@ -36,7 +36,7 @@ namespace Google.ProtocolBuffers.Descriptors {
/// <summary>
/// Descriptor for a single enum value within an enum in a .proto file.
/// </summary>
public
sealed
class
EnumValueDescriptor
:
IndexedDescriptorBase
<
EnumValueDescriptorProto
,
EnumValueOptions
>
{
public
sealed
class
EnumValueDescriptor
:
IndexedDescriptorBase
<
EnumValueDescriptorProto
,
EnumValueOptions
>
,
IEnumLite
{
private
readonly
EnumDescriptor
enumDescriptor
;
...
...
src/ProtocolBuffers/Descriptors/FieldDescriptor.cs
View file @
804b6d84
...
...
@@ -40,7 +40,7 @@ namespace Google.ProtocolBuffers.Descriptors {
/// <summary>
/// Descriptor for a field or extension within a message in a .proto file.
/// </summary>
public
sealed
class
FieldDescriptor
:
IndexedDescriptorBase
<
FieldDescriptorProto
,
FieldOptions
>,
IComparable
<
FieldDescriptor
>
{
public
sealed
class
FieldDescriptor
:
IndexedDescriptorBase
<
FieldDescriptorProto
,
FieldOptions
>,
IComparable
<
FieldDescriptor
>
,
IFieldDescriptorLite
{
private
readonly
MessageDescriptor
extensionScope
;
private
EnumDescriptor
enumType
;
...
...
@@ -299,9 +299,26 @@ namespace Google.ProtocolBuffers.Descriptors {
}
return
FieldNumber
-
other
.
FieldNumber
;
}
/// <summary>
/// Compares this descriptor with another one, ordering in "canonical" order
/// which simply means ascending order by field number. <paramref name="other"/>
/// must be a field of the same type, i.e. the <see cref="ContainingType"/> of
/// both fields must be the same.
/// </summary>
public
int
CompareTo
(
IFieldDescriptorLite
other
)
{
return
FieldNumber
-
other
.
FieldNumber
;
}
IEnumLiteMap
IFieldDescriptorLite
.
EnumType
{
get
{
return
EnumType
;
}
}
bool
IFieldDescriptorLite
.
MessageSetWireFormat
{
get
{
return
ContainingType
.
Options
.
MessageSetWireFormat
;
}
}
/// <summary>
/// For enum fields, returns the field's type.
/// </summary>
public
EnumDescriptor
EnumType
{
...
...
src/ProtocolBuffers/DynamicMessage.cs
View file @
804b6d84
...
...
@@ -180,7 +180,7 @@ namespace Google.ProtocolBuffers {
}
public
override
IDictionary
<
FieldDescriptor
,
object
>
AllFields
{
get
{
return
fields
.
AllFields
;
}
get
{
return
fields
.
AllField
Descriptor
s
;
}
}
public
override
bool
HasField
(
FieldDescriptor
field
)
{
...
...
@@ -216,7 +216,7 @@ namespace Google.ProtocolBuffers {
}
public
bool
Initialized
{
get
{
return
fields
.
IsInitializedWithRespectTo
(
type
);
}
get
{
return
fields
.
IsInitializedWithRespectTo
(
type
.
Fields
);
}
}
public
override
void
WriteTo
(
CodedOutputStream
output
)
{
...
...
@@ -335,7 +335,7 @@ namespace Google.ProtocolBuffers {
}
public
override
bool
IsInitialized
{
get
{
return
fields
.
IsInitializedWithRespectTo
(
type
);
}
get
{
return
fields
.
IsInitializedWithRespectTo
(
type
.
Fields
);
}
}
public
override
Builder
MergeFrom
(
CodedInputStream
input
,
ExtensionRegistry
extensionRegistry
)
{
...
...
@@ -354,7 +354,7 @@ namespace Google.ProtocolBuffers {
}
public
override
IDictionary
<
FieldDescriptor
,
object
>
AllFields
{
get
{
return
fields
.
AllFields
;
}
get
{
return
fields
.
AllField
Descriptor
s
;
}
}
public
override
IBuilder
CreateBuilderForField
(
FieldDescriptor
field
)
{
...
...
src/ProtocolBuffers/EnumLite.cs
0 → 100644
View file @
804b6d84
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
Google.ProtocolBuffers
{
///<summary>
///Interface for an enum value or value descriptor, to be used in FieldSet.
///The lite library stores enum values directly in FieldSets but the full
///library stores EnumValueDescriptors in order to better support reflection.
///</summary>
public
interface
IEnumLite
{
int
Number
{
get
;
}
}
///<summary>
///Interface for an object which maps integers to {@link EnumLite}s.
///{@link Descriptors.EnumDescriptor} implements this interface by mapping
///numbers to {@link Descriptors.EnumValueDescriptor}s. Additionally,
///every generated enum type has a static method internalGetValueMap() which
///returns an implementation of this type that maps numbers to enum values.
///</summary>
public
interface
IEnumLiteMap
<
T
>
:
IEnumLiteMap
where
T
:
IEnumLite
{
T
FindValueByNumber
(
int
number
);
}
public
interface
IEnumLiteMap
{
bool
IsValidValue
(
IEnumLite
value
);
}
}
src/ProtocolBuffers/ExtendableMessage.cs
View file @
804b6d84
...
...
@@ -102,8 +102,8 @@ namespace Google.ProtocolBuffers {
public
override
IDictionary
<
FieldDescriptor
,
object
>
AllFields
{
get
{
IDictionary
<
FieldDescriptor
,
object
>
result
=
GetMutableFieldMap
();
foreach
(
KeyValuePair
<
FieldDescriptor
,
object
>
entry
in
extensions
.
AllFields
)
{
result
[
entry
.
Key
]
=
entry
.
Value
;
foreach
(
KeyValuePair
<
IFieldDescriptorLite
,
object
>
entry
in
extensions
.
AllFields
)
{
result
[
(
FieldDescriptor
)
entry
.
Key
]
=
entry
.
Value
;
}
return
Dictionaries
.
AsReadOnly
(
result
);
}
...
...
@@ -173,9 +173,9 @@ namespace Google.ProtocolBuffers {
/// TODO(jonskeet): See if we can improve this in terms of readability.
/// </summary>
protected
class
ExtensionWriter
{
readonly
IEnumerator
<
KeyValuePair
<
FieldDescriptor
,
object
>>
iterator
;
readonly
IEnumerator
<
KeyValuePair
<
IFieldDescriptorLite
,
object
>>
iterator
;
readonly
FieldSet
extensions
;
KeyValuePair
<
FieldDescriptor
,
object
>?
next
=
null
;
KeyValuePair
<
IFieldDescriptorLite
,
object
>?
next
=
null
;
internal
ExtensionWriter
(
ExtendableMessage
<
TMessage
,
TBuilder
>
message
)
{
extensions
=
message
.
extensions
;
...
...
src/ProtocolBuffers/FieldSet.cs
View file @
804b6d84
This diff is collapsed.
Click to expand it.
src/ProtocolBuffers/GeneratedExtensionLite.cs
View file @
804b6d84
using
System
;
using
Google.ProtocolBuffers.Descriptors
;
namespace
Google.ProtocolBuffers
{
...
...
@@ -7,4 +8,111 @@ namespace Google.ProtocolBuffers {
object
ContainingType
{
get
;
}
IMessageLite
MessageDefaultInstance
{
get
;
}
}
public
class
ExtensionDescriptorLite
{
private
readonly
EnumLiteMap
enumTypeMap
;
private
readonly
int
number
;
private
readonly
FieldType
type
;
private
readonly
bool
isRepeated
;
private
readonly
bool
isPacked
;
public
ExtensionDescriptorLite
(
EnumLiteMap
enumTypeMap
,
int
number
,
FieldType
type
,
bool
isRepeated
,
bool
isPacked
)
{
this
.
enumTypeMap
=
enumTypeMap
;
this
.
number
=
number
;
this
.
type
=
type
;
this
.
isRepeated
=
isRepeated
;
this
.
isPacked
=
isPacked
;
}
public
int
Number
{
get
{
return
number
;
}
}
}
public
class
EnumLiteMap
{
}
public
class
GeneratedExtensionLite
<
TContainingType
,
TExtensionType
>
:
IGeneratedExtensionLite
where
TContainingType
:
IMessageLite
{
private
readonly
TContainingType
containingTypeDefaultInstance
;
private
readonly
TExtensionType
defaultValue
;
private
readonly
IMessageLite
messageDefaultInstance
;
private
readonly
ExtensionDescriptorLite
descriptor
;
// We can't always initialize a GeneratedExtension when we first construct
// it due to initialization order difficulties (namely, the default
// instances may not have been constructed yet). So, we construct an
// uninitialized GeneratedExtension once, then call internalInit() on it
// later. Generated code will always call internalInit() on all extensions
// as part of the static initialization code, and internalInit() throws an
// exception if called more than once, so this method is useless to users.
protected
GeneratedExtensionLite
(
TContainingType
containingTypeDefaultInstance
,
TExtensionType
defaultValue
,
IMessageLite
messageDefaultInstance
,
ExtensionDescriptorLite
descriptor
)
{
this
.
containingTypeDefaultInstance
=
containingTypeDefaultInstance
;
this
.
messageDefaultInstance
=
messageDefaultInstance
;
this
.
defaultValue
=
defaultValue
;
this
.
descriptor
=
descriptor
;
}
/** For use by generated code only. */
public
GeneratedExtensionLite
(
TContainingType
containingTypeDefaultInstance
,
TExtensionType
defaultValue
,
IMessageLite
messageDefaultInstance
,
EnumLiteMap
enumTypeMap
,
int
number
,
FieldType
type
)
:
this
(
containingTypeDefaultInstance
,
defaultValue
,
messageDefaultInstance
,
new
ExtensionDescriptorLite
(
enumTypeMap
,
number
,
type
,
false
/* isRepeated */
,
false
/* isPacked */
))
{
}
/** For use by generated code only. */
public
GeneratedExtensionLite
(
TContainingType
containingTypeDefaultInstance
,
TExtensionType
defaultValue
,
IMessageLite
messageDefaultInstance
,
EnumLiteMap
enumTypeMap
,
int
number
,
FieldType
type
,
bool
isPacked
)
:
this
(
containingTypeDefaultInstance
,
defaultValue
,
messageDefaultInstance
,
new
ExtensionDescriptorLite
(
enumTypeMap
,
number
,
type
,
true
/* isRepeated */
,
isPacked
))
{
}
/// <summary>
/// used for the extension registry
/// </summary>
object
IGeneratedExtensionLite
.
ContainingType
{
get
{
return
ContainingTypeDefaultInstance
;
}
}
/**
* Default instance of the type being extended, used to identify that type.
*/
public
TContainingType
ContainingTypeDefaultInstance
{
get
{
return
containingTypeDefaultInstance
;
}
}
/** Get the field number. */
public
int
Number
{
get
{
return
descriptor
.
Number
;
}
}
/**
* If the extension is an embedded message, this is the default instance of
* that type.
*/
public
IMessageLite
MessageDefaultInstance
{
get
{
return
messageDefaultInstance
;
}
}
}
}
\ No newline at end of file
src/ProtocolBuffers/ProtocolBuffers.csproj
View file @
804b6d84
...
...
@@ -90,6 +90,7 @@
<Compile
Include=
"Descriptors\PackageDescriptor.cs"
/>
<Compile
Include=
"Descriptors\ServiceDescriptor.cs"
/>
<Compile
Include=
"DynamicMessage.cs"
/>
<Compile
Include=
"EnumLite.cs"
/>
<Compile
Include=
"ExtendableBuilder.cs"
/>
<Compile
Include=
"ExtendableMessage.cs"
/>
<Compile
Include=
"ExtensionInfo.cs"
>
...
...
src/ProtocolBuffers/ProtocolBuffersLite.csproj
View file @
804b6d84
...
...
@@ -46,6 +46,8 @@
<ItemGroup>
<Reference
Include=
"mscorlib"
/>
<Reference
Include=
"System"
/>
<Reference
Include=
"System.Data"
/>
<Reference
Include=
"System.Xml"
/>
</ItemGroup>
<ItemGroup>
</ItemGroup>
...
...
@@ -55,7 +57,17 @@
<ItemGroup>
<Compile
Include=
"AbstractBuilderLite.cs"
/>
<Compile
Include=
"AbstractMessageLite.cs"
/>
<Compile
Include=
"Collections\Dictionaries.cs"
/>
<Compile
Include=
"Collections\Lists.cs"
/>
<Compile
Include=
"Descriptors\FieldMappingAttribute.cs"
/>
<Compile
Include=
"Descriptors\FieldType.cs"
/>
<Compile
Include=
"Descriptors\MappedType.cs"
/>
<Compile
Include=
"EnumLite.cs"
/>
<Compile
Include=
"ExtendableMessageLite.cs"
/>
<Compile
Include=
"FieldSet.cs"
/>
<Compile
Include=
"GeneratedBuilderLite.cs"
/>
<Compile
Include=
"GeneratedExtensionLite.cs"
/>
<Compile
Include=
"GeneratedMessageLite.cs"
/>
<Compile
Include=
"Properties\AssemblyInfo.cs"
/>
<Compile
Include=
"ByteString.cs"
/>
<Compile
Include=
"CodedInputStream.cs"
/>
...
...
@@ -66,6 +78,7 @@
<SubType>
Code
</SubType>
</Compile>
<Compile
Include=
"InvalidProtocolBufferException.cs"
/>
<Compile
Include=
"ThrowHelper.cs"
/>
<Compile
Include=
"UninitializedMessageException.cs"
/>
<Compile
Include=
"WireFormat.cs"
/>
</ItemGroup>
...
...
src/ProtocolBuffersLite.Test/ProtocolBuffersLite.Test.csproj
View file @
804b6d84
...
...
@@ -54,9 +54,13 @@
<Reference
Include=
"System.Xml"
/>
</ItemGroup>
<ItemGroup>
<Compile
Include=
"..\..\tmp\UnitTestLiteProtoFile.cs"
>
<Link>
UnitTestLiteProtoFile.cs
</Link>
</Compile>
<Compile
Include=
"..\ProtocolBuffers.Test\Properties\AssemblyInfo.cs"
>
<Link>
Properties\AssemblyInfo.cs
</Link>
</Compile>
<Compile
Include=
"Todo.cs"
/>
<None
Include=
"TestProtos\UnittestLite.cs"
/>
<None
Include=
"TestProtos\UnittestLiteImportsNonlite.cs"
/>
</ItemGroup>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment