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
6d0cbe72
Commit
6d0cbe72
authored
Aug 14, 2008
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use a switch instead of a map for WireFormat.
parent
09809820
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
17 deletions
+48
-17
WireFormatTest.cs
csharp/ProtocolBuffers.Test/WireFormatTest.cs
+9
-5
UnknownFieldSet.cs
csharp/ProtocolBuffers/UnknownFieldSet.cs
+1
-1
WireFormat.cs
csharp/ProtocolBuffers/WireFormat.cs
+38
-11
No files found.
csharp/ProtocolBuffers.Test/WireFormatTest.cs
View file @
6d0cbe72
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// See the License for the specific language governing permissions and
// limitations under the License.
// limitations under the License.
using
System.Reflection
;
using
Google.ProtocolBuffers.Descriptors
;
using
Google.ProtocolBuffers.Descriptors
;
using
Google.ProtocolBuffers.TestProtos
;
using
Google.ProtocolBuffers.TestProtos
;
using
NUnit.Framework
;
using
NUnit.Framework
;
...
@@ -21,13 +22,16 @@ namespace Google.ProtocolBuffers {
...
@@ -21,13 +22,16 @@ namespace Google.ProtocolBuffers {
[
TestFixture
]
[
TestFixture
]
public
class
WireFormatTest
{
public
class
WireFormatTest
{
/// <summary>
/// Keeps the attributes on FieldType and the switch statement in WireFormat in sync.
/// </summary>
[
Test
]
[
Test
]
public
void
FieldTypeToWireTypeMapping
()
{
public
void
FieldTypeToWireTypeMapping
()
{
foreach
(
FieldInfo
field
in
typeof
(
FieldType
).
GetFields
(
BindingFlags
.
Static
|
BindingFlags
.
Public
))
{
// Just test a few values
FieldType
fieldType
=
(
FieldType
)
field
.
GetValue
(
null
);
Assert
.
AreEqual
(
WireFormat
.
WireType
.
Fixed64
,
WireFormat
.
FieldTypeToWireFormatMap
[
FieldType
.
SFixed64
])
;
FieldMappingAttribute
mapping
=
(
FieldMappingAttribute
)
field
.
GetCustomAttributes
(
typeof
(
FieldMappingAttribute
),
false
)[
0
]
;
Assert
.
AreEqual
(
WireFormat
.
WireType
.
LengthDelimited
,
WireFormat
.
FieldTypeToWireFormatMap
[
FieldType
.
String
]
);
Assert
.
AreEqual
(
mapping
.
WireType
,
WireFormat
.
GetWireType
(
fieldType
)
);
Assert
.
AreEqual
(
WireFormat
.
WireType
.
LengthDelimited
,
WireFormat
.
FieldTypeToWireFormatMap
[
FieldType
.
Message
]);
}
}
}
[
Test
]
[
Test
]
...
...
csharp/ProtocolBuffers/UnknownFieldSet.cs
View file @
6d0cbe72
...
@@ -480,7 +480,7 @@ namespace Google.ProtocolBuffers {
...
@@ -480,7 +480,7 @@ namespace Google.ProtocolBuffers {
}
}
// Unknown field or wrong wire type. Skip.
// Unknown field or wrong wire type. Skip.
if
(
field
==
null
||
wireType
!=
WireFormat
.
FieldTypeToWireFormatMap
[
field
.
FieldType
]
)
{
if
(
field
==
null
||
wireType
!=
WireFormat
.
GetWireType
(
field
.
FieldType
)
)
{
return
MergeFieldFrom
(
tag
,
input
);
return
MergeFieldFrom
(
tag
,
input
);
}
}
...
...
csharp/ProtocolBuffers/WireFormat.cs
View file @
6d0cbe72
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// See the License for the specific language governing permissions and
// limitations under the License.
// limitations under the License.
using
System
;
using
System.Reflection
;
using
System.Reflection
;
using
Google.ProtocolBuffers.Descriptors
;
using
Google.ProtocolBuffers.Descriptors
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
...
@@ -77,19 +78,45 @@ namespace Google.ProtocolBuffers {
...
@@ -77,19 +78,45 @@ namespace Google.ProtocolBuffers {
}
}
/// <summary>
/// <summary>
///
Immutable mapping from field type to wire type. Built using the attributes on
///
Converts a field type to its wire type. Done with a switch for the sake
///
FieldType values
.
///
of speed - this is significantly faster than a dictionary lookup
.
/// </summary>
/// </summary>
public
static
readonly
IDictionary
<
FieldType
,
WireType
>
FieldTypeToWireFormatMap
=
MapFieldTypes
();
public
static
WireType
GetWireType
(
FieldType
fieldType
)
{
switch
(
fieldType
)
{
private
static
IDictionary
<
FieldType
,
WireType
>
MapFieldTypes
()
{
case
FieldType
.
Double
:
var
map
=
new
Dictionary
<
FieldType
,
WireType
>();
return
WireType
.
Fixed64
;
foreach
(
FieldInfo
field
in
typeof
(
FieldType
).
GetFields
(
BindingFlags
.
Static
|
BindingFlags
.
Public
))
{
case
FieldType
.
Float
:
FieldType
fieldType
=
(
FieldType
)
field
.
GetValue
(
null
);
return
WireType
.
Fixed32
;
FieldMappingAttribute
mapping
=
(
FieldMappingAttribute
)
field
.
GetCustomAttributes
(
typeof
(
FieldMappingAttribute
),
false
)[
0
];
case
FieldType
.
Int64
:
map
[
fieldType
]
=
mapping
.
WireType
;
case
FieldType
.
UInt64
:
case
FieldType
.
Int32
:
return
WireType
.
Varint
;
case
FieldType
.
Fixed64
:
return
WireType
.
Fixed64
;
case
FieldType
.
Fixed32
:
return
WireType
.
Fixed32
;
case
FieldType
.
Bool
:
return
WireType
.
Varint
;
case
FieldType
.
String
:
return
WireType
.
LengthDelimited
;
case
FieldType
.
Group
:
return
WireType
.
StartGroup
;
case
FieldType
.
Message
:
case
FieldType
.
Bytes
:
return
WireType
.
LengthDelimited
;
case
FieldType
.
UInt32
:
return
WireType
.
Varint
;
case
FieldType
.
SFixed32
:
return
WireType
.
Fixed32
;
case
FieldType
.
SFixed64
:
return
WireType
.
Fixed64
;
case
FieldType
.
SInt32
:
case
FieldType
.
SInt64
:
case
FieldType
.
Enum
:
return
WireType
.
Varint
;
default
:
throw
new
ArgumentOutOfRangeException
(
"No such field type"
);
}
}
return
Dictionaries
.
AsReadOnly
(
map
);
}
}
}
}
}
}
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