add factor registerer

main
feixyz10 2021-03-01 17:36:57 +08:00 committed by feixyz
parent 191ba733b1
commit 8e70b81e19
6 changed files with 62 additions and 51 deletions

View File

@ -5,15 +5,65 @@
#include <string>
#include "common/log/log.h"
#include "common/macro/macros.h"
namespace common {
template <typename BaseClass>
class AbstractFactory {
public:
virtual ~AbstractFactory() = default;
virtual std::shared_ptr<BaseClass> Create() = 0;
virtual BaseClass *Create() = 0;
};
template <typename BaseClass, typename DerivedClass>
class ConcreteFactory {
virtual std::shared_ptr<BaseClass> Create() = 0;
};
class ConcreteFactory : public AbstractFactory<BaseClass> {
public:
BaseClass *Create() override {
return new DerivedClass;
}
};
template <typename BaseClass>
inline auto &GetFactoryMap() {
static std::map<std::string, std::shared_ptr<AbstractFactory<BaseClass>>> map;
return map;
}
template <typename BaseClass>
class Registerer {
public:
template <typename DerivedClass>
static void Register(const std::string &derived_class_name) {
static_assert(std::is_base_of<BaseClass, DerivedClass>::value, "");
auto &factory_map = GetFactoryMap<BaseClass>();
if (factory_map.find(derived_class_name) != factory_map.end()) {
return;
}
factory_map[derived_class_name].reset(
new ConcreteFactory<BaseClass, DerivedClass>);
}
static BaseClass *NewInstance(const std::string &derived_class_name) {
auto &factory_map = GetFactoryMap<BaseClass>();
auto iter = factory_map.find(derived_class_name);
if (iter == factory_map.end()) {
AFATAL << "Class not registered: " << derived_class_name << ".";
return nullptr;
}
return iter->second->Create();
}
static bool IsRegistered(const std::string &derived_class_name) {
auto &factory_map = GetFactoryMap<BaseClass>();
return factory_map.find(derived_class_name) != factory_map.end();
}
};
} // namespace common
#define REGISTER_CLASS(base_class, derived_class) \
namespace { \
__attribute__((constructor)) void Register##derived_class() { \
::common::Registerer<base_class>::Register<derived_class>(#derived_class); \
} \
} // namespace

42
ggg.txt
View File

@ -1,42 +0,0 @@
scan line number 16
line resolution 0.200000 plane resolution 0.400000
Mapping 10 Hz
... logging to /home/liufei/.ros/log/8dd723e0-69e0-11eb-931b-4cedfbbfbb36/roslaunch-liufei-fabu-4816.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.
]2;/home/liufei/catkin_ws/src/A-LOAM/launch/aloam_velodyne_VLP_16.launch
started roslaunch server http://liufei-fabu:38301/
SUMMARY
========
PARAMETERS
* /mapping_line_resolution: 0.2
* /mapping_plane_resolution: 0.4
* /mapping_skip_frame: 1
* /minimum_range: 0.3
* /rosdistro: kinetic
* /rosversion: 1.12.16
* /scan_line: 16
NODES
/
alaserMapping (aloam_velodyne/alaserMapping)
alaserOdometry (aloam_velodyne/alaserOdometry)
ascanRegistration (aloam_velodyne/ascanRegistration)
rviz (rviz/rviz)
ROS_MASTER_URI=http://localhost:11311
]2;/home/liufei/catkin_ws/src/A-LOAM/launch/aloam_velodyne_VLP_16.launch http://localhost:11311
process[ascanRegistration-1]: started with pid [4854]
process[alaserOdometry-2]: started with pid [4855]
process[alaserMapping-3]: started with pid [4856]
process[rviz-4]: started with pid [4857]
[rviz-4] killing on exit
[alaserMapping-3] killing on exit
[alaserOdometry-2] killing on exit
[ascanRegistration-1] killing on exit
shutting down processing monitor...
... shutting down processing monitor complete
done

View File

@ -1,5 +1,5 @@
# global configs
lidar: VPL16
lidar: VLP16
log_to_file: false
log_path: /data/log/oh_my_loam
vis: true

View File

@ -1,6 +1,7 @@
#include "extractor_VLP16.h"
#include "common/math/math_utils.h"
#include "common/registerer/registerer.h"
namespace oh_my_loam {
@ -10,4 +11,6 @@ int ExtractorVLP16::GetScanID(const common::Point &pt) const {
return static_cast<int>(std::round(theta / 2.0) + 1.e-5);
};
REGISTER_CLASS(Extractor, ExtractorVLP16)
} // namespace oh_my_loam

View File

@ -2,9 +2,8 @@
#include <vector>
#include "common/common.h"
#include "common/pcl/pcl_utils.h"
#include "oh_my_loam/extractor/extractor_VLP16.h"
#include "common/registerer/registerer.h"
namespace oh_my_loam {
@ -15,7 +14,8 @@ const double kPointMinDist = 0.5;
bool OhMyLoam::Init() {
config_ = common::YAMLConfig::Instance()->config();
is_vis_ = config_["vis"].as<bool>();
extractor_.reset(new ExtractorVLP16);
extractor_.reset(common::Registerer<Extractor>::NewInstance(
"Extractor" + config_["lidar"].as<std::string>()));
if (!extractor_->Init()) {
AERROR << "Failed to initialize extractor";
return false;