我想知道是否有一种方法可以使对象的寿命超过其作用域的寿命,并且仍然可以使用,例如下面所示的示例。从广义上讲,我试图根据输入参数使用不同类型的对象。如果if-else
语句与对calc_force
的调用处于同一范围内,则可以轻松解决此问题,但这无疑会阻碍程序的性能。
我研究了使用std::function
和std::shared_ptr
的一些可能的解决方案,但无济于事。我不确定是否使用MD_tools::GCM_force
和MD_tools::GCM_force
的类包装器以正确的心态来解决这个问题。任何帮助将不胜感激。
std::tuple<double, double> GCM_pp::get_force(double &r, double m, double C) {
auto [ff, u] = MD_tools::GCM_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
std::tuple<double, double> Exp_pp::get_force(double &r, double m, double C) {
auto [ff, u] = MD_tools::Exp_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
void simulate(std::string pp_type, size_t step, double POWER, double A_CST{
// Normally the if-else loop contains a couple dozen if statements
if (pp_type == "GCM") GCM_pp potential;
else Exp_pp potential;
for (step = 0; step < step_max; ++step) { // typical values: step ~1,000,000
size_t i, j;
for (i = 0; i < N - 1; ++i) { // typical values: N ~10,000
for (j = i + 1; j < N; ++j) {
// stuff happening ...
double r = sqrt((x * x) + (y * y) + (z * z));
auto [ff, temp_u] = potential.calc_force(r, POWER, A_CST);
// stuff happening ...
}
}
}
}
答案 0 :(得分:4)
这是多态和类工厂可用于的事情,例如:
class Base_pp
{
public:
virtual ~Base_pp() {}
virtual std::tuple<double, double> get_force(double &r, double m, double C) = 0;
};
class GCM_pp : public Base_pp
{
public:
std::tuple<double, double> get_force(double &r, double m, double C) override {
auto [ff, u] = MD_tools::GCM_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
class Exp_pp : public Base_pp
{
public:
std::tuple<double, double> get_force(double &r, double m, double C) override {
auto [ff, u] = MD_tools::Exp_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
...
using funcType = std::unique_ptr<Base_pp> (*)();
std::map<std::string, funcType> make_funcs = {
{"GCM", []() -> std::unique_ptr<Base_pp> { return std::make_unique<GCM_pp>(); }},
...
{"Exp", []() -> std::unique_ptr<Base_pp> { return std::make_unique<Exp_pp>(); }}
};
std::unique_ptr<Base_pp> make_pp(std::string pp_type) {
funcType func = make_funcs[pp_type];
if (!func) func = make_funcs["Exp"];
return func();
}
...
void simulate(std::string pp_type, size_t step, double POWER, double A_CST) {
std::unique_ptr<Base_pp> potential = make_pp(pp_type);
for (step = 0; step < step_max; ++step) { // typical values: step ~1,000,000
size_t i, j;
for (i = 0; i < N - 1; ++i) { // typical values: N ~10,000
for (j = i + 1; j < N; ++j) {
// stuff happening ...
double r = sqrt((x * x) + (y * y) + (z * z));
auto [ff, temp_u] = potential->get_force(r, POWER, A_CST);
// stuff happening ...
}
}
}
}
或者,您在示例中实际上不需要多态。由于您的课程不使用实例数据,因此您可以尝试以下类似方法:
class GCM_pp
{
public:
static std::tuple<double, double> get_force(double &r, double m, double C) {
auto [ff, u] = MD_tools::GCM_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
class Exp_pp
{
public:
static std::tuple<double, double> get_force(double &r, double m, double C) {
auto [ff, u] = MD_tools::Exp_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
...
using funcType = std::tuple<double, double> (*)(double&, double, double);
std::map<std::string, funcType> get_force_funcs = {
{"GCM", &GCM_pp::get_force},
...
{"Exp", &Exp_pp::get_force}
};
funcType get_force_func(std::string pp_type) {
funcType func = get_force_funcs[pp_type];
if (!func) func = get_force_funcs["Exp"];
return func;
}
...
void simulate(std::string pp_type, size_t step, double POWER, double A_CST) {
funcType get_force = get_force_func(pp_type);
for (step = 0; step < step_max; ++step) { // typical values: step ~1,000,000
size_t i, j;
for (i = 0; i < N - 1; ++i) { // typical values: N ~10,000
for (j = i + 1; j < N; ++j) {
// stuff happening ...
double r = sqrt((x * x) + (y * y) + (z * z));
auto [ff, temp_u] = get_force(r, POWER, A_CST);
// stuff happening ...
}
}
}
}
或者这个:
class GCM_pp
{
public:
static std::tuple<double, double> get_force(double &r, double m, double C) {
auto [ff, u] = MD_tools::GCM_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
class Exp_pp
{
public:
static std::tuple<double, double> get_force(double &r, double m, double C) {
auto [ff, u] = MD_tools::Exp_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
...
template<typename T>
void do_simulate(size_t step, double POWER, double A_CST) {
for (step = 0; step < step_max; ++step) { // typical values: step ~1,000,000
size_t i, j;
for (i = 0; i < N - 1; ++i) { // typical values: N ~10,000
for (j = i + 1; j < N; ++j) {
// stuff happening ...
double r = sqrt((x * x) + (y * y) + (z * z));
auto [ff, temp_u] = T::get_force(r, POWER, A_CST);
// stuff happening ...
}
}
}
}
using funcType = void (*)(size_t, double, double);
std::map<std::string, funcType> simulate_funcs = {
{"GCM", &do_simulate<GCM_pp>},
...
{"Exp", &do_simulate<Exp_pp>}
};
funcType get_simulate_func(std::string pp_type) {
funcType func = simulate_funcs[pp_type];
if (!func) func = simulate_funcs["Exp"];
return func;
}
...
void simulate(std::string pp_type, size_t step, double POWER, double A_CST) {
funcType simulate_func = get_simulate_func(pp_type);
simulate_func(step, POWER, A_CST);
}