mirror of
https://gitee.com/xiaohuolufeihua/bizhang_-obav.git
synced 2026-05-22 01:12:31 +00:00
Clean up of app.h
app.h, generated from app.h_in, has unnecessary code duplication and isn't a header file (it defines globals, static functions and doesn't have a header guard, moreover, it has a 'using namespace std;'). Because of this, a real headerfile that declares the stuff defined in apps.h was missing leading to even more code duplication: scattered forward declarations in .cpp files and an often repeated type of std::map<std::string, px4_main_t>. This patch moves cmake/qurt/apps.h_in to src/platforms/apps.cpp.in (with some changes) and removes cmake/posix/apps.h_in. Then src/platforms/apps.cpp.in is split into src/platforms/apps.cpp.in and src/platforms/apps.h.in, splitting declarations from definitions. A typedef is defined for the map (apps_map_type). The main difference between cmake/posix/apps.h_in and cmake/qurt/apps.h_in was that the first defined a global 'apps', while qurt stores the apps in QShell. I opted to get rid of the global variable (which are in general evil for various reasons) and used the API of cmake/qurt/apps.h_in where a provided 'apps' map is initialized with a call to init_app_map. Thus removing the existing code duplication.
This commit is contained in:
@@ -1,88 +0,0 @@
|
||||
/* builtin command list - automatically generated, do not edit */
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <px4_tasks.h>
|
||||
#include <px4_posix.h>
|
||||
#include <px4_log.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
extern void px4_show_devices(void);
|
||||
|
||||
extern "C" {
|
||||
${builtin_apps_decl_string}
|
||||
static int shutdown_main(int argc, char *argv[]);
|
||||
static int list_tasks_main(int argc, char *argv[]);
|
||||
static int list_files_main(int argc, char *argv[]);
|
||||
static int list_devices_main(int argc, char *argv[]);
|
||||
static int list_topics_main(int argc, char *argv[]);
|
||||
static int sleep_main(int argc, char *argv[]);
|
||||
}
|
||||
|
||||
static map<string,px4_main_t> app_map(void)
|
||||
{
|
||||
static map<string,px4_main_t> apps;
|
||||
|
||||
${builtin_apps_string}
|
||||
apps["shutdown"] = shutdown_main;
|
||||
apps["list_tasks"] = list_tasks_main;
|
||||
apps["list_files"] = list_files_main;
|
||||
apps["list_devices"] = list_devices_main;
|
||||
apps["list_topics"] = list_topics_main;
|
||||
apps["sleep"] = sleep_main;
|
||||
|
||||
return apps;
|
||||
}
|
||||
|
||||
map<string,px4_main_t> apps = app_map();
|
||||
|
||||
static void list_builtins(void)
|
||||
{
|
||||
cout << "Builtin Commands:" << endl;
|
||||
for (map<string,px4_main_t>::iterator it=apps.begin(); it!=apps.end(); ++it)
|
||||
cout << '\t' << it->first << endl;
|
||||
}
|
||||
|
||||
static int shutdown_main(int argc, char *argv[])
|
||||
{
|
||||
printf("Shutting down\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static int list_tasks_main(int argc, char *argv[])
|
||||
{
|
||||
px4_show_tasks();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int list_devices_main(int argc, char *argv[])
|
||||
{
|
||||
px4_show_devices();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int list_topics_main(int argc, char *argv[])
|
||||
{
|
||||
px4_show_topics();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int list_files_main(int argc, char *argv[])
|
||||
{
|
||||
px4_show_files();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sleep_main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2) {
|
||||
cout << "Usage: sleep <seconds>" << endl;
|
||||
return 1;
|
||||
}
|
||||
sleep(atoi(argv[1]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/cmake/posix)
|
||||
# MODULE_LIST : list of modules
|
||||
#
|
||||
# Output:
|
||||
# OUT : generated builtin_commands.c src
|
||||
# OUT : stem of generated apps.cpp/apps.h ("apps")
|
||||
#
|
||||
# Example:
|
||||
# px4_posix_generate_builtin_commands(
|
||||
@@ -97,12 +97,14 @@ function(px4_posix_generate_builtin_commands)
|
||||
set(builtin_apps_string
|
||||
"${builtin_apps_string}\tapps[\"${MAIN}\"] = ${MAIN}_main;\n")
|
||||
set(builtin_apps_decl_string
|
||||
"${builtin_apps_decl_string}extern int ${MAIN}_main(int argc, char *argv[]);\n")
|
||||
"${builtin_apps_decl_string}int ${MAIN}_main(int argc, char *argv[]);\n")
|
||||
math(EXPR command_count "${command_count}+1")
|
||||
endif()
|
||||
endforeach()
|
||||
configure_file(${PX4_SOURCE_DIR}/cmake/posix/apps.h_in
|
||||
${OUT})
|
||||
configure_file(${PX4_SOURCE_DIR}/src/platforms/apps.cpp.in
|
||||
${OUT}.cpp)
|
||||
configure_file(${PX4_SOURCE_DIR}/src/platforms/apps.h.in
|
||||
${OUT}.h)
|
||||
endfunction()
|
||||
|
||||
#=============================================================================
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
/* builtin command list - automatically generated, do not edit */
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <px4_tasks.h>
|
||||
#include <px4_posix.h>
|
||||
#include <px4_log.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
extern void px4_show_devices(void);
|
||||
|
||||
extern "C" {
|
||||
${builtin_apps_decl_string}
|
||||
static int shutdown_main(int argc, char *argv[]);
|
||||
static int list_tasks_main(int argc, char *argv[]);
|
||||
static int list_files_main(int argc, char *argv[]);
|
||||
static int list_devices_main(int argc, char *argv[]);
|
||||
static int list_topics_main(int argc, char *argv[]);
|
||||
static int sleep_main(int argc, char *argv[]);
|
||||
}
|
||||
|
||||
|
||||
void init_app_map(map<string,px4_main_t> &apps)
|
||||
{
|
||||
${builtin_apps_string}
|
||||
apps["shutdown"] = shutdown_main;
|
||||
apps["list_tasks"] = list_tasks_main;
|
||||
apps["list_files"] = list_files_main;
|
||||
apps["list_devices"] = list_devices_main;
|
||||
apps["list_topics"] = list_topics_main;
|
||||
apps["sleep"] = sleep_main;
|
||||
}
|
||||
|
||||
void list_builtins(map<string,px4_main_t> &apps)
|
||||
{
|
||||
PX4_INFO("Builtin Commands:\\n");
|
||||
for (map<string,px4_main_t>::iterator it=apps.begin(); it!=apps.end(); ++it)
|
||||
PX4_INFO("%s : 0x%x\n", (it->first).c_str(), it->second);
|
||||
}
|
||||
|
||||
static int shutdown_main(int argc, char *argv[])
|
||||
{
|
||||
printf("Shutting down\\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static int list_tasks_main(int argc, char *argv[])
|
||||
{
|
||||
px4_show_tasks();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int list_devices_main(int argc, char *argv[])
|
||||
{
|
||||
px4_show_devices();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int list_topics_main(int argc, char *argv[])
|
||||
{
|
||||
px4_show_topics();
|
||||
return 0;
|
||||
}
|
||||
static int list_files_main(int argc, char *argv[])
|
||||
{
|
||||
px4_show_files();
|
||||
return 0;
|
||||
}
|
||||
static int sleep_main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2) {
|
||||
PX4_WARN( "Usage: sleep <seconds>" );
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned long usecs = ( (unsigned long) atol( argv[1] ) ) * 1000 * 1000;
|
||||
PX4_WARN("Sleeping for %s, %ld",argv[1],usecs);
|
||||
usleep( usecs );
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/cmake/qurt)
|
||||
# MODULE_LIST : list of modules
|
||||
#
|
||||
# Output:
|
||||
# OUT : generated builtin_commands.c src
|
||||
# OUT : stem of generated apps.cpp/apps.h ("apps").
|
||||
#
|
||||
# Example:
|
||||
# px4_qurt_generate_builtin_commands(
|
||||
@@ -91,11 +91,14 @@ function(px4_qurt_generate_builtin_commands)
|
||||
set(builtin_apps_string
|
||||
"${builtin_apps_string}\tapps[\"${MAIN}\"] = ${MAIN}_main;\n")
|
||||
set(builtin_apps_decl_string
|
||||
"${builtin_apps_decl_string}extern int ${MAIN}_main(int argc, char *argv[]);\n")
|
||||
"${builtin_apps_decl_string}int ${MAIN}_main(int argc, char *argv[]);\n")
|
||||
math(EXPR command_count "${command_count}+1")
|
||||
endif()
|
||||
endforeach()
|
||||
configure_file(${PX4_SOURCE_DIR}/cmake/qurt/apps.h_in ${OUT})
|
||||
configure_file(${PX4_SOURCE_DIR}/src/platforms/apps.cpp.in
|
||||
${OUT}.cpp)
|
||||
configure_file(${PX4_SOURCE_DIR}/src/platforms/apps.h.in
|
||||
${OUT}.h)
|
||||
endfunction()
|
||||
|
||||
#=============================================================================
|
||||
|
||||
Reference in New Issue
Block a user