单例模式

class CostFunctionFactory
{
public:
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
    CostFunctionFactory();

    static boost::shared_ptr<CostFunctionFactory> instance(void);

    ceres::CostFunction* generateCostFunction(const CameraConstPtr& camera,
                                              const Eigen::Vector3d& observed_P,
                                              const Eigen::Vector2d& observed_p,
                                              int flags) const;

    ceres::CostFunction* generateCostFunction(const CameraConstPtr& camera,
                                              const Eigen::Vector3d& observed_P,
                                              const Eigen::Vector2d& observed_p,
                                              const Eigen::Matrix2d& sqrtPrecisionMat,
                                              int flags) const;

    ceres::CostFunction* generateCostFunction(const CameraConstPtr& camera,
                                              const Eigen::Vector2d& observed_p,
                                              int flags, bool optimize_cam_odo_z = true) const;

    ceres::CostFunction* generateCostFunction(const CameraConstPtr& camera,
                                              const Eigen::Vector2d& observed_p,
                                              const Eigen::Matrix2d& sqrtPrecisionMat,
                                              int flags, bool optimize_cam_odo_z = true) const;

    ceres::CostFunction* generateCostFunction(const CameraConstPtr& camera,
                                              const Eigen::Vector3d& odo_pos,
                                              const Eigen::Vector3d& odo_att,
                                              const Eigen::Vector2d& observed_p,
                                              int flags, bool optimize_cam_odo_z = true) const;

    ceres::CostFunction* generateCostFunction(const CameraConstPtr& camera,
                                              const Eigen::Quaterniond& cam_odo_q,
                                              const Eigen::Vector3d& cam_odo_t,
                                              const Eigen::Vector3d& odo_pos,
                                              const Eigen::Vector3d& odo_att,
                                              const Eigen::Vector2d& observed_p,
                                              int flags) const;

    ceres::CostFunction* generateCostFunction(const CameraConstPtr& cameraLeft,
                                              const CameraConstPtr& cameraRight,
                                              const Eigen::Vector3d& observed_P,
                                              const Eigen::Vector2d& observed_p_left,
                                              const Eigen::Vector2d& observed_p_right) const;

private:
    static boost::shared_ptr<CostFunctionFactory> m_instance;
};


这段代码中,涉及到单例模式

chatgpt的回答如下

单例模式是一种设计模式,用于确保一个类只有一个实例,并提供全局访问该实例的方式。要实现一个单例模式,需要满足以下条件:

私有的构造函数:确保其他类不能直接实例化该类的对象。将构造函数声明为私有的,防止外部代码通过new操作符创建类的实例。

静态成员变量:在类的内部定义一个静态成员变量,用于保存类的唯一实例。

静态成员函数:提供一个公共的静态成员函数,用于获取类的唯一实例。这个函数通常被命名为instance()或getInstance()。

延迟实例化:在静态成员函数中,检查类的唯一实例是否已经存在。如果存在,直接返回该实例;如果不存在,创建一个新的实例并返回。

线程安全性:如果在多线程环境下使用单例模式,需要考虑线程安全性。可以使用互斥锁(mutex)或双重检查锁定(double-checked locking)等机制来确保线程安全性。

在给定的代码中,CostFunctionFactory类实现了单例模式。它满足上述条件:私有的构造函数、静态成员变量m_instance用于保存实例、静态成员函数instance()用于获取实例,以及延迟实例化的逻辑。这样,通过CostFunctionFactory::instance()方法可以获取全局唯一的CostFunctionFactory实例。

posted @ 2024-03-07 10:27  水水滴答  阅读(1)  评论(0编辑  收藏  举报