Call CPP Function from RTSP-Media-Factory.c - gstreamer

I have CPP Function. I need to call this function from rtsp_media_factory.c file in Gstreamer rtspServer.
How can I call this function?
I have a function pointer
GstElement* CreatePipeline(GstRTSPMediaFactory *factory)
in rtsp_media_factory.c.
I need to assign a CPP class function to above function pointer.
How can I do that?

If it is really function pointer, you can assign to it only static class member because other members also contains this in signature. But, as I understand, you can assign to it any function (not class member).

Related

Calling private function in lambda from outside the class

In my current project I am trying to pass a private member function as parameter to another function. In my code, the other function is a member function of a different class, but for keeping it simple, here it is a free function.
void outside_function(std::function<void(int)> func) {
// do something with func
}
class MyClass {
public:
void run();
private:
bool my_func(double); // defined in source file
};
Now, from inside run I want to put my_func into outside_function as argument. Since the signature of run does not fit the requirements for the parameter, I cannot simply pass it. Using the adapter pattern was my first try, which was when I was reminded that member functions implicitly take the this pointer as first argument. This answer suggests using a lambda expression, which is what I did.
void MyClass::run() {
outside_function([this](int a) -> void {
double d = static_cast<double>(a);
this->my_func(d);
});
}
Why does this work? From my point of understanding, outside_function does not have access to my_func. Why don't I have to make my_func public first? Is it something the compiler does or some C++ rule I don't know?
Additionally, are there any catches to this approach that might break my code?
private access specifier only restrict an object name to be visible (it cannot be looked up) outside the class. The object itself is like any other member.
[class.access]/1
A member of a class can be
(1.1) private; that is, its name can be used only by members and friends of the class in which it is declared.
(1.2) protected; that is, its name can be used only by members and friends of the class in which it is declared, by classes derived from that class, and by their friends (see [class.protected]).
(1.3) public; that is, its name can be used anywhere without access restriction.
outside_function never accesses the member (it only knows the type of the func parameter and can't possibly care about how it's defined) -- only the lambda function does.
Since you're defining the lambda function inside MyClass, you can access all the private names within it.
outside_function calls the provided function, but if you weren't allowed to call a function that uses any private members, you couldn't do much.

Why can't I pass the this pointer explicitly to a member function?

The c++ standard (ISO c++11) mentions in Section 9.3.1 that
A non-static member function may be called for an object of its class
type, or for an object of a class derived (Clause 10) from its class
type, using the class member access syntax (5.2.5, 13.3.1.1).
An attempt to compile this code with g++ (version 4.8.2)
class foo{
public:
void bar(){
cout<<"hey there"<<endl;
}
};
int main(){
foo obj;
foo::bar(&obj);
}
gives a compile time error because it couldn't match the function's signature. I guess that is expected given what the standard states about calling member functions.
Since the method will eventually take a form similar to bar(foo*) during some stage of compilation, why does the standard asks for member access syntax to call the member function?
Lets add a static member to the class as:
class foo{
public:
void bar() { cout<<"hey there"<<endl; }
static void bar(foo*) { cout<<"STATIC MEMBER"<<endl; }
};
Now if you write this:
foo::bar(&obj); //static or non-static?
Which function should be called? In such situation, how would you call both of them? What would be the syntax? If you allow one function to have this syntax, you've to abandon it (i.e syntax) for other function. The Standard decided to have foo::bar(&obj) syntax for static member function, while abandoning it for non-static member function.
Anyway, if you want to pass &obj as argument to the non-static member function, then you can use type-erasure facilitated by std::function as:
void (foo::*pbar)() = &foo::bar; //non-static member function #1
std::function<void(foo*)> bar(pbar);
bar(&obj); //same as obj.bar();
Likewise, you could call static member function as:
void (*pbar)(foo*) = &foo::bar; //static member function #2
std::function<void(foo*)> bar(pbar);
bar(&obj); //same as foo::bar(&obj);
Note that at lines #1 and #2, the types of the object pbar makes the compiler to choose the correct member function — in the first case, it takes the pointer to the non-static member-function while in the latter case, it takes the pointer to the static member function.
Hope that helps.

An issue with a non-member function

