Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello everybody, can anyone analysis the "AutoRegister" function? I curious about it and thank you in advance, if you give me a suitable answer.
C++
#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());
    }
};
Posted
Updated 20-Jul-13 11:14am
v4
Comments
[no name] 20-Jul-13 16:45pm    
If you want a "suitable answer" then maybe you should ask a suitable question.
smss IR 20-Jul-13 17:04pm    
Doesn't suitable question? can you help me to improve it?
My question is about "AutoRegister" function and do NOT more, I will understand the role of behavior_name and how it works?
[no name] 20-Jul-13 17:13pm    
"Explain my code to me" is not a suitable question. You wrote it so you should understand it. Even if you did not write this code, you would find out everything there is to know about it by running it through the debugger and seeing what it does.

1 solution

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jul-13 3:17am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900