lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tuesday 08 July 2008 07:41:37 Ry Lowry wrote:
> Ah, I understand now!  I did not do a very good job of studying the
> objects that were being passed to Luna.  I was assuming that
>
> ::className and ::methods were some dark magic I had not yet learned,
>
> but they were just plain old static members of the class that was
> being passed to Luna.  That's the danger of assuming!  Time to go get
> a glass of water and just sit on it for a while.  Thanks again for the
> light bulb moment.

dear top-poster,

the dark magic comes from behind, as always... no, just kidding, BUT:
you can actually get the class-name without any ugly hand writing, however, 
this *might* not always be what you want, however, here's a little neat 
example (using GCC) that automatically detects the class name:

#include <string>
#include <typeinfo>
#include <cxxabi.h>

inline std::string demangleSymbol(const char *AMangledName) {
    std::string result;
    if (AMangledName != 0) {
        char buf[1024];
        size_t len = sizeof(buf);
        int status = 0;

        try {
            result = abi::__cxa_demangle(AMangledName, buf, &len, &status);
            if (result.empty())
                result = AMangledName;
        } catch (...) {
            result = AMangledName;
        }
    } else
        result = "??";

    return result;
}

template<typename T>
inline std::string className() {
    return demangleSymbol(typeid(T).name());
}

class TMyClass { // a simple test class! nothing worth talkin'bout.
public:
};

int main() {
    // just two examples
    std::cout << className<TMyClass>() << std::endl;
    std::cout << className<std::string>() << std::endl;

    return 0;
}

This said, you could integrate my above code into luna.h (which I am using, 
btw) to let luna know about the class name without explicitely passing it, 
however, you *should* be aware, that C++ namespace seperators aren't that 
welcome in lua, and you shall replace them with something else, or create 
lua-namespaces by hand out of them.


Regards,
Christian Parpart.

Attachment: signature.asc
Description: This is a digitally signed message part.