I am trying to define a member function as a delegate for a C++/WinRT component. The component is something that I am trying to build to connect to a BLE device
The idl for the component defines the event as such
event Windows.Foundation.EventHandler<UInt32> ConnectNotification;
In the consuming app I am able to register a delegate for event's if I define it as a lambda function in this fashion
foundBleNotification = m_bleManager.ConnectNotification([](const auto&, uint32_t foundFlags)
{
OutputDebugStringW((L"Lambda Called handler->" + hstring(std::to_wstring(foundFlags)) +L"\r\n").c_str());
});
The above works fine and it gets invoked as expected,
However I would like to try and understand how to use a member function for the delegate instead of a lambda function, based on the idl I assumed I could simply define a member function as so...
void BleFound(uint32_t foundFlags);
And then register the notification by....
foundBleNotification = m_bleManager.ConnectNotification(&MainPage::BleFound };
This results in an error that I just cannot figure out :-(
Error C2511 'void winrt::HelloWorldWinRT::implementation::MainPage::BleFound(const winrt::implements<D,winrt::HelloWorldWinRT::MainPage,winrt::composing,winrt::Windows::UI::Xaml::Controls::IPageOverrides,winrt::Windows::UI::Xaml::Controls::IControlOverrides,winrt::Windows::UI::Xaml::Controls::IControlOverrides6,winrt::Windows::UI::Xaml::IFrameworkElementOverrides,winrt::Windows::UI::Xaml::IFrameworkElementOverrides2,winrt::Windows::UI::Xaml::IUIElementOverrides,winrt::Windows::UI::Xaml::IUIElementOverrides7,winrt::Windows::UI::Xaml::IUIElementOverrides8,winrt::Windows::UI::Xaml::IUIElementOverrides9,winrt::Windows::UI::Xaml::Markup::IComponentConnector,winrt::Windows::UI::Xaml::Markup::IComponentConnector2>::IInspectable &,uint32_t)': overloaded member function not found in 'winrt::HelloWorldWinRT::implementation::MainPage' HelloWorldWinRT C:\Users\Jay\source\repos\HelloWorldWinRT\HelloWorldWinRT\MainPage.cpp 66
I tried reading up on this here
https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/weak-references
But that has me even more confused...
Can some experts please point out what I am doing incorrectly and maybe define a member function that would work per the lambda function?
Thanks!
Related
I'm trying to call a function using reflection based on it's name.
I first recover the function with
f := reflect.ValueOf(s).MethodByName(name)
I do error control on the function name by checking if "f" is valid with
if !f.IsValid() {
return errors.New("There is no function with that name")
}
And finally I perform the call with
f.Call(inputs)
My challenge is that inputs depends on the user, and sometimes they can put too many parameters, too little or invalid types.
IE:
2019/01/04 16:47:54 http: panic serving [::1]:53662: reflect: Call using string as type int
How can I control that the inputs are valid before performing the call?
Maybe there is a way to recover the expected inputs in my method and check them against the provided ones?
I have modified inet NodeStatus.cc with a customized function that return the variable value as follows:
int NodeStatus::getValueA()
{
return ValueA;
}
Then, I created another simple module called simpleNodeB.cc and I wanted to retrieve ValueA from NodeStatus.cc. I tried the following code in simpleNodeB.cc but didn't work:
if(getParentModule()->getSubModule(NodeStatus).getValueA()==test1)
bubble("Value is the same");
The error message I got -> error: expected expected primary-expression before ')' token. I'm not sure if I used the correct way to call getValueA() function. Please enlighten me. thanks a lot.
There are many errors in your code.
The method getSubmodule requires a name of module, not a name of class. Look at your NED file and check the actual name of this module.
getSubmodule returns a pointer to the cModule object. It has to be manually cast into another class.
Assuming that an NodeStatus module in your NED is named fooStatus the correct code should look like:
cModule *mod = getParentModule()->getSubmodule("fooStatus");
NodeStatus *status = check_and_cast<NodeStatus*>(mod);
if(status->getValueA() == test1)
bubble("Value is the same");
Reference: OMNeT++ Manual.
I'm trying to build a swift script but I got stuck with this error:
./1.swift:10:11: error: use of instance member 'thisIsmyFunction' on type 'myScript'; did you mean to use a value of type 'myScript' instead?
myScript.thisIsmyFunction()
~~~~~~~~ ^
Here is my code:
#!/usr/bin/swift
import Foundation
class myScript {
func thisIsmyFunction() {
print("Do something in there!")
}
}
myScript.thisIsmyFunction()
What I'm trying to do is access to the function and execute the print.
Any of you knows what I'm doing wrong?
I'll really appreciate your help.
You can only call an instance method on an instance of a class. For example, you would have to create an instance of myScript, then call it:
let script = myScript()
script.thisIsmyFunction()
You can also choose to make thisIsmyFunction a class method (which is formally known as a "type method" in Swift), and call it like the way you are doing right now:
class func thisIsmyFunction() {...}
Note the class modifier in front of func. Of course, this means you can't access self inside the function, because there is no longer an instance of the class.
For more information, see the Swift documentation on methods.
Aside: Swift classes should start with capital letters.
I have a following method:
using async_handler_t = std::function<void(boost::system::error_code, std::size_t)>;
async_handler_t deadlineHandler(boost::system::error_code &ec) {
return [&ec, this](boost::system::error_code newEc, std::size_t) {
ec = newEc;
deadline_.cancel();
};
}
This is simple asio deadline handler which stops the deadline timer and allows the io_service-run loop to continue (grabbed this from official asio timeout docs).
The question is about how much times the function itself is generated. I understand, that I return a functional object from this method (std::function object) which is created dynamically during runtime but is the target function created only once?
The code inside the body of the lambda will only be compiled once and all instances of that lambda's closure object type will share the code. But each call to deadlineHandler will result in a new instance of the closure type being created, its members ec and this being initialised, and then a std::function object being created from that instance.
The lambda expression implicitly declares a functor class, and creates an instance of it.
The generation of the class' type (and the code of its operator()) is, of course, done once at compile-time.
A new instance of this class (containing what the lambda captured) is created each time you call your function.
I try to implement a C# program in VB.NET and I've stuck on a point. I would like to get the exact equivalent. I could not find out how to pass an event to a sub or function in the following way. So the part which I cannot do in vb.net is var mediaOpenedHandler = MediaOpened;. In VB you cannot pass an event like this. If I write in VB: Dim mediaOpenedHandler = MediaOpened then it says "'Public Event MediaOpened()' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event." So how can I get exactly the same result. Thanks in advance.
public abstract class MediaPlayerBase : WorkDispatcherObject
{
…
public event Action MediaOpened;
…
protected void InvokeMediaOpened()
{
var mediaOpenedHandler = MediaOpened;
if (mediaOpenedHandler != null)
mediaOpenedHandler();
}
}
of the top of my head, it is literally just
RaiseEvent mediaOpenedHandler()
I found the answer myself here Event handler raising method convention. So basically as hermiod answered you just use RaiseEvent in VB. In C# they have to use this local assignment for this purpose: "In a multi-threaded application, you could get a null reference exception if the caller unregisters from the event. The assignment to a local variable protects against this." In VB the RaiseEvent handles this internally.