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
1661f3a2
Commit
1661f3a2
authored
Jul 09, 2016
by
daksenik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added function GenSimpleParam. Tests added.
parent
dfbda986
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
48 deletions
+77
-48
idl_gen_cpp.cpp
src/idl_gen_cpp.cpp
+38
-48
monster_test_generated.h
tests/monster_test_generated.h
+39
-0
No files found.
src/idl_gen_cpp.cpp
View file @
1661f3a2
...
...
@@ -457,6 +457,30 @@ class CppGenerator : public BaseGenerator {
:
field
.
value
.
constant
;
}
void
GenSimpleParam
(
std
::
string
&
code
,
FieldDef
&
field
)
{
code
+=
",
\n
"
+
GenTypeWire
(
field
.
value
.
type
,
" "
,
true
);
code
+=
field
.
name
+
" = "
;
if
(
field
.
value
.
type
.
enum_def
&&
IsScalar
(
field
.
value
.
type
.
base_type
))
{
auto
ev
=
field
.
value
.
type
.
enum_def
->
ReverseLookup
(
static_cast
<
int
>
(
StringToInt
(
field
.
value
.
constant
.
c_str
())),
false
);
if
(
ev
)
{
code
+=
WrapInNameSpace
(
field
.
value
.
type
.
enum_def
->
defined_namespace
,
GetEnumVal
(
*
field
.
value
.
type
.
enum_def
,
*
ev
,
parser_
.
opts
));
}
else
{
code
+=
GenUnderlyingCast
(
field
,
true
,
field
.
value
.
constant
);
}
}
else
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_BOOL
)
{
code
+=
field
.
value
.
constant
==
"0"
?
"false"
:
"true"
;
}
else
{
code
+=
GenDefaultConstant
(
field
);
}
}
// Generate an accessor struct, builder structs & function for a table.
void
GenTable
(
StructDef
&
struct_def
,
std
::
string
*
code_ptr
)
{
std
::
string
&
code
=
*
code_ptr
;
...
...
@@ -679,9 +703,7 @@ class CppGenerator : public BaseGenerator {
// Generate a convenient CreateX function that uses the above builder
// to create a table in one go.
bool
gen_vector_pars
=
false
;
code
+=
"inline flatbuffers::Offset<"
+
struct_def
.
name
+
"> Create"
;
code
+=
struct_def
.
name
;
code
+=
"(flatbuffers::FlatBufferBuilder &_fbb"
;
...
...
@@ -689,25 +711,11 @@ class CppGenerator : public BaseGenerator {
it
!=
struct_def
.
fields
.
vec
.
end
();
++
it
)
{
auto
&
field
=
**
it
;
if
(
!
field
.
deprecated
)
{
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_STRING
||
field
.
value
.
type
.
base_type
==
BASE_TYPE_VECTOR
)
gen_vector_pars
=
true
;
code
+=
",
\n
"
+
GenTypeWire
(
field
.
value
.
type
,
" "
,
true
);
code
+=
field
.
name
+
" = "
;
if
(
field
.
value
.
type
.
enum_def
&&
IsScalar
(
field
.
value
.
type
.
base_type
))
{
auto
ev
=
field
.
value
.
type
.
enum_def
->
ReverseLookup
(
static_cast
<
int
>
(
StringToInt
(
field
.
value
.
constant
.
c_str
())),
false
);
if
(
ev
)
{
code
+=
WrapInNameSpace
(
field
.
value
.
type
.
enum_def
->
defined_namespace
,
GetEnumVal
(
*
field
.
value
.
type
.
enum_def
,
*
ev
,
parser_
.
opts
));
}
else
{
code
+=
GenUnderlyingCast
(
field
,
true
,
field
.
value
.
constant
);
}
}
else
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_BOOL
)
{
code
+=
field
.
value
.
constant
==
"0"
?
"false"
:
"true"
;
}
else
{
code
+=
GenDefaultConstant
(
field
);
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_STRING
||
field
.
value
.
type
.
base_type
==
BASE_TYPE_VECTOR
)
{
gen_vector_pars
=
true
;
}
GenSimpleParam
(
code
,
field
);
}
}
code
+=
") {
\n
"
+
struct_def
.
name
+
"Builder builder_(_fbb);
\n
"
;
...
...
@@ -738,31 +746,11 @@ class CppGenerator : public BaseGenerator {
code
+=
field
.
name
+
" = nullptr"
;
}
else
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_VECTOR
)
{
code
+=
",
\n
std::vector<"
+
GenTypeWire
(
field
.
value
.
type
.
VectorType
(),
""
,
false
)
+
"> *"
+
field
.
name
;
code
+=
" = nullptr"
;
}
else
{
code
+=
",
\n
"
+
GenTypeWire
(
field
.
value
.
type
,
" "
,
true
);
code
+=
field
.
name
+
" = "
;
if
(
field
.
value
.
type
.
enum_def
&&
IsScalar
(
field
.
value
.
type
.
base_type
))
{
auto
ev
=
field
.
value
.
type
.
enum_def
->
ReverseLookup
(
static_cast
<
int
>
(
StringToInt
(
field
.
value
.
constant
.
c_str
())),
false
);
if
(
ev
)
{
code
+=
WrapInNameSpace
(
field
.
value
.
type
.
enum_def
->
defined_namespace
,
GetEnumVal
(
*
field
.
value
.
type
.
enum_def
,
*
ev
,
parser_
.
opts
));
}
else
{
code
+=
GenUnderlyingCast
(
field
,
true
,
field
.
value
.
constant
);
}
}
else
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_BOOL
)
{
code
+=
field
.
value
.
constant
==
"0"
?
"false"
:
"true"
;
}
else
{
code
+=
GenDefaultConstant
(
field
);
}
code
+=
",
\n
std::vector<"
;
code
+=
GenTypeWire
(
field
.
value
.
type
.
VectorType
(),
""
,
false
);
code
+=
"> *"
+
field
.
name
+
" = nullptr"
;
}
else
{
GenSimpleParam
(
code
,
field
);
}
}
}
...
...
@@ -776,11 +764,13 @@ class CppGenerator : public BaseGenerator {
auto
&
field
=
**
it
;
if
(
!
field
.
deprecated
)
{
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_STRING
)
{
code
+=
", "
+
field
.
name
+
"
==nullptr
? 0 : "
;
code
+=
", "
+
field
.
name
+
"
== nullptr
? 0 : "
;
code
+=
"_fbb.CreateString("
+
field
.
name
+
")"
;
}
else
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_VECTOR
)
{
code
+=
", "
+
field
.
name
+
"==nullptr? 0 : "
;
code
+=
"_fbb.CreateVector<"
+
GenTypeWire
(
field
.
value
.
type
.
VectorType
(),
""
,
false
)
+
">(*"
+
field
.
name
+
")"
;
code
+=
", "
+
field
.
name
+
" == nullptr ? 0 : "
;
code
+=
"_fbb.CreateVector<"
;
code
+=
GenTypeWire
(
field
.
value
.
type
.
VectorType
(),
""
,
false
);
code
+=
">(*"
+
field
.
name
+
")"
;
}
else
code
+=
", "
+
field
.
name
;
}
}
...
...
tests/monster_test_generated.h
View file @
1661f3a2
...
...
@@ -215,6 +215,13 @@ inline flatbuffers::Offset<Stat> CreateStat(flatbuffers::FlatBufferBuilder &_fbb
return
builder_
.
Finish
();
}
inline
flatbuffers
::
Offset
<
Stat
>
CreateStat
(
flatbuffers
::
FlatBufferBuilder
&
_fbb
,
const
char
*
id
=
nullptr
,
int64_t
val
=
0
,
uint16_t
count
=
0
)
{
return
CreateStat
(
_fbb
,
id
==
nullptr
?
0
:
_fbb
.
CreateString
(
id
),
val
,
count
);
}
/// an example documentation comment: monster object
struct
Monster
FLATBUFFERS_FINAL_CLASS
:
private
flatbuffers
::
Table
{
enum
{
...
...
@@ -457,6 +464,38 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
return
builder_
.
Finish
();
}
inline
flatbuffers
::
Offset
<
Monster
>
CreateMonster
(
flatbuffers
::
FlatBufferBuilder
&
_fbb
,
const
Vec3
*
pos
=
0
,
int16_t
mana
=
150
,
int16_t
hp
=
100
,
const
char
*
name
=
nullptr
,
std
::
vector
<
uint8_t
>
*
inventory
=
nullptr
,
Color
color
=
Color_Blue
,
Any
test_type
=
Any_NONE
,
flatbuffers
::
Offset
<
void
>
test
=
0
,
std
::
vector
<
const
Test
*>
*
test4
=
nullptr
,
std
::
vector
<
flatbuffers
::
Offset
<
flatbuffers
::
String
>>
*
testarrayofstring
=
nullptr
,
std
::
vector
<
flatbuffers
::
Offset
<
Monster
>>
*
testarrayoftables
=
nullptr
,
flatbuffers
::
Offset
<
Monster
>
enemy
=
0
,
std
::
vector
<
uint8_t
>
*
testnestedflatbuffer
=
nullptr
,
flatbuffers
::
Offset
<
Stat
>
testempty
=
0
,
bool
testbool
=
false
,
int32_t
testhashs32_fnv1
=
0
,
uint32_t
testhashu32_fnv1
=
0
,
int64_t
testhashs64_fnv1
=
0
,
uint64_t
testhashu64_fnv1
=
0
,
int32_t
testhashs32_fnv1a
=
0
,
uint32_t
testhashu32_fnv1a
=
0
,
int64_t
testhashs64_fnv1a
=
0
,
uint64_t
testhashu64_fnv1a
=
0
,
std
::
vector
<
uint8_t
>
*
testarrayofbools
=
nullptr
,
float
testf
=
3
.
14159
f
,
float
testf2
=
3
.
0
f
,
float
testf3
=
0
.
0
f
,
std
::
vector
<
flatbuffers
::
Offset
<
flatbuffers
::
String
>>
*
testarrayofstring2
=
nullptr
)
{
return
CreateMonster
(
_fbb
,
pos
,
mana
,
hp
,
name
==
nullptr
?
0
:
_fbb
.
CreateString
(
name
),
inventory
==
nullptr
?
0
:
_fbb
.
CreateVector
<
uint8_t
>
(
*
inventory
),
color
,
test_type
,
test
,
test4
==
nullptr
?
0
:
_fbb
.
CreateVector
<
const
Test
*>
(
*
test4
),
testarrayofstring
==
nullptr
?
0
:
_fbb
.
CreateVector
<
flatbuffers
::
Offset
<
flatbuffers
::
String
>>
(
*
testarrayofstring
),
testarrayoftables
==
nullptr
?
0
:
_fbb
.
CreateVector
<
flatbuffers
::
Offset
<
Monster
>>
(
*
testarrayoftables
),
enemy
,
testnestedflatbuffer
==
nullptr
?
0
:
_fbb
.
CreateVector
<
uint8_t
>
(
*
testnestedflatbuffer
),
testempty
,
testbool
,
testhashs32_fnv1
,
testhashu32_fnv1
,
testhashs64_fnv1
,
testhashu64_fnv1
,
testhashs32_fnv1a
,
testhashu32_fnv1a
,
testhashs64_fnv1a
,
testhashu64_fnv1a
,
testarrayofbools
==
nullptr
?
0
:
_fbb
.
CreateVector
<
uint8_t
>
(
*
testarrayofbools
),
testf
,
testf2
,
testf3
,
testarrayofstring2
==
nullptr
?
0
:
_fbb
.
CreateVector
<
flatbuffers
::
Offset
<
flatbuffers
::
String
>>
(
*
testarrayofstring2
));
}
inline
bool
VerifyAny
(
flatbuffers
::
Verifier
&
verifier
,
const
void
*
union_obj
,
Any
type
)
{
switch
(
type
)
{
case
Any_NONE
:
return
true
;
...
...
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