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
15863c34
Commit
15863c34
authored
Jun 07, 2016
by
lakedaemon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SetNameSpace algorithm documentation and more meaningful variable names
parent
a3a2bf89
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
20 deletions
+32
-20
idl_gen_cpp.cpp
src/idl_gen_cpp.cpp
+32
-20
No files found.
src/idl_gen_cpp.cpp
View file @
15863c34
...
@@ -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
)
{
Check
NameSpace
(
struct_def
.
defined_namespace
,
&
code
);
Set
NameSpace
(
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
)
{
Check
NameSpace
((
**
it
).
defined_namespace
,
&
code
);
Set
NameSpace
((
**
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
)
{
Check
NameSpace
(
struct_def
.
defined_namespace
,
&
code
);
Set
NameSpace
(
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
)
{
Check
NameSpace
(
struct_def
.
defined_namespace
,
&
code
);
Set
NameSpace
(
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
)
{
Check
NameSpace
(
enum_def
.
defined_namespace
,
&
code
);
Set
NameSpace
(
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_
)
{
Check
NameSpace
((
*
parser_
.
root_struct_def_
).
defined_namespace
,
&
code
);
Set
NameSpace
((
*
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
);
...
@@ -196,7 +196,7 @@ class CppGenerator : public BaseGenerator {
...
@@ -196,7 +196,7 @@ class CppGenerator : public BaseGenerator {
}
}
assert
(
cur_name_space_
);
assert
(
cur_name_space_
);
Check
NameSpace
(
nullptr
,
&
code
);
Set
NameSpace
(
nullptr
,
&
code
);
// Close the include guard.
// Close the include guard.
code
+=
"#endif // "
+
include_guard
+
"
\n
"
;
code
+=
"#endif // "
+
include_guard
+
"
\n
"
;
...
@@ -862,23 +862,35 @@ class CppGenerator : public BaseGenerator {
...
@@ -862,23 +862,35 @@ class CppGenerator : public BaseGenerator {
}
}
// Set up the correct namespace. Only open a namespace if
// Set up the correct namespace. Only open a namespace if
// the existing one is different (opening/closing only what is necessary)
// the existing one is different (closing/opening only what is necessary) :
// the file must start and end with an empty namespace
//
void
CheckNameSpace
(
const
Namespace
*
ns
,
std
::
string
*
code_ptr
)
{
// the file must start and end with an empty (or null) namespace
// so that namespaces are properly opened and closed
void
SetNameSpace
(
const
Namespace
*
ns
,
std
::
string
*
code_ptr
)
{
if
(
cur_name_space_
==
ns
)
return
;
if
(
cur_name_space_
==
ns
)
return
;
auto
s1
=
cur_name_space_
==
nullptr
?
0
:
cur_name_space_
->
components
.
size
();
// compute the size of the longest common namespace prefix.
auto
s2
=
ns
==
nullptr
?
0
:
ns
->
components
.
size
();
// if cur_name_space is A::B::C::D and ns is A::B::E::F::G,
std
::
vector
<
std
::
string
>::
size_type
limit
=
0
;
// the common prefix is A::B:: and we have old_size = 4, new_size = 5
while
(
limit
<
s1
&&
limit
<
s2
&&
// and common_prefix_size = 2
ns
->
components
[
limit
]
==
cur_name_space_
->
components
[
limit
])
auto
old_size
=
limit
++
;
cur_name_space_
==
nullptr
?
0
:
cur_name_space_
->
components
.
size
();
for
(
auto
j
=
s1
;
j
>
limit
;
--
j
)
auto
new_size
=
ns
==
nullptr
?
0
:
ns
->
components
.
size
();
std
::
vector
<
std
::
string
>::
size_type
common_prefix_size
=
0
;
while
(
common_prefix_size
<
old_size
&&
common_prefix_size
<
new_size
&&
ns
->
components
[
common_prefix_size
]
==
cur_name_space_
->
components
[
common_prefix_size
])
common_prefix_size
++
;
// close cur_name_space in reverse order to reach the common prefix
// in the previous example, D then C are closed
for
(
auto
j
=
old_size
;
j
>
common_prefix_size
;
--
j
)
*
code_ptr
+=
*
code_ptr
+=
"} // namespace "
+
cur_name_space_
->
components
[
j
-
1
]
+
"
\n
"
;
"} // namespace "
+
cur_name_space_
->
components
[
j
-
1
]
+
"
\n
"
;
if
(
s1
!=
limit
)
*
code_ptr
+=
"
\n
"
;
if
(
old_size
!=
common_prefix_size
)
*
code_ptr
+=
"
\n
"
;
for
(
auto
j
=
limit
;
j
!=
s2
;
++
j
)
// open namespace parts to reach the ns namespace
// in the previous example, E, then F, then G are opened
for
(
auto
j
=
common_prefix_size
;
j
!=
new_size
;
++
j
)
*
code_ptr
+=
"namespace "
+
ns
->
components
[
j
]
+
" {
\n
"
;
*
code_ptr
+=
"namespace "
+
ns
->
components
[
j
]
+
" {
\n
"
;
if
(
s2
!=
limit
)
*
code_ptr
+=
"
\n
"
;
if
(
new_size
!=
common_prefix_size
)
*
code_ptr
+=
"
\n
"
;
cur_name_space_
=
ns
;
cur_name_space_
=
ns
;
}
}
};
};
...
...
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