A problem I'm working on is asking me to define an istream constructor inside the class body. Let's call the class Sound. Now this constructor uses a function in its own body. But the function is supposed to be a non-member function. I have it defined in another file, but I have it declared in the header that contains the class definition itself. I've placed the header in the other file containing the non-member functions already.
The problem is, one of the parameters of the non member function has type Sound and it performs operations on the type Sound objects.
When I declare this function in the header file, if I put it before the class definition I get an error saying the objects haven't been defined.
When I put the declaration after the definition, the constructor now tells me that the function inside it's body is undefined.
If I put the declaration inside the class body, it becomes a member function.
The problem didn't explicitly state that I cannot make the function a member function so I am wondering if there is a way to overcome this catch-22.
You do not necessarily need to make the function member. You can have at least two ways to solve it in a different manner.
The problem is that you are having all this in a situation where a Sound object is not yet defined if I understand correctly.
1) You can refactor your code a bit as demonstrated below.
header file:
class Sound
{
public:
Sound();
doStuff();
}
cpp file:
void non_member_function(Sound sound)
Sound::Sound() { non_member_function(*this); }
Sound::doStuff() {}
void non_member_function(Sound sound) { sound.doStuff(); }
2) if you insist on the scenario aforementioned, you put a Sound sound forward declaration before the non-member function to get the type recognized. Or, you can just put the declaration after the class declaration.
header file:
class Sound
{
public:
Sound();
doStuff();
}
void non_member_function(Sound sound)
cpp file:
Sound::Sound() { non_member_function(*this); }
Sound::doStuff() {}
void non_member_function(Sound sound) { sound.doStuff(); }
Looks like best thing to do is to use forward declaration for class, before the function:
header:
class Sound;
void f(Sound s);
class Sound
{...};
In c++ you can define a function as a member function or a non-member function. If it is not specified or required that the function be define as member or non-member as you have pointed out, then I would opt for an easy or simple solution.
Calling a non-member function within a constructor might require you, to use a certain technic to go about this function call, now that might not be required if the function was defined as a member function.

Using static function in class where a variable depends on constructor

Lets say I have a class with a static function. This function is called as static by another part of the code, without instantiating a class object. However, this static function also has in its code a dependance on variable x. x, however, is only initialized to a certain value or cleared in the constructor. But, given that no object of the class is instantiated, I believe that the variable is undefined. So, I have a few questions:
1) Is the constructor called at all?
2) Is the variable x undefined?
How can I work around a case like that?
In the example below, i'd want y to be 25, but can it ever be?
class CExample
{
public:
CExample(void);
~CExample(void);
static void foo();
int x;
};
CExample::CExample()
{
x = 5;
}
void CExample::foo()
{
int y = x*5;
}
Your code doesn't compile since the static function is using a variable which will only exist in objects instantiated by the class.
I'm not sure you've understood static. Only one static function exist. You can call it from anywhere. Which instance if CExample's x is it supposed to use?
This looks like a case of bad design. You could fix it so that it compiles by making x static and initialising it with 5. However you are probably a lot of better rethinking your design and what you want it to do. Remember there is only ever one instance of something that is static but there are as many instances of CExample as times you call it's constructor.
A static member function is a service of the class, not of a specific object of the class. A class's static data members and static member functions exist independently of instantiation of a object of that class.
The use of a static function will not call the constructor of its respective class, therefore your variable will not exist, causing a compilation error.
Just remember static member functions exist and operate independently of any objects of the class.
It's illegal to access a non-static member from a static function. See the output of gcc when I try to compile your program:
test.cpp: In static member function ‘static void CExample::foo()’:
test.cpp:9: error: invalid use of member ‘CExample::x’ in static member function

How to call a non-member function that takes in an object within a method

Say I have a class Student, and I have already declared a non-member function called "function_A" that takes in as an argument, type Student.
Now say INSIDE the Student class, I had a member function, and in it, I wanted to reference the non-member function, "function_A", declared earlier. What would I pass in as the argument (the argument itself must be type Student).
code
Do you mean something like this?
void function_A(Student s);
class Student {
void function_A() {
::function_A(*this);
}
if the member function's name is different than function_A, I can't see any problem.