Pybind11: is make_tuple() owns wrapped pointers? - pybind11

Suppose I have a function like this, which I'm wrapping in Pybind11:
void func(){
SomeCppType* var1 = new SomeCppType();
SomeCppType* var2 = new SomeCppType();
return py::make_tuple(var1,var2)
}
What will happen with ownership of pointers? Is it posisble to tell pybind11::tuple to own the pointers and call delete on them? What is the correct way of writing this?

py::make_tuple is a templated function so you can do
return py::make_tuple<py::return_value_policy::take_ownership>(var1,var2);
with any return policy.

Related

Is there any way to route random string to dashboard in Codeigniter 3?

I have a question about Codeigniter: Is there any way to check function exists In the controller file and then route to the function path or else route it to a specific url?
I don't know what kind of Dashboard mean in your question.
I just answer how to check function exist or not in controller and route to that function.
In some case, you may need to add a route rule in config/routes.php.
$route['/programme/(.*)$'] = 'programme/programmes/$2';
Use method_exists() to check if function exist.
class Programme extends CI_Controller {
public function programmes($sub_part = NULL) {
if (!empty($sub_part) and method_exists($this, 'programmes_'. $sub_part)) {
return $this->{'programmes_'. $sub_part}(array_slice(func_get_args(), 1));
}
die('bar')
}
private function programmes_foo() {
die('foo')
}
}
When a visitor go to https://www.foobar.com/programme/foo (foo is something affect which function to call), your website should able to show foo. If the function is not exist, it show bar.
You can use
$route['product/(:any)'] = 'yourcontroller/your_function';
Source : https://codeigniter.com/userguide3/general/routing.html
i think you can change your route into this
$route[‘produk/(:num)/(:any)’] = ‘HomeControl/detail_produk/$1/$2’;
result from that route it's like this
http://www.localhost.com/produk/1/nama-produk-1
or maybe you can read the documentation CI 3 in this link or you can read my reference this link
i hope i can help you, thanks.

Casting with Single<> on RxJava

I would like to know if there is a way to do a cast from Single<Object> to Single<CustomClass>.
I have a class that implements a method that should return a Single<Customer>, I implemented the search like here
Single.create(single -> {
CustomerServiceDto customer = mapper.map(customerRepository.findById(id).get(), CustomerServiceDto.class);
single.onSuccess(customer);
});
There isn't any problem. It's what I need. This create returns me a Single<Customer> but when I implement another function to handling an exception
Single.create(single -> {
CustomerServiceDto customer = mapper.map(customerRepository.findById(id).get(), CustomerServiceDto.class);
single.onSuccess(customer);
}).onErrorReturn(error -> new CustomerServiceDto());
It returns me a Single<Object>. Can I do a casting here? To avoid change the method's signature. I tried with the classic (Single<Customer>) Single<Object> instance, but it isn't work. Thanks for your advice.
The answer was the #dano's comment. Thanks, #dano.

Should I always use "this" in Dart?

Is it a good practice to use always 'this' in a method.
Even if there is no usage of the same variable name as input to this method.
String getStory(number) {
return this.storyData[number];
}
By doing this there won't have any conflicts with any input variable name (never).
Thank you in advance for your feedback.
Generally this. is only supposed to be used when there is ambiguity about what variable is being used.
class Test{
final int x;
Test(this.x);
}

How to copy a bsoncxx::builder::basic::document to another?

Is there any way to safely copy a bsoncxx document to another.
In following code I am not able to do that
class DocClass
{
private:
bsoncxx::builder::basic::document m_doc;
public:
bsoncxx::builder::basic::document& copy(bsoncxx::builder::basic::document& obj)
{
obj = m_doc; //Not allowed
//Error C2280 attempting to reference a deleted function
}
};
There should not be any harm to the object even after copy.
Please help.
Thanks,
Shibin
If you want to copy a bsoncxx::document::value, you can construct a new one from its view:
bsoncxx::document::value foo = ...;
bsoncxx::document::value bar{foo.view()};
bsoncxx::builder::basic::document is only movable, not copyable. However, you can get view to the underlying document from the builder with the view() method, which might be able to help you depending on your use cases. You'll still only be able to extract from the builder once though, so you'll have to rely on constructing a second document::value if you need more than one.

Unable to modify Coffeescript's global variables in a JQuery listener

Today I was migrating some of my javascript code into coffeescript and got stuck in something really silly but even though I didn't know how to make it work.
I wanted to update the value of a global variable when a click event was triggered, have a look at the code below to see one of my guesses
Here's the code
#activeObject = null
# Some other code
$ ->
$('#header').click ->
if !headerSelected
showMenu '#header-menu', event
else
#activeObject = "#header"
showMenu '#menu-style-header', event
Unfortunately even though the click event was triggered the variable was not getting updated.
I came up with a work around. I created a function that set the value of the variable and called it instead of the assignment and this time it worked.
I just wanted to know why I wasn't able to do it the other way. For me it was a simple operation and it seemed silly to define a new function just for this.
Your problem is that # (AKA this) inside the click handler isn't the same as it is outside so this:
#activeObject = null
and this:
#activeObject = "#header"
are referring to two different activeObjects. You should be able to bind everything with => to get the right this:
$ =>
$('#header').click =>
#...
or better (IMHO), just refer to window.activeObject directly in both places so that it is obvious to everyone that you're referring to a global variable:
window.activeObject = null
$ ->
$('#header').click ->
if !headerSelected
showMenu '#header-menu', event
else
window.activeObject = "#header"
showMenu '#menu-style-header', event
Alternatively, you could stop using globals altogether in favor of, perhaps, a data attribute:
$ ->
$('#header').data 'activeObject', null
$('#header').click ->
if !headerSelected
showMenu '#header-menu', event
else
$(#).data 'activeObject', '#header'
showMenu '#menu-style-header', event
I think the confusion is about the usage of #, which is basically just a shortcut for this.
If you compile your code and see what CoffeeScript compiler it produces, the confusion becomes clear
this.activeObject = null;
$(function() {
return $('#header').click(function() {
if (!headerSelected) {
return showMenu('#header-menu', event);
} else {
this.activeObject = "#header";
return showMenu('#menu-style-header', event);
}
});
});
if activeObject is global you whould reference to it
window.activeObject = null
and
window.activeObject = "#header";
in both occurences in this code, cause one might be tempted to use it without window in second occurence, but that will cause a new local variable to be implecitly defined.
Generally when starting with CoffeeScript, its usefull to try small snipets like this in
http://coffeescript.org/ on the Try Now Tab