Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

interop with C++ std::string #28

Closed
3 tasks done
ctaggart opened this issue Sep 27, 2017 · 2 comments · Fixed by #40
Closed
3 tasks done

interop with C++ std::string #28

ctaggart opened this issue Sep 27, 2017 · 2 comments · Fixed by #40

Comments

@ctaggart
Copy link
Owner

ctaggart commented Sep 27, 2017

The string interop is not currently correct. I'm not sure why I thought this might work.

    let name = CString::new("helloworld").unwrap();
    let pname = name.as_ptr() as *const octh::root::std::string;
    std::mem::forget(pname);

https://github.com/ctaggart/octh_examples/blob/723cfaf9fc6d16e0c0caec6148ecf1e1dbc62831/src/lib.rs#L11-L13

I think we have to disable .opague_type("std::.*") in build.rs in order to create bindgen for std::string. This opens up a can of worms though:

@mtmiller
Copy link
Collaborator

Much of the Octave API uses std::strings, so I think this will be pretty much required to get a useful binding working anyway. Even if you could get past the function call registration part of this, it would be hard to get the value of a string argument passed in to your function without std::string.

@ctaggart
Copy link
Owner Author

Reviewing this two years later, it looks like I should pursue the answer to the question I asked at https://stackoverflow.com/q/46524781/23059

I think I'm doing the interop wrong here for #18:

    let name = CString::new("add").unwrap();
    let pname = name.as_ptr() as *const octh::root::std::string;
    std::mem::forget(pname);

    let doc = CString::new("adds inputs and returns sum to all outputs").unwrap();
    let pdoc = doc.as_ptr() as *const octh::root::std::string;
    std::mem::forget(pdoc);
    
    let fcn = octh::root::octave_dld_function_create(Some(Fadd), shl, pname, pdoc);

It is creating char*, but those are not std::string and the cast is bad. Based on https://www.geeksforgeeks.org/convert-character-array-to-string-in-c/ may be this will work:

string string_create(char* a)
{
    string s(a);
    return s;
}

Or wrap octave_dld_function_create to use have pname and pdoc by char*.

ctaggart added a commit that referenced this issue Nov 21, 2019
* added stdstring_new

* add octave_value type helpers

* remove stdstring_new that did not work #28

* print all arg types

* added helloworld example

* started Rust API
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants