Re: Fwd: access enclosing class name in a %macro to allow defining attributes without having to use the %attribute macro

William S Fulton <[email protected]> Mon, 20 Nov 2023 18:33:31 +0000
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftDE8N=zxAg6kkr_PUdMZdSJU=BNZhPUJtTihfpP+uR_-A@mail.gmail.com>
There is no C++ syntax nor SWIG code that I can think of for getting the
enclosed class name for mangling into a function definition. When declaring
C++ functions, the C preprocessor is about as good as it gets.

If you are just trying to define an immutable (in target language only)
property and you can modify the code you are wrapping, then use something
like this:

%{
#define READONLY_PROPERTY(Type, Name) Type Name
%}


#ifdef SWIG

#define READONLY_PROPERTY(Type, Name)
               \
  %immutable Name;
                \
  Type Name

#endif

%inline %{
// foo.hpp
struct Bar {};
struct Foo {
  READONLY_PROPERTY(Bar, bar);
};
%}

William

On Fri, 3 Nov 2023 at 14:25, Alessio M <[email protected]>
wrote:

> Greetings
>
> Question: can a %macro in SWIG somehow access the name of the enclosing
> class type in a statement like %mangle(...)?
>
> The objective here is to avoid having to use the %attribute macros
> I would prefer to write hpp files as follows and just %include them
> without any need for %attribute
>
> // foo.hpp
> class Foo {
>   READONLY_PROPERTY(Bar, bar);
> };
>
> Following the technique used in attributes.swg I came up with the
> following possible definition for a READONLY_PROPERTY C++ macro.
> The only issue is the %mangle(Class) statement. If I could access the
> enclosing class name, then this would work quite nicely.
>
> Thank you for any advice you can provide
>
>
> #ifndef SWIG
>
> #define READONLY_PROPERTY(Type, Name) Type Name() const
>
> #else // SWIG defined
>
> #define READONLY_PROPERTY(Type, Name)
>                  \
>   %ignore Name() const;
>                  \
>   Type Name() const;
>                 \
>   %immutable Name;
>                 \
>   %extend {
>                  \
>     Type Name;
>                 \
>   }
>                  \
>   %{
>                 \
>     template < class C > inline Type %mangle(Class) ##_## Name ##
> _get(const C* self_) {   \
>       return self_->Name();
>                  \
>     }
>                  \
>   %}
> _______________________________________________
> Swig-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user