bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.48k stars 581 forks source link

Mapping Template Constraints, Parser Error #725

Closed benshiffman closed 11 months ago

benshiffman commented 11 months ago

I have a simple template class that I'm testing as a proof of concept that uses the C++20 template features, namely template type constraints. It's causing a hitch with the parser that no amount of infoMapping I've tried (except skipping of course) has been able to fix. I'm curious to know whether this is even possible to map or if it must be handled in a helper class with JNI bindings.

Exact error text is written as a comment on the line that caused it.

class DLLExport TemplatedClass
{
public:

    TemplatedClass() = delete;
    TemplatedClass(const TT& val1, const TT& val2);

    #ifdef USE_BASIC_CLASS
    TT add() const;
    #else
    TT add() const requires !std::same_as<TT,bool>; //Could not parse declaration at '::'
    size_t add() const requires std::same_as<TT,bool>;
    #endif

private:
    TT m_data[2];
};
saudet commented 11 months ago

It can work if we add something to ignore the requires bit, yes.

benshiffman commented 11 months ago

I spent some more time cracking away with infoMapping to no avail. Do you believe this would have to be fixed in the parser? If not, what kind of mapping would allow me to completely ignore requires and std::same_as<TT,bool>?

The following attempts have been unsuccessful in terms of getting past the parser error:

infoMap.put(new Info("std::same_as").skip());
infoMap.put(new Info("requires").skip());
infoMap.put(new Info("requires std::same_as").skip());
infoMap.put(new Info("requires !std::same_as").skip());
infoMap.put(new Info("TT add() const requires !std::same_as<TT,bool>").cppText("TT add() const"));
saudet commented 11 months ago

It's something we'd need to add to the parser itself. If you have control over that source code though, we can easily put those in macros and tell the parser to ignore them: https://github.com/bytedeco/javacpp/wiki/Mapping-Recipes#ignoring-attributes-and-macros

benshiffman commented 11 months ago

This is now running a lot smoother since I've added my own property file with C++20 enabled. After putting "requires !std::same_as<TT,bool>" and "requires std::same_as<TT,bool>" in macros, I've been successful at getting past the parser error by either skipping them or a using cppText(""). Not sure which is better here since they both work the same. This was a workaround since using cppTypes().annotations() causes an error in the macro definition though.

Now I'm trying to get the add() function that's created for TemplatedClassBool to point to the "size_t add()" function but as far as I can tell it's difficult for the infomapper to differentiate between return types and it's still pointing to "TT add()". I get a "NoSuchMethodError" when I try to call the library from a demo java class, but only on TemplatedClassBool.add()". These are my current mappings, any tips?

infoMap.put(new Info("REQUIRES_NOT_BOOL", "REQUIRES_BOOL").cppText(""));
infoMap.put(new Info("USE_BASIC_CLASS").define(false));
infoMap.put(new Info("TemplatedClass<double>").pointerTypes("TemplatedClassDouble"));
infoMap.put(new Info("TemplatedClass<int>").pointerTypes("TemplatedClassInt"));
infoMap.put(new Info("TemplatedClass<bool>").pointerTypes("TemplatedClassBool"));
infoMap.put(new Info("TemplatedClass<bool>::add").javaText("public native int add();"));
benshiffman commented 11 months ago

I'll be darned. Scratch that last comment. I guess maven took some time to load the new libraries because it's working now!