分析行为基础文件 [英] Analysis the behavior base file
本文介绍了分析行为基础文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
各位大家好,有谁可以分析AutoRegister功能?我很好奇,如果你给我一个合适的答案,请提前感谢你。
Hello everybody, can anyone analysis the "AutoRegister" function? I curious about it and thank you in advance, if you give me a suitable answer.
#include <cstdio>
#include <cmath>
#include "Types.h"
int abort_with_SIGSEGV(const char *expr, const char *file, int line, const char *function)
{
fprintf(stderr, "%s in %s:%d aborted at %s\n", function, file, line, expr);
int *p = 0;
*p = 1; //segfault here
return *p;
}
#ifdef _Debug
#ifndef WIN32
#define Assert(expr) ((expr)? (0): abort_with_SIGSEGV (__STRING(expr), __FILE__, __LINE__, __PRETTY_FUNCTION__))
#else
#include <cassert>
#define Assert(expr) assert(expr)
#endif
#else
#define Assert(expr)
#endif
class BehaviorExecutable {
BehaviorExecutable(const BehaviorExecutable&);
public:
BehaviorExecutable() {}
virtual ~BehaviorExecutable() {}
virtual bool Execute(const ActiveBehavior & act_bhv) = 0; //每周期只有一个行为被执行
virtual void SubmitVisualRequest(const ActiveBehavior & act_bhv, double plus = 0.0) {
(void) act_bhv;
(void) plus;
}
/// behavior factory interfaces
template <class BehaviorDerived >
static BehaviorExecutable* Creator(Agent & agent) { return new BehaviorDerived(agent); }
template<class BehaviorDerived>
static bool AutoRegister() {
std::string behavior_name;
#ifndef WIN32
std::string function_name = __PRETTY_FUNCTION__;
size_t end = function_name.find("Executer");
if (end != std::string::npos) {
size_t begin = end;
while (begin && isalpha(function_name[begin--])) ;
begin += 2;
if (begin < end) {
behavior_name = function_name.substr(begin, end - begin);
}
}
#endif
return BehaviorFactory::instance().RegisterBehavior(
BehaviorDerived::BEHAVIOR_TYPE, Creator<BehaviorDerived>, behavior_name.c_str());
}
};
推荐答案
它似乎是用非Visual Studio编译器正确编译的东西,可能是gcc或clang,它支持扩展名__PRETTY_FUNCTION__。代码解析函数签名并提取依赖于此处未包含的某些代码的字符串。
__PRETTY_FUNCTION__被描述为 here [ ^ ] for gcc。信息此处 [ ^ ]表明clang以类似的方式实现它,但细节略有不同。
It appears to be something that compiles properly with a non-Visual Studio compiler, probably gcc or clang, which support the extension "__PRETTY_FUNCTION__". The code parses a function signature and extracts a string that depends on some code that is not included here.
__PRETTY_FUNCTION__ is described here[^] for gcc. Info here[^] suggests that clang implements it in a similar fashion but with slightly different details.
这篇关于分析行为基础文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文