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
61b101d4
Commit
61b101d4
authored
May 28, 2016
by
lakedaemon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sharing namespace_dir and the namespace string methods
parent
cd1493b0
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
67 additions
and
102 deletions
+67
-102
code_generators.h
include/flatbuffers/code_generators.h
+39
-7
idl_gen_general.cpp
src/idl_gen_general.cpp
+21
-55
idl_gen_go.cpp
src/idl_gen_go.cpp
+2
-14
idl_gen_php.cpp
src/idl_gen_php.cpp
+2
-16
idl_gen_python.cpp
src/idl_gen_python.cpp
+3
-10
No files found.
include/flatbuffers/code_generators.h
View file @
61b101d4
...
@@ -21,19 +21,35 @@ namespace flatbuffers {
...
@@ -21,19 +21,35 @@ namespace flatbuffers {
class
BaseGenerator
{
class
BaseGenerator
{
public
:
public
:
BaseGenerator
(
const
Parser
&
parser
,
const
std
::
string
&
path
,
const
std
::
string
&
file_name
)
:
parser_
(
parser
),
path_
(
path
),
file_name_
(
file_name
){};
virtual
bool
generate
()
=
0
;
virtual
bool
generate
()
=
0
;
static
const
std
::
string
NamespaceDir
(
const
Parser
&
parser
,
const
std
::
string
&
path
)
{
EnsureDirExists
(
path
.
c_str
());
if
(
parser
.
opts
.
one_file
)
return
path
;
std
::
string
namespace_dir
=
path
;
// Either empty or ends in separator.
auto
&
namespaces
=
parser
.
namespaces_
.
back
()
->
components
;
for
(
auto
it
=
namespaces
.
begin
();
it
!=
namespaces
.
end
();
++
it
)
{
namespace_dir
+=
*
it
+
kPathSeparator
;
EnsureDirExists
(
namespace_dir
.
c_str
());
}
return
namespace_dir
;
}
protected
:
protected
:
BaseGenerator
(
const
Parser
&
parser
,
const
std
::
string
&
path
,
const
std
::
string
&
file_name
)
:
parser_
(
parser
),
path_
(
path
),
file_name_
(
file_name
),
namespace_dir_
(
BaseGenerator
::
NamespaceDir
(
parser
,
path
)){};
virtual
~
BaseGenerator
(){};
virtual
~
BaseGenerator
(){};
const
char
*
FlatBuffersGeneratedWarning
()
{
const
char
*
FlatBuffersGeneratedWarning
()
{
return
"automatically generated by the FlatBuffers compiler,"
return
"automatically generated by the FlatBuffers compiler,"
" do not modify
\n\n
"
;
" do not modify
\n\n
"
;
}
}
bool
IsEverythingGenerated
()
{
bool
IsEverythingGenerated
()
{
for
(
auto
it
=
parser_
.
enums_
.
vec
.
begin
();
it
!=
parser_
.
enums_
.
vec
.
end
();
for
(
auto
it
=
parser_
.
enums_
.
vec
.
begin
();
it
!=
parser_
.
enums_
.
vec
.
end
();
++
it
)
{
++
it
)
{
...
@@ -46,9 +62,25 @@ class BaseGenerator {
...
@@ -46,9 +62,25 @@ class BaseGenerator {
return
true
;
return
true
;
}
}
std
::
string
FullNamespace
(
const
char
*
separator
)
{
std
::
string
namespace_name
;
auto
&
namespaces
=
parser_
.
namespaces_
.
back
()
->
components
;
for
(
auto
it
=
namespaces
.
begin
();
it
!=
namespaces
.
end
();
++
it
)
{
if
(
namespace_name
.
length
())
namespace_name
+=
separator
;
namespace_name
+=
*
it
;
}
return
namespace_name
;
}
const
std
::
string
LastNamespacePart
()
{
auto
&
namespaces
=
parser_
.
namespaces_
.
back
()
->
components
;
if
(
namespaces
.
size
())
return
*
(
namespaces
.
end
()
-
1
);
else
return
std
::
string
(
""
);
}
const
Parser
&
parser_
;
const
Parser
&
parser_
;
const
std
::
string
&
path_
;
const
std
::
string
&
path_
;
const
std
::
string
&
file_name_
;
const
std
::
string
&
file_name_
;
const
std
::
string
namespace_dir_
;
};
};
}
// namespace flatbuffers
}
// namespace flatbuffers
...
...
src/idl_gen_general.cpp
View file @
61b101d4
...
@@ -1133,7 +1133,7 @@ class GeneralGenerator : public BaseGenerator {
...
@@ -1133,7 +1133,7 @@ class GeneralGenerator : public BaseGenerator {
if
(
parser_
.
opts
.
one_file
)
{
if
(
parser_
.
opts
.
one_file
)
{
one_file_code
+=
enumcode
;
one_file_code
+=
enumcode
;
}
else
{
}
else
{
if
(
!
SaveType
(
lang
,
(
**
it
).
name
,
enumcode
,
false
,
false
))
return
false
;
if
(
!
SaveType
(
lang
,
(
**
it
).
name
,
enumcode
,
false
))
return
false
;
}
}
}
}
...
@@ -1144,12 +1144,12 @@ class GeneralGenerator : public BaseGenerator {
...
@@ -1144,12 +1144,12 @@ class GeneralGenerator : public BaseGenerator {
if
(
parser_
.
opts
.
one_file
)
{
if
(
parser_
.
opts
.
one_file
)
{
one_file_code
+=
declcode
;
one_file_code
+=
declcode
;
}
else
{
}
else
{
if
(
!
SaveType
(
lang
,
(
**
it
).
name
,
declcode
,
true
,
false
))
return
false
;
if
(
!
SaveType
(
lang
,
(
**
it
).
name
,
declcode
,
true
))
return
false
;
}
}
}
}
if
(
parser_
.
opts
.
one_file
)
{
if
(
parser_
.
opts
.
one_file
)
{
return
SaveType
(
lang
,
file_name_
,
one_file_code
,
true
,
true
);
return
SaveType
(
lang
,
file_name_
,
one_file_code
,
true
);
}
}
return
true
;
return
true
;
}
}
...
@@ -1157,34 +1157,20 @@ class GeneralGenerator : public BaseGenerator {
...
@@ -1157,34 +1157,20 @@ class GeneralGenerator : public BaseGenerator {
// Save out the generated code for a single class while adding
// Save out the generated code for a single class while adding
// declaration boilerplate.
// declaration boilerplate.
bool
SaveType
(
const
LanguageParameters
&
lang
,
const
std
::
string
&
defname
,
bool
SaveType
(
const
LanguageParameters
&
lang
,
const
std
::
string
&
defname
,
const
std
::
string
&
classcode
,
bool
needs_includes
,
const
std
::
string
&
classcode
,
bool
needs_includes
)
{
bool
onefile
)
{
if
(
!
classcode
.
length
())
return
true
;
if
(
!
classcode
.
length
())
return
true
;
std
::
string
namespace_general
;
std
::
string
namespace_dir
=
path_
;
// Either empty or ends in separator.
auto
&
namespaces
=
parser_
.
namespaces_
.
back
()
->
components
;
for
(
auto
it
=
namespaces
.
begin
();
it
!=
namespaces
.
end
();
++
it
)
{
if
(
namespace_general
.
length
())
{
namespace_general
+=
"."
;
}
namespace_general
+=
*
it
;
if
(
!
onefile
)
{
namespace_dir
+=
*
it
+
kPathSeparator
;
}
}
EnsureDirExists
(
namespace_dir
);
std
::
string
code
;
std
::
string
code
;
code
=
code
+
"// "
+
FlatBuffersGeneratedWarning
();
code
=
code
+
"// "
+
FlatBuffersGeneratedWarning
();
if
(
!
namespace_general
.
empty
())
{
std
::
string
namespace_name
=
FullNamespace
(
"."
);
code
+=
lang
.
namespace_ident
+
namespace_general
+
lang
.
namespace_begin
;
if
(
!
namespace_name
.
empty
())
{
code
+=
lang
.
namespace_ident
+
namespace_name
+
lang
.
namespace_begin
;
code
+=
"
\n\n
"
;
code
+=
"
\n\n
"
;
}
}
if
(
needs_includes
)
code
+=
lang
.
includes
;
if
(
needs_includes
)
code
+=
lang
.
includes
;
code
+=
classcode
;
code
+=
classcode
;
if
(
!
namespace_
general
.
empty
())
code
+=
lang
.
namespace_end
;
if
(
!
namespace_
name
.
empty
())
code
+=
lang
.
namespace_end
;
auto
filename
=
namespace_dir
+
defname
+
lang
.
file_extension
;
auto
filename
=
namespace_dir
_
+
defname
+
lang
.
file_extension
;
return
SaveFile
(
filename
.
c_str
(),
code
,
false
);
return
SaveFile
(
filename
.
c_str
(),
code
,
false
);
}
}
};
};
...
@@ -1196,50 +1182,30 @@ bool GenerateGeneral(const Parser &parser, const std::string &path,
...
@@ -1196,50 +1182,30 @@ bool GenerateGeneral(const Parser &parser, const std::string &path,
return
generator
.
generate
();
return
generator
.
generate
();
}
}
static
std
::
string
ClassFileName
(
const
LanguageParameters
&
lang
,
std
::
string
GeneralMakeRule
(
const
Parser
&
parser
,
const
std
::
string
&
path
,
const
Parser
&
parser
,
const
Definition
&
def
,
const
std
::
string
&
path
)
{
std
::
string
namespace_general
;
std
::
string
namespace_dir
=
path
;
auto
&
namespaces
=
parser
.
namespaces_
.
back
()
->
components
;
for
(
auto
it
=
namespaces
.
begin
();
it
!=
namespaces
.
end
();
++
it
)
{
if
(
namespace_general
.
length
())
{
namespace_general
+=
"."
;
namespace_dir
+=
kPathSeparator
;
}
namespace_general
+=
*
it
;
namespace_dir
+=
*
it
;
}
return
namespace_dir
+
kPathSeparator
+
def
.
name
+
lang
.
file_extension
;
}
std
::
string
GeneralMakeRule
(
const
Parser
&
parser
,
const
std
::
string
&
path
,
const
std
::
string
&
file_name
)
{
const
std
::
string
&
file_name
)
{
assert
(
parser
.
opts
.
lang
<=
IDLOptions
::
kMAX
);
assert
(
parser
.
opts
.
lang
<=
IDLOptions
::
kMAX
);
auto
lang
=
language_parameters
[
parser
.
opts
.
lang
];
auto
lang
=
language_parameters
[
parser
.
opts
.
lang
];
std
::
string
make_rule
;
std
::
string
make_rule
;
std
::
string
directory
=
BaseGenerator
::
NamespaceDir
(
parser
,
path
)
+
kPathSeparator
;
for
(
auto
it
=
parser
.
enums_
.
vec
.
begin
();
for
(
auto
it
=
parser
.
enums_
.
vec
.
begin
();
it
!=
parser
.
enums_
.
vec
.
end
();
it
!=
parser
.
enums_
.
vec
.
end
();
++
it
)
{
++
it
)
{
if
(
make_rule
!=
""
)
if
(
make_rule
!=
""
)
make_rule
+=
" "
;
make_rule
+=
" "
;
make_rule
+=
directory
+
(
**
it
).
name
+
lang
.
file_extension
;
make_rule
+=
ClassFileName
(
lang
,
parser
,
**
it
,
path
);
}
}
for
(
auto
it
=
parser
.
structs_
.
vec
.
begin
();
for
(
auto
it
=
parser
.
structs_
.
vec
.
begin
();
it
!=
parser
.
structs_
.
vec
.
end
();
it
!=
parser
.
structs_
.
vec
.
end
();
++
it
)
{
++
it
)
{
if
(
make_rule
!=
""
)
if
(
make_rule
!=
""
)
make_rule
+=
" "
;
make_rule
+=
" "
;
make_rule
+=
directory
+
(
**
it
).
name
+
lang
.
file_extension
;
make_rule
+=
ClassFileName
(
lang
,
parser
,
**
it
,
path
);
}
}
make_rule
+=
": "
;
make_rule
+=
": "
;
auto
included_files
=
parser
.
GetIncludedFilesRecursive
(
file_name
);
auto
included_files
=
parser
.
GetIncludedFilesRecursive
(
file_name
);
for
(
auto
it
=
included_files
.
begin
();
for
(
auto
it
=
included_files
.
begin
();
it
!=
included_files
.
end
();
++
it
)
{
it
!=
included_files
.
end
();
++
it
)
{
make_rule
+=
" "
+
*
it
;
make_rule
+=
" "
+
*
it
;
}
}
return
make_rule
;
return
make_rule
;
...
...
src/idl_gen_go.cpp
View file @
61b101d4
...
@@ -663,22 +663,10 @@ class GoGenerator : public BaseGenerator {
...
@@ -663,22 +663,10 @@ class GoGenerator : public BaseGenerator {
bool
needs_imports
)
{
bool
needs_imports
)
{
if
(
!
classcode
.
length
())
return
true
;
if
(
!
classcode
.
length
())
return
true
;
std
::
string
namespace_name
;
std
::
string
namespace_dir
=
path_
;
// Either empty or ends in separator.
auto
&
namespaces
=
parser_
.
namespaces_
.
back
()
->
components
;
for
(
auto
it
=
namespaces
.
begin
();
it
!=
namespaces
.
end
();
++
it
)
{
if
(
namespace_name
.
length
())
{
namespace_name
+=
"."
;
}
namespace_name
=
*
it
;
namespace_dir
+=
*
it
+
kPathSeparator
;
}
EnsureDirExists
(
namespace_dir
);
std
::
string
code
=
""
;
std
::
string
code
=
""
;
BeginFile
(
namespace_name
,
needs_imports
,
&
code
);
BeginFile
(
LastNamespacePart
()
,
needs_imports
,
&
code
);
code
+=
classcode
;
code
+=
classcode
;
std
::
string
filename
=
namespace_dir
+
def
.
name
+
".go"
;
std
::
string
filename
=
namespace_dir
_
+
def
.
name
+
".go"
;
return
SaveFile
(
filename
.
c_str
(),
code
,
false
);
return
SaveFile
(
filename
.
c_str
(),
code
,
false
);
}
}
};
};
...
...
src/idl_gen_php.cpp
View file @
61b101d4
...
@@ -984,26 +984,12 @@ namespace php {
...
@@ -984,26 +984,12 @@ namespace php {
bool
needs_imports
)
{
bool
needs_imports
)
{
if
(
!
classcode
.
length
())
return
true
;
if
(
!
classcode
.
length
())
return
true
;
std
::
string
namespace_name
;
std
::
string
namespace_dir
=
path_
;
auto
&
namespaces
=
parser_
.
namespaces_
.
back
()
->
components
;
for
(
auto
it
=
namespaces
.
begin
();
it
!=
namespaces
.
end
();
++
it
)
{
if
(
namespace_name
.
length
())
{
namespace_name
+=
"
\\
"
;
namespace_dir
+=
kPathSeparator
;
}
namespace_name
+=
*
it
;
namespace_dir
+=
*
it
;
EnsureDirExists
(
namespace_dir
.
c_str
());
}
std
::
string
code
=
""
;
std
::
string
code
=
""
;
BeginFile
(
namespace_name
,
needs_imports
,
&
code
);
BeginFile
(
FullNamespace
(
"
\\
"
)
,
needs_imports
,
&
code
);
code
+=
classcode
;
code
+=
classcode
;
std
::
string
filename
=
std
::
string
filename
=
namespace_dir
+
kPathSeparator
+
def
.
name
+
".php"
;
namespace_dir
_
+
kPathSeparator
+
def
.
name
+
".php"
;
return
SaveFile
(
filename
.
c_str
(),
code
,
false
);
return
SaveFile
(
filename
.
c_str
(),
code
,
false
);
}
}
};
};
...
...
src/idl_gen_python.cpp
View file @
61b101d4
...
@@ -642,26 +642,19 @@ class PythonGenerator : public BaseGenerator {
...
@@ -642,26 +642,19 @@ class PythonGenerator : public BaseGenerator {
bool
needs_imports
)
{
bool
needs_imports
)
{
if
(
!
classcode
.
length
())
return
true
;
if
(
!
classcode
.
length
())
return
true
;
std
::
string
namespace_name
;
std
::
string
namespace_dir
=
path_
;
std
::
string
namespace_dir
=
path_
;
auto
&
namespaces
=
parser_
.
namespaces_
.
back
()
->
components
;
auto
&
namespaces
=
parser_
.
namespaces_
.
back
()
->
components
;
for
(
auto
it
=
namespaces
.
begin
();
it
!=
namespaces
.
end
();
++
it
)
{
for
(
auto
it
=
namespaces
.
begin
();
it
!=
namespaces
.
end
();
++
it
)
{
if
(
namespace_name
.
length
())
{
if
(
it
!=
namespaces
.
begin
())
namespace_dir
+=
kPathSeparator
;
namespace_name
+=
"."
;
namespace_dir
+=
kPathSeparator
;
}
namespace_name
=
*
it
;
namespace_dir
+=
*
it
;
namespace_dir
+=
*
it
;
EnsureDirExists
(
namespace_dir
.
c_str
());
std
::
string
init_py_filename
=
namespace_dir
+
"/__init__.py"
;
std
::
string
init_py_filename
=
namespace_dir
+
"/__init__.py"
;
SaveFile
(
init_py_filename
.
c_str
(),
""
,
false
);
SaveFile
(
init_py_filename
.
c_str
(),
""
,
false
);
}
}
std
::
string
code
=
""
;
std
::
string
code
=
""
;
BeginFile
(
namespace_name
,
needs_imports
,
&
code
);
BeginFile
(
LastNamespacePart
()
,
needs_imports
,
&
code
);
code
+=
classcode
;
code
+=
classcode
;
std
::
string
filename
=
namespace_dir
+
kPathSeparator
+
def
.
name
+
".py"
;
std
::
string
filename
=
namespace_dir
_
+
kPathSeparator
+
def
.
name
+
".py"
;
return
SaveFile
(
filename
.
c_str
(),
code
,
false
);
return
SaveFile
(
filename
.
c_str
(),
code
,
false
);
}
}
};
};
...
...
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