Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
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
flatbuffers
Commits
e4eb2864
Commit
e4eb2864
authored
Nov 18, 2015
by
Wouter van Oortmerssen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #312 from chobie/unity-fix
C# Unity (.NET 3.5) can't cast enum default value.
parents
097797bf
37e28d98
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
5 deletions
+24
-5
idl_gen_general.cpp
src/idl_gen_general.cpp
+23
-4
TestSimpleTableWithEnum.cs
tests/MyGame/Example/TestSimpleTableWithEnum.cs
+1
-1
No files found.
src/idl_gen_general.cpp
View file @
e4eb2864
...
...
@@ -409,6 +409,24 @@ static std::string GenDefaultValue(const LanguageParameters &lang, const Value &
:
value
.
constant
;
}
static
std
::
string
GenEnumDefaultValue
(
const
FieldDef
&
field
)
{
auto
enum_def
=
field
.
value
.
type
.
enum_def
;
auto
vec
=
enum_def
->
vals
.
vec
;
auto
default_value
=
StringToInt
(
field
.
value
.
constant
.
c_str
());
auto
result
=
field
.
value
.
constant
;
for
(
auto
it
=
vec
.
begin
();
it
!=
vec
.
end
();
++
it
)
{
auto
enum_val
=
**
it
;
if
(
enum_val
.
value
==
default_value
)
{
result
=
enum_def
->
name
+
"."
+
enum_val
.
name
;
break
;
}
}
return
result
;
}
static
void
GenEnum
(
const
LanguageParameters
&
lang
,
EnumDef
&
enum_def
,
std
::
string
*
code_ptr
)
{
std
::
string
&
code
=
*
code_ptr
;
...
...
@@ -873,14 +891,15 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
// supply all arguments, and thus won't compile when fields are added.
if
(
lang
.
language
!=
GeneratorOptions
::
kJava
)
{
code
+=
" = "
;
// in C#, enum values have their own type,
so we need to cast the
//
numeric value to the proper type
// in C#, enum values have their own type,
but Unity (specifically .Net 3.5) can't
//
cast enum type from numeric value.
if
(
lang
.
language
==
GeneratorOptions
::
kCSharp
&&
field
.
value
.
type
.
enum_def
!=
nullptr
&&
field
.
value
.
type
.
base_type
!=
BASE_TYPE_UNION
)
{
code
+=
"("
+
field
.
value
.
type
.
enum_def
->
name
+
")"
;
code
+=
GenEnumDefaultValue
(
field
);
}
else
{
code
+=
GenDefaultValue
(
lang
,
field
.
value
,
false
);
}
code
+=
GenDefaultValue
(
lang
,
field
.
value
,
false
);
}
}
code
+=
") {
\n
builder."
;
...
...
tests/MyGame/Example/TestSimpleTableWithEnum.cs
View file @
e4eb2864
...
...
@@ -14,7 +14,7 @@ public sealed class TestSimpleTableWithEnum : Table {
public
bool
MutateColor
(
Color
color
)
{
int
o
=
__offset
(
4
);
if
(
o
!=
0
)
{
bb
.
PutSbyte
(
o
+
bb_pos
,
(
sbyte
)
color
);
return
true
;
}
else
{
return
false
;
}
}
public
static
Offset
<
TestSimpleTableWithEnum
>
CreateTestSimpleTableWithEnum
(
FlatBufferBuilder
builder
,
Color
color
=
(
Color
)
2
)
{
Color
color
=
Color
.
Green
)
{
builder
.
StartObject
(
1
);
TestSimpleTableWithEnum
.
AddColor
(
builder
,
color
);
return
TestSimpleTableWithEnum
.
EndTestSimpleTableWithEnum
(
builder
);
...
...
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