pthom / litgen

litgen: a pybind11 automatic generator for humans who like nice code and API documentation. Also a C++ transformer tool
https://pthom.github.io/litgen
GNU General Public License v3.0
46 stars 6 forks source link

`const_` field not specified with overload_cast for overloaded member methods that are const #11

Closed vishwa2710 closed 4 months ago

vishwa2710 commented 4 months ago

Hello! The following code snippet:

import litgen
from litgen.demo import litgen_demo

options = litgen.LitgenOptions()

# This namespace will not be outputted as a python module
options.namespaces_root = ["ostk", "core", "type"]
options.python_convert_to_snake_case = True

code = """
namespace ostk
{
namespace core
{
namespace type
{

typedef int8_t Int8;
typedef int16_t Int16;
typedef int32_t Int32;
typedef int64_t Int64;

typedef uint8_t Uint8;
typedef uint16_t Uint16;
typedef uint32_t Uint32;
typedef uint64_t Uint64;

/// @brief                      Integer type

class Integer
{
   public:
    typedef int32_t ValueType;

    /// @brief              Constructor
    ///
    /// @code
    ///                     Integer integer(123) ;
    /// @endcode
    ///
    /// @param              [in] anInteger An integer

    Integer(Integer::ValueType anInteger);

    bool operator==(const Integer& anInteger) const;

    /// @brief              Equal to ValueType operator
    ///
    /// @code
    ///                     Integer(123) == 123 ; // True
    /// @endcode
    ///
    /// @param              [in] anInteger An integer (ValueType)
    /// @return             True if integer is equal to ValueType

    bool operator==(const Integer::ValueType& anInteger) const;

   private:

    Integer::Type type_;
    Integer::ValueType value_;

    Integer(const Integer::Type& aType, const Integer::ValueType& anInteger);
};

}  // namespace type
}  // namespace core
}  // namespace ostk
"""

litgen_demo.demo(
    options, code, show_cpp=False, show_pydef=True, height=80
)

generates the following pybind11 bindings:

auto pyClassInteger =
    py::class_<ostk::core::type::Integer>
        (m, "Integer", "")
    .def(py::init<Integer::ValueType>(),
        py::arg("an_integer"))
    .def("__eq__",
        py::overload_cast<const ostk::core::type::Integer &>(&ostk::core::type::Integer::operator==), py::arg("an_integer"))
    .def("__eq__",
        py::overload_cast<const Integer::ValueType &>(&ostk::core::type::Integer::operator==), py::arg("an_integer"))
    ;

This does not compile however as the operator== methods are of const type.

    bool operator==(const Integer::ValueType& anInteger) const;

and therefore the overload_cast must also include the const_ field:

    ...
    .def("__eq__",
        py::overload_cast<const ostk::core::type::Integer &>(&ostk::core::type::Integer::operator==, const_), py::arg("an_integer"))

I have very briefly looked at the litgen and might be missing something. Happy to hear your thoughts!

Thanks!

pthom commented 4 months ago

Hello,

This should be fixed in this commit: https://github.com/pthom/litgen/commit/226992d4e30be298963fa96e52254e2e45af6477

Please do keep me informed about the result.

Also, I'm always curious to know in what ways people are using this project. Can you give me some more information about what you are doing with it?

Thanks!

vishwa2710 commented 4 months ago

Amazing, thank you for the lightening fast response, that indeed did the trick! I am one of the developers of the Open Space Collective, which is a set of open source repositories for various space applications. We use pybind11 to create python bindings for our repos, and I have always been on the lookout for something that would allow us to automate binding generation.

As a separate effort I have also been trying to use pybind11-stubgen to generate stubs for our packages, and litgen looked like it might solve both!

I'm going to try and play with it some more and see how well it works. I will definitely reach out with some questions. Thanks so much!

pthom commented 4 months ago

I am one of the developers of the Open Space Collective, which is a set of open source repositories for various space applications. We use pybind11 to create python bindings for our repos, and I have always been on the lookout for something that would allow us to automate binding generation.

Interesting! If you get to something that does work, please keep me informed.