1

For example I do have this code:

class MyClass : public QObject
{
    Q_OBJECT
public:
    void MyClass()
    {
    QStringList selfFunctionList;
    //Get functions list
    qDebug()<<selfFunctionList;
    }
    void function1(){}
    void function435(){}
};

How do I set to selfFunctionList list of "function1" and "function435"?

IGHOR
  • 691
  • 6
  • 22
  • 1
    Do you know function pointers? – fonZ Aug 06 '14 at 17:49
  • I know function pointers, but what do you mean? – IGHOR Aug 06 '14 at 17:51
  • 1
    Well it's not really clear to me what you want to achieve. – fonZ Aug 06 '14 at 17:51
  • I want to get list of function names to QStringList without adding additional code to functions body. – IGHOR Aug 06 '14 at 17:52
  • Why would the user need to know the name of all of the member functions of a class at run time? This sounds like something that should be somewhere in the class documentation... – scohe001 Aug 06 '14 at 17:53
  • QStringList is class of Qt, but anyway, it probably possible do this only in C++. I ask the same by another words: how can I printf all function names during runtime? I made script language and I want to make it more easy to add new methods, functions begins with "get_" should be automatically added to menus. If I'll get names of functions that I'll need to change code only in one place. – IGHOR Aug 06 '14 at 17:57
  • This is where Objective-C is much greater than C++ – Andrey Chernukha Aug 06 '14 at 18:00
  • :) Hope Qt will help with it and I just don't know solution. – IGHOR Aug 06 '14 at 18:02
  • @AndreyChernukha look at c++11 and c++14 for example. With C++ you can do anything, just have to know how. – fonZ Aug 06 '14 at 18:14
  • @fonZ ok, I will, thank you for the suggestion! Can you please tell me what exactly C++ offers for introspection? – Andrey Chernukha Aug 06 '14 at 18:17
  • @AndreyChernukha check out draft n3951: C++ type reflection via variadic template expansion. That is one way of achieving introspection in C++14. And it can also be done with previous versions. – fonZ Aug 06 '14 at 19:32

2 Answers2

3

You can use the staticMetaObject for that.

class Test : public QObject
{
    Q_OBJECT
public:
    explicit Test(QObject *parent = 0) :
        QObject(parent)
    {
        for (int n = 0; n < staticMetaObject.methodCount(); n++) {
           functions.append(QString::fromLocal8Bit(staticMetaObject.method(n).name()));
        }
        qDebug() << functions;
    }

signals:
    void testSignal();

private slots:   
    void privateTestFunction() {}

public slots:   
    void publicTestFunction() {}

private:
    QStringList functions;
};

One condition, the functions need to be declared as slots or signal.

Output:

("destroyed","destroyed","objectNameChanged","deleteLater","_q_reregisterTimers",
"testSignal","privateTestFunction","publicTestFunction")
fonZ
  • 2,428
  • 4
  • 21
  • 40
  • @IGHOR You see this part right? "One condition, the functions need to be declared as slots or signal." As I said in my answer Qt's moc won't help in the general case. – Matt Phillips Aug 06 '14 at 18:23
  • 1
    Required functions I need is already slots. I didn't mention it because I thought that this information is not needed to solve my problem :). This answer solved my problem 100%. – IGHOR Aug 06 '14 at 18:30
  • 1
    @IGHOR This information *is* needed to solve your problem because the information about the signals and slots is not collected by the C++ compiler, but by the `moc` code generator that comes with Qt. It's the `moc` that reads your source file and generates the metadata that describes the invokable methods on the class. It adds the reflection data that's otherwise missing from the C++ standard. – Kuba hasn't forgotten Monica Aug 06 '14 at 23:36
  • Of course I understand it now. I hoped that there is C++ solution. But Qt is fine for me too. – IGHOR Aug 07 '14 at 03:30
0

C++ does not have the sort of self-awareness (reflection) you seem to want here. That is, there is no programmatic way to get the string name of a function and enter it into a container without hand-coding it. Qt does a very limited amount of this with its signals and slots, but not functions in general. So you will have to write

MyClass::MyClass(...)
{
   selfFunctionList << function1 << function345 << ... etc.
}

as the constructor, or something thing similar.

Edit: OP brought these macros to my attention which give you a bit of introspection, but implementing them to solve OP's problem would be far more arduous than the above, see comments.

Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
  • I see. I can use this like code. I was interested in automated method because If I add 10 functions, but forgot to add it to list of selfFunctionList it will cause error. Also if I remove function from code and forgot to remove from selfFunctionList it bad too. It needed just in case to cover human factor. So there is no way to make it automated? – IGHOR Aug 06 '14 at 17:59
  • @IGHOR Not within the C++ program itself. What you could do is write a program that reads your `.h` file and extracts the function names, and then auto-generates the files you need with these names. Auto-generating code, even C++, is less difficult than you might expect, especially if you use a characteristic naming conventions in your `.h` file. – Matt Phillips Aug 06 '14 at 18:02
  • Thanks for answer. I thinked about this question because I have found this http://stackoverflow.com/questions/597078/file-line-and-function-usage-in-c And just want to know is there way to get all __FUNCTION__ of current class. – IGHOR Aug 06 '14 at 18:06
  • @IGHOR Thanks for that reference I had never looked into those macros before. However they only print the name of the function being *executed*, so there is really no way to use them without writing far more code than is required simply pushing the names--in each function you would need a __func__ call, then you would need to actually call that function, push the name on the list, but only the first time it was called, so you'd need a local static variable... etc. – Matt Phillips Aug 06 '14 at 18:15