Implement InfoMapper in subclass

30 views
Skip to first unread message

Rod B

unread,
Jun 3, 2025, 6:18:16 PMJun 3
to javacpp
Hi,

Can InfoMapper be implemented as a subclass? Or is there some way to tell the builder that a typedef already exists and not to generate a stub for it?

For instance in my header file I have:

    /**
     * Connection Error callback type
     */
    using ConnectionErrorCallback = std::function<void(ConnectionError)>;

and I am trying to make that type available to me in Java by making this class:

@Platform(
        include = {
                "mavsdk/core/include/mavsdk/mavsdk.h"
        }
)
@Name("mavsdk::Mavsdk::ConnectionErrorCallback")
public abstract class ConnectionErrorCallback extends FunctionPointer {
    static { Loader.load(); }

    /** Cast from a raw pointer (rarely needed). */
    public ConnectionErrorCallback(Pointer p) { super(p); }

    /** Allocate a new C++‐side std::function<void(ConnectionError)> stub. */
    public ConnectionErrorCallback() { allocate(); }
    private native void allocate();

    /** This method is invoked by C++ when a ConnectionError event fires. */
    @Name("operator()")
    public abstract void call(@ByRef Mavsdk.ConnectionError err);
}

But I keep getting compile time errors saying:

definition of type 'ConnectionErrorCallback' conflicts with type alias of the same name [arm64-v8a] 

How do I get around this by telling the builder that I don't want to have this generated this way?

I've tried just referring to the type without the alias and get another error:

  template specialization requires ‘template<>

which sucks because I try to define the template like it says to in the wiki but the InfoMapper doesn't take into consideration my hand written class.

Any help?  

Samuel Audet

unread,
Jun 3, 2025, 8:03:52 PMJun 3
to javacpp...@googlegroups.com, Rod B
If I understand correctly what you're looking for, we should be able to
get this working with something like
infoMap.put(new
Info("std::function<void(ConnectionError)>").pointerTypes("ConnectionErrorCallback").define());

Rod B

unread,
Jun 4, 2025, 4:55:58 PMJun 4
to javacpp
Thank you! That worked great.

I hope I'm not a bother but is there an exception for templates that take no args? My C++ skills aren't great so I could be wrong in describing this but I have another typedef

using ConnectionHandle = Handle<>;

and for some reason it won't generate correctly? Anytime the ConnectionHandle alias is mentioned in the C++ source code it is picked up by the parsers and some default stuff gets generated but even if I use .define() or .PointerTypes() the best that happens is that the garbage is replaced with what I describe in .PointerTypes but the java peer is never generated?

Samuel Audet

unread,
Jun 4, 2025, 7:21:16 PMJun 4
to javacpp...@googlegroups.com, Rod B
That probably just means that the file where the Handle template is
defined isn't in the include list.

Rod B

unread,
Jun 4, 2025, 7:40:22 PMJun 4
to javacpp
Crazy enough, it is being included? I'm gonna try using the logger to see if I can figure out what the issue is

Samuel Audet

unread,
Jun 4, 2025, 8:46:01 PMJun 4
to javacpp...@googlegroups.com, Rod B
I see, it's probably having problems picking up the default argument.
JavaCPP's parser isn't good enough for that. You'll probably need to do
something like
infoMap.put(new
Info("Handle<theDefaultArgument>",
"Handle<>").pointerTypes("ConnectionHandle").define());

Rod B

unread,
Jun 4, 2025, 10:16:03 PMJun 4
to javacpp
What if Handle doesn't have a default argument?

#pragma once

#include <cstdint>

namespace mavsdk {

template<typename... Args> class FakeHandle;
template<typename... Args> class HandleFactory;

/**
* @brief A handle returned from subscribe which allows to unsubscribe again.
*/
template<typename... Args> class Handle {
public:
Handle() = default;
~Handle() = default;

/**
* @brief Wheter handle is valid
*
* @return true if handle is valid
*/
bool valid() const { return _id != 0; }

bool operator<(const Handle& other) const { return _id < other._id; }
bool operator==(const Handle& other) const { return _id == other._id; }

private:
explicit Handle(uint64_t id) : _id(id) {}
uint64_t _id{0};

friend FakeHandle<Args...>;
template<typename...> friend class HandleFactory;
};

} // namespace mavsdk

Samuel Audet

unread,
Jun 4, 2025, 10:44:05 PMJun 4
to javacpp...@googlegroups.com, Rod B
Ah, a variadic template. That's not something that's supported too well
either, but it should work. If it doesn't please open an issue with a
simple example to reproduce it:
https://github.com/bytedeco/javacpp/issues
Reply all
Reply to author
Forward
0 new messages