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
a3a2bf89
Commit
a3a2bf89
authored
Jun 02, 2016
by
lakedaemon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Better C++ namespace management
parent
a9194c4c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
45 deletions
+31
-45
idl_gen_cpp.cpp
src/idl_gen_cpp.cpp
+31
-41
namespace_test2_generated.h
tests/namespace_test/namespace_test2_generated.h
+0
-4
No files found.
src/idl_gen_cpp.cpp
View file @
a3a2bf89
...
@@ -86,7 +86,7 @@ class CppGenerator : public BaseGenerator {
...
@@ -86,7 +86,7 @@ class CppGenerator : public BaseGenerator {
if
(
num_includes
)
code
+=
"
\n
"
;
if
(
num_includes
)
code
+=
"
\n
"
;
}
}
assert
(
!
c
ode_generator_cur_name_space
);
assert
(
!
c
ur_name_space_
);
// Generate forward declarations for all structs/tables, since they may
// Generate forward declarations for all structs/tables, since they may
// have circular references.
// have circular references.
...
@@ -94,7 +94,7 @@ class CppGenerator : public BaseGenerator {
...
@@ -94,7 +94,7 @@ class CppGenerator : public BaseGenerator {
it
!=
parser_
.
structs_
.
vec
.
end
();
++
it
)
{
it
!=
parser_
.
structs_
.
vec
.
end
();
++
it
)
{
auto
&
struct_def
=
**
it
;
auto
&
struct_def
=
**
it
;
if
(
!
struct_def
.
generated
)
{
if
(
!
struct_def
.
generated
)
{
CheckNameSpace
(
struct_def
,
&
code
);
CheckNameSpace
(
struct_def
.
defined_namespace
,
&
code
);
code
+=
"struct "
+
struct_def
.
name
+
";
\n\n
"
;
code
+=
"struct "
+
struct_def
.
name
+
";
\n\n
"
;
}
}
}
}
...
@@ -104,7 +104,7 @@ class CppGenerator : public BaseGenerator {
...
@@ -104,7 +104,7 @@ class CppGenerator : public BaseGenerator {
++
it
)
{
++
it
)
{
auto
&
enum_def
=
**
it
;
auto
&
enum_def
=
**
it
;
if
(
!
enum_def
.
generated
)
{
if
(
!
enum_def
.
generated
)
{
CheckNameSpace
(
**
it
,
&
code
);
CheckNameSpace
(
(
**
it
).
defined_namespace
,
&
code
);
GenEnum
(
**
it
,
&
code
);
GenEnum
(
**
it
,
&
code
);
}
}
}
}
...
@@ -114,7 +114,7 @@ class CppGenerator : public BaseGenerator {
...
@@ -114,7 +114,7 @@ class CppGenerator : public BaseGenerator {
it
!=
parser_
.
structs_
.
vec
.
end
();
++
it
)
{
it
!=
parser_
.
structs_
.
vec
.
end
();
++
it
)
{
auto
&
struct_def
=
**
it
;
auto
&
struct_def
=
**
it
;
if
(
struct_def
.
fixed
&&
!
struct_def
.
generated
)
{
if
(
struct_def
.
fixed
&&
!
struct_def
.
generated
)
{
CheckNameSpace
(
struct_def
,
&
code
);
CheckNameSpace
(
struct_def
.
defined_namespace
,
&
code
);
GenStruct
(
struct_def
,
&
code
);
GenStruct
(
struct_def
,
&
code
);
}
}
}
}
...
@@ -122,7 +122,7 @@ class CppGenerator : public BaseGenerator {
...
@@ -122,7 +122,7 @@ class CppGenerator : public BaseGenerator {
it
!=
parser_
.
structs_
.
vec
.
end
();
++
it
)
{
it
!=
parser_
.
structs_
.
vec
.
end
();
++
it
)
{
auto
&
struct_def
=
**
it
;
auto
&
struct_def
=
**
it
;
if
(
!
struct_def
.
fixed
&&
!
struct_def
.
generated
)
{
if
(
!
struct_def
.
fixed
&&
!
struct_def
.
generated
)
{
CheckNameSpace
(
struct_def
,
&
code
);
CheckNameSpace
(
struct_def
.
defined_namespace
,
&
code
);
GenTable
(
struct_def
,
&
code
);
GenTable
(
struct_def
,
&
code
);
}
}
}
}
...
@@ -132,14 +132,14 @@ class CppGenerator : public BaseGenerator {
...
@@ -132,14 +132,14 @@ class CppGenerator : public BaseGenerator {
++
it
)
{
++
it
)
{
auto
&
enum_def
=
**
it
;
auto
&
enum_def
=
**
it
;
if
(
enum_def
.
is_union
&&
!
enum_def
.
generated
)
{
if
(
enum_def
.
is_union
&&
!
enum_def
.
generated
)
{
CheckNameSpace
(
enum_def
,
&
code
);
CheckNameSpace
(
enum_def
.
defined_namespace
,
&
code
);
GenEnumPost
(
enum_def
,
&
code
);
GenEnumPost
(
enum_def
,
&
code
);
}
}
}
}
// Generate convenient global helper functions:
// Generate convenient global helper functions:
if
(
parser_
.
root_struct_def_
)
{
if
(
parser_
.
root_struct_def_
)
{
CheckNameSpace
(
*
parser_
.
root_struct_def_
,
&
code
);
CheckNameSpace
(
(
*
parser_
.
root_struct_def_
).
defined_namespace
,
&
code
);
auto
&
name
=
parser_
.
root_struct_def_
->
name
;
auto
&
name
=
parser_
.
root_struct_def_
->
name
;
std
::
string
qualified_name
=
std
::
string
qualified_name
=
parser_
.
namespaces_
.
back
()
->
GetFullyQualifiedName
(
name
);
parser_
.
namespaces_
.
back
()
->
GetFullyQualifiedName
(
name
);
...
@@ -195,25 +195,23 @@ class CppGenerator : public BaseGenerator {
...
@@ -195,25 +195,23 @@ class CppGenerator : public BaseGenerator {
code
+=
"); }
\n\n
"
;
code
+=
"); }
\n\n
"
;
}
}
assert
(
code_generator_cur_name_space
);
assert
(
cur_name_space_
);
CloseNestedNameSpaces
(
code_generator_cur_name_space
,
&
code
);
CheckNameSpace
(
nullptr
,
&
code
);
code_generator_cur_name_space
=
nullptr
;
// Close the include guard.
// Close the include guard.
code
+=
"
\n
#endif // "
+
include_guard
+
"
\n
"
;
code
+=
"#endif // "
+
include_guard
+
"
\n
"
;
return
SaveFile
(
GeneratedFileName
(
path_
,
file_name_
).
c_str
(),
code
,
false
);
return
SaveFile
(
GeneratedFileName
(
path_
,
file_name_
).
c_str
(),
code
,
false
);
}
}
private
:
private
:
// This tracks the current namespace so we can insert namespace declarations.
// This tracks the current namespace so we can insert namespace declarations.
const
Namespace
*
c
ode_generator_cur_name_space
=
nullptr
;
const
Namespace
*
c
ur_name_space_
=
nullptr
;
// Ensure that a type is prefixed with its namespace whenever it is used
// Ensure that a type is prefixed with its namespace whenever it is used
// outside of its namespace.
// outside of its namespace.
std
::
string
WrapInNameSpace
(
const
Namespace
*
ns
,
const
std
::
string
&
name
)
{
std
::
string
WrapInNameSpace
(
const
Namespace
*
ns
,
const
std
::
string
&
name
)
{
if
(
c
ode_generator_cur_name_space
!=
ns
)
{
if
(
c
ur_name_space_
!=
ns
)
{
std
::
string
qualified_name
;
std
::
string
qualified_name
;
for
(
auto
it
=
ns
->
components
.
begin
();
it
!=
ns
->
components
.
end
();
++
it
)
{
for
(
auto
it
=
ns
->
components
.
begin
();
it
!=
ns
->
components
.
end
();
++
it
)
{
qualified_name
+=
*
it
+
"::"
;
qualified_name
+=
*
it
+
"::"
;
...
@@ -863,33 +861,25 @@ class CppGenerator : public BaseGenerator {
...
@@ -863,33 +861,25 @@ class CppGenerator : public BaseGenerator {
code
+=
NumToString
(
struct_def
.
bytesize
)
+
");
\n\n
"
;
code
+=
NumToString
(
struct_def
.
bytesize
)
+
");
\n\n
"
;
}
}
void
GenerateNestedNameSpaces
(
const
Namespace
*
ns
,
std
::
string
*
code_ptr
)
{
// Set up the correct namespace. Only open a namespace if
for
(
auto
it
=
ns
->
components
.
begin
();
it
!=
ns
->
components
.
end
();
++
it
)
{
// the existing one is different (opening/closing only what is necessary)
*
code_ptr
+=
"namespace "
+
*
it
+
" {
\n
"
;
// the file must start and end with an empty namespace
}
void
CheckNameSpace
(
const
Namespace
*
ns
,
std
::
string
*
code_ptr
)
{
}
if
(
cur_name_space_
==
ns
)
return
;
auto
s1
=
cur_name_space_
==
nullptr
?
0
:
cur_name_space_
->
components
.
size
();
void
CloseNestedNameSpaces
(
const
Namespace
*
ns
,
std
::
string
*
code_ptr
)
{
auto
s2
=
ns
==
nullptr
?
0
:
ns
->
components
.
size
();
for
(
auto
it
=
ns
->
components
.
rbegin
();
it
!=
ns
->
components
.
rend
();
++
it
)
{
std
::
vector
<
std
::
string
>::
size_type
limit
=
0
;
*
code_ptr
+=
"} // namespace "
+
*
it
+
"
\n
"
;
while
(
limit
<
s1
&&
limit
<
s2
&&
}
ns
->
components
[
limit
]
==
cur_name_space_
->
components
[
limit
])
}
limit
++
;
for
(
auto
j
=
s1
;
j
>
limit
;
--
j
)
void
CheckNameSpace
(
const
Definition
&
def
,
std
::
string
*
code_ptr
)
{
*
code_ptr
+=
// Set up the correct namespace. Only open a namespace if
"} // namespace "
+
cur_name_space_
->
components
[
j
-
1
]
+
"
\n
"
;
// the existing one is different.
if
(
s1
!=
limit
)
*
code_ptr
+=
"
\n
"
;
// TODO: this could be done more intelligently, by sorting to
for
(
auto
j
=
limit
;
j
!=
s2
;
++
j
)
// namespace path and only opening/closing what is necessary, but that's
*
code_ptr
+=
"namespace "
+
ns
->
components
[
j
]
+
" {
\n
"
;
// quite a bit more complexity.
if
(
s2
!=
limit
)
*
code_ptr
+=
"
\n
"
;
if
(
code_generator_cur_name_space
!=
def
.
defined_namespace
)
{
cur_name_space_
=
ns
;
if
(
code_generator_cur_name_space
)
{
CloseNestedNameSpaces
(
code_generator_cur_name_space
,
code_ptr
);
if
(
code_generator_cur_name_space
->
components
.
size
())
*
code_ptr
+=
"
\n
"
;
}
GenerateNestedNameSpaces
(
def
.
defined_namespace
,
code_ptr
);
code_generator_cur_name_space
=
def
.
defined_namespace
;
if
(
code_generator_cur_name_space
->
components
.
size
())
*
code_ptr
+=
"
\n
"
;
}
}
}
};
};
}
// namespace cpp
}
// namespace cpp
...
...
tests/namespace_test/namespace_test2_generated.h
View file @
a3a2bf89
...
@@ -23,10 +23,6 @@ namespace NamespaceA {
...
@@ -23,10 +23,6 @@ namespace NamespaceA {
struct
SecondTableInA
;
struct
SecondTableInA
;
}
// namespace NamespaceA
namespace
NamespaceA
{
struct
TableInFirstNS
FLATBUFFERS_FINAL_CLASS
:
private
flatbuffers
::
Table
{
struct
TableInFirstNS
FLATBUFFERS_FINAL_CLASS
:
private
flatbuffers
::
Table
{
enum
{
enum
{
VT_FOO_TABLE
=
4
,
VT_FOO_TABLE
=
4
,
...
...
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