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
249f71a1
Commit
249f71a1
authored
May 06, 2015
by
Brett Cooley
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial support for propagating namespaces from schema files to generated code
Change-Id: Ifc10c54845ea7553586d1896d509314d68e9ab0f
parent
1d138fbe
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
65 additions
and
17 deletions
+65
-17
.gitignore
.gitignore
+2
-0
idl.h
include/flatbuffers/idl.h
+6
-0
monster_generated.h
samples/monster_generated.h
+3
-3
idl_gen_cpp.cpp
src/idl_gen_cpp.cpp
+26
-7
idl_parser.cpp
src/idl_parser.cpp
+24
-3
monster_test_generated.h
tests/monster_test_generated.h
+4
-4
No files found.
.gitignore
View file @
249f71a1
...
...
@@ -19,7 +19,9 @@
**/*.dir/**
**/CMakeFiles/**
**/cmake_install.cmake
**/install_manifest.txt
**/CMakeCache.txt
**/CMakeTestfile.cmake
**/Debug/**
**/Release/**
build.xml
...
...
include/flatbuffers/idl.h
View file @
249f71a1
...
...
@@ -313,6 +313,12 @@ class Parser {
// Mark all definitions as already having code generated.
void
MarkGenerated
();
// Given a (potentally unqualified) name, return the "fully qualified" name
// which has a full namespaced descriptor. If the parser has no current
// namespace context, or if the name passed is partially qualified the input
// is simply returned.
std
::
string
GetFullyQualifiedName
(
const
std
::
string
&
name
)
const
;
// Get the files recursively included by the given file. The returned
// container will have at least the given file.
std
::
set
<
std
::
string
>
GetIncludedFilesRecursive
(
...
...
samples/monster_generated.h
View file @
249f71a1
...
...
@@ -127,13 +127,13 @@ inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, An
}
}
inline
const
M
onster
*
GetMonster
(
const
void
*
buf
)
{
return
flatbuffers
::
GetRoot
<
Monster
>
(
buf
);
}
inline
const
M
yGame
::
Sample
::
Monster
*
GetMonster
(
const
void
*
buf
)
{
return
flatbuffers
::
GetRoot
<
MyGame
::
Sample
::
Monster
>
(
buf
);
}
inline
Monster
*
GetMutableMonster
(
void
*
buf
)
{
return
flatbuffers
::
GetMutableRoot
<
Monster
>
(
buf
);
}
inline
bool
VerifyMonsterBuffer
(
flatbuffers
::
Verifier
&
verifier
)
{
return
verifier
.
VerifyBuffer
<
Monster
>
();
}
inline
bool
VerifyMonsterBuffer
(
flatbuffers
::
Verifier
&
verifier
)
{
return
verifier
.
VerifyBuffer
<
M
yGame
::
Sample
::
M
onster
>
();
}
inline
void
FinishMonsterBuffer
(
flatbuffers
::
FlatBufferBuilder
&
fbb
,
flatbuffers
::
Offset
<
Monster
>
root
)
{
fbb
.
Finish
(
root
);
}
inline
void
FinishMonsterBuffer
(
flatbuffers
::
FlatBufferBuilder
&
fbb
,
flatbuffers
::
Offset
<
M
yGame
::
Sample
::
M
onster
>
root
)
{
fbb
.
Finish
(
root
);
}
}
// namespace Sample
}
// namespace MyGame
...
...
src/idl_gen_cpp.cpp
View file @
249f71a1
...
...
@@ -44,6 +44,18 @@ static std::string WrapInNameSpace(const Parser &parser,
return
WrapInNameSpace
(
parser
,
def
.
defined_namespace
,
def
.
name
);
}
// Translates a qualified name in flatbuffer text format to the same name in
// the equivalent C++ namepsace.
static
std
::
string
TranslateNameSpace
(
const
std
::
string
&
qualified_name
)
{
std
::
string
cpp_qualified_name
=
qualified_name
;
size_t
start_pos
=
0
;
while
((
start_pos
=
cpp_qualified_name
.
find
(
"."
,
start_pos
))
!=
std
::
string
::
npos
)
{
cpp_qualified_name
.
replace
(
start_pos
,
1
,
"::"
);
}
return
cpp_qualified_name
;
}
// Return a C++ type from the table in idl.h
static
std
::
string
GenTypeBasic
(
const
Parser
&
parser
,
const
Type
&
type
,
...
...
@@ -258,11 +270,15 @@ static void GenTable(const Parser &parser, StructDef &struct_def,
}
auto
nested
=
field
.
attributes
.
Lookup
(
"nested_flatbuffer"
);
if
(
nested
)
{
auto
nested_root
=
parser
.
structs_
.
Lookup
(
nested
->
constant
);
std
::
string
qualified_name
=
parser
.
GetFullyQualifiedName
(
nested
->
constant
);
auto
nested_root
=
parser
.
structs_
.
Lookup
(
qualified_name
);
assert
(
nested_root
);
// Guaranteed to exist by parser.
code
+=
" const "
+
nested_root
->
name
+
" *"
+
field
.
name
;
std
::
string
cpp_qualified_name
=
TranslateNameSpace
(
qualified_name
);
code
+=
" const "
+
cpp_qualified_name
+
" *"
+
field
.
name
;
code
+=
"_nested_root() const { return flatbuffers::GetRoot<"
;
code
+=
nested_root
->
name
+
">("
+
field
.
name
+
"()->Data()); }
\n
"
;
code
+=
cpp_qualified_
name
+
">("
+
field
.
name
+
"()->Data()); }
\n
"
;
}
// Generate a comparison function for this field if it is a key.
if
(
field
.
key
)
{
...
...
@@ -685,11 +701,14 @@ std::string GenerateCPP(const Parser &parser,
// Generate convenient global helper functions:
if
(
parser
.
root_struct_def
)
{
auto
&
name
=
parser
.
root_struct_def
->
name
;
std
::
string
qualified_name
=
parser
.
GetFullyQualifiedName
(
name
);
std
::
string
cpp_qualified_name
=
TranslateNameSpace
(
qualified_name
);
// The root datatype accessor:
code
+=
"inline const "
+
name
+
" *Get"
;
code
+=
"inline const "
+
cpp_qualified_
name
+
" *Get"
;
code
+=
name
;
code
+=
"(const void *buf) { return flatbuffers::GetRoot<"
;
code
+=
name
+
">(buf); }
\n\n
"
;
code
+=
cpp_qualified_
name
+
">(buf); }
\n\n
"
;
if
(
opts
.
mutable_buffer
)
{
code
+=
"inline "
+
name
+
" *GetMutable"
;
code
+=
name
;
...
...
@@ -702,7 +721,7 @@ std::string GenerateCPP(const Parser &parser,
code
+=
name
;
code
+=
"Buffer(flatbuffers::Verifier &verifier) { "
"return verifier.VerifyBuffer<"
;
code
+=
name
+
">(); }
\n\n
"
;
code
+=
cpp_qualified_
name
+
">(); }
\n\n
"
;
if
(
parser
.
file_identifier_
.
length
())
{
// Return the identifier
...
...
@@ -727,7 +746,7 @@ std::string GenerateCPP(const Parser &parser,
// Finish a buffer with a given root object:
code
+=
"inline void Finish"
+
name
;
code
+=
"Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<"
;
code
+=
name
+
"> root) { fbb.Finish(root"
;
code
+=
cpp_qualified_
name
+
"> root) { fbb.Finish(root"
;
if
(
parser
.
file_identifier_
.
length
())
code
+=
", "
+
name
+
"Identifier()"
;
code
+=
"); }
\n\n
"
;
...
...
src/idl_parser.cpp
View file @
249f71a1
...
...
@@ -773,12 +773,13 @@ void Parser::ParseSingleValue(Value &e) {
}
StructDef
*
Parser
::
LookupCreateStruct
(
const
std
::
string
&
name
)
{
auto
struct_def
=
structs_
.
Lookup
(
name
);
std
::
string
qualified_name
=
GetFullyQualifiedName
(
name
);
auto
struct_def
=
structs_
.
Lookup
(
qualified_name
);
if
(
!
struct_def
)
{
// Rather than failing, we create a "pre declared" StructDef, due to
// circular references, and check for errors at the end of parsing.
struct_def
=
new
StructDef
();
structs_
.
Add
(
name
,
struct_def
);
structs_
.
Add
(
qualified_
name
,
struct_def
);
struct_def
->
name
=
name
;
struct_def
->
predecl
=
true
;
struct_def
->
defined_namespace
=
namespaces_
.
back
();
...
...
@@ -951,10 +952,30 @@ void Parser::ParseDecl() {
}
bool
Parser
::
SetRootType
(
const
char
*
name
)
{
root_struct_def
=
structs_
.
Lookup
(
name
);
root_struct_def
=
structs_
.
Lookup
(
GetFullyQualifiedName
(
name
)
);
return
root_struct_def
!=
nullptr
;
}
std
::
string
Parser
::
GetFullyQualifiedName
(
const
std
::
string
&
name
)
const
{
Namespace
*
ns
=
namespaces_
.
back
();
// Early exit if we don't have a defined namespace, or if the name is already
// partially qualified
if
(
ns
->
components
.
size
()
==
0
||
name
.
find
(
"."
)
!=
std
::
string
::
npos
)
{
return
name
;
}
std
::
stringstream
stream
;
for
(
size_t
i
=
0
;
i
!=
ns
->
components
.
size
();
++
i
)
{
if
(
i
!=
0
)
{
stream
<<
"."
;
}
stream
<<
ns
->
components
[
i
];
}
stream
<<
"."
<<
name
;
return
stream
.
str
();
}
void
Parser
::
MarkGenerated
()
{
// Since the Parser object retains definitions across files, we must
// ensure we only output code for definitions once, in the file they are first
...
...
tests/monster_test_generated.h
View file @
249f71a1
...
...
@@ -167,7 +167,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
Monster
*
mutable_enemy
()
{
return
GetPointer
<
Monster
*>
(
28
);
}
const
flatbuffers
::
Vector
<
uint8_t
>
*
testnestedflatbuffer
()
const
{
return
GetPointer
<
const
flatbuffers
::
Vector
<
uint8_t
>
*>
(
30
);
}
flatbuffers
::
Vector
<
uint8_t
>
*
mutable_testnestedflatbuffer
()
{
return
GetPointer
<
flatbuffers
::
Vector
<
uint8_t
>
*>
(
30
);
}
const
M
onster
*
testnestedflatbuffer_nested_root
()
const
{
return
flatbuffers
::
GetRoot
<
Monster
>
(
testnestedflatbuffer
()
->
Data
());
}
const
M
yGame
::
Example
::
Monster
*
testnestedflatbuffer_nested_root
()
const
{
return
flatbuffers
::
GetRoot
<
MyGame
::
Example
::
Monster
>
(
testnestedflatbuffer
()
->
Data
());
}
const
Stat
*
testempty
()
const
{
return
GetPointer
<
const
Stat
*>
(
32
);
}
Stat
*
mutable_testempty
()
{
return
GetPointer
<
Stat
*>
(
32
);
}
uint8_t
testbool
()
const
{
return
GetField
<
uint8_t
>
(
34
,
0
);
}
...
...
@@ -322,11 +322,11 @@ inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, An
}
}
inline
const
M
onster
*
GetMonster
(
const
void
*
buf
)
{
return
flatbuffers
::
GetRoot
<
Monster
>
(
buf
);
}
inline
const
M
yGame
::
Example
::
Monster
*
GetMonster
(
const
void
*
buf
)
{
return
flatbuffers
::
GetRoot
<
MyGame
::
Example
::
Monster
>
(
buf
);
}
inline
Monster
*
GetMutableMonster
(
void
*
buf
)
{
return
flatbuffers
::
GetMutableRoot
<
Monster
>
(
buf
);
}
inline
bool
VerifyMonsterBuffer
(
flatbuffers
::
Verifier
&
verifier
)
{
return
verifier
.
VerifyBuffer
<
Monster
>
();
}
inline
bool
VerifyMonsterBuffer
(
flatbuffers
::
Verifier
&
verifier
)
{
return
verifier
.
VerifyBuffer
<
M
yGame
::
Example
::
M
onster
>
();
}
inline
const
char
*
MonsterIdentifier
()
{
return
"MONS"
;
}
...
...
@@ -334,7 +334,7 @@ inline bool MonsterBufferHasIdentifier(const void *buf) { return flatbuffers::Bu
inline
const
char
*
MonsterExtension
()
{
return
"mon"
;
}
inline
void
FinishMonsterBuffer
(
flatbuffers
::
FlatBufferBuilder
&
fbb
,
flatbuffers
::
Offset
<
Monster
>
root
)
{
fbb
.
Finish
(
root
,
MonsterIdentifier
());
}
inline
void
FinishMonsterBuffer
(
flatbuffers
::
FlatBufferBuilder
&
fbb
,
flatbuffers
::
Offset
<
M
yGame
::
Example
::
M
onster
>
root
)
{
fbb
.
Finish
(
root
,
MonsterIdentifier
());
}
}
// namespace Example
}
// namespace MyGame
...
...
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