Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8811277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:19:26+00:00 2026-06-14T03:19:26+00:00

I have some code like below: class SingleValue { public: SingleValue() {} ~SingleValue() {}

  • 0

I have some code like below:

class SingleValue
{
public:
   SingleValue()
   {}

   ~SingleValue()
   {}

   const std::string& getValue()const
   {
    return m_nSingleValue;
   }
private:
   int m_nSingleValue;
};

typedef SingleValue RoadType;
typedef SingleValue RoadSubType;
typedef SingleValue FunctionalClass;

now I want to use SWIG to generate a Java wrapper, But it generate only one proxy class ‘SingleValue’, I want to know how generate the other proxy class using swig, but I can’t find some relative information after googling.

I try %rename, but it only generate one proxy class, not three.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:19:27+00:00Added an answer on June 14, 2026 at 3:19 am

    What you’re trying to achieve seems to be strong typing. SWIG by default tries to expose an interface with the same behaviours as you would see in C++, so in that case the behaviour is expected – weak typedefs are all C++ offers. You can work around this though.

    Given the header file:

    #include <string>
    
    class SingleValue
    {
    public:
       SingleValue()
       {}
    
       ~SingleValue()
       {}
    
       std::string getValue() const
       {
          return std::to_string(m_nSingleValue);
       }
    private:
       int m_nSingleValue;
    };
    
    typedef SingleValue RoadType;
    typedef SingleValue RoadSubType;
    typedef SingleValue FunctionalClass;
    
    inline RoadType make_road() { return RoadType(); }
    FunctionalClass make_func() { return FunctionalClass(); }
    

    which differs from yours only in the corrections to getValue() and the addition of two inline functions for testing we can wrap it and get close to strong typedef semantics by doing:

    %module test
    
    %{
    #include "test.h"
    %}
    
    %include <std_string.i>
    
    class SingleValue
    {
    public:
       SingleValue();
       ~SingleValue();
       std::string getValue() const;
    };
    
    struct RoadType : public SingleValue {
    };
    
    struct RoadSubType : public SingleValue {
    };
    
    struct FunctionalClass : public SingleValue {
    };
    
    RoadType make_road();
    FunctionalClass make_func();
    

    Notice that I’ve not shown SWIG the typedef at all and I’ve lied completely about the type and existance of RoadType etc., but this is OK like that because all the code that gets generated by SWIG is legal and correct still.

    This cause an interface to be generated where the make_X functions return distinct types.


    If you wanted to avoid the duplication between the header file and the interface file you could introduce a macro to help, the header file becomes:

    #include <string>
    
    class SingleValue
    {
    public:
       SingleValue()
       {}
    
       ~SingleValue()
       {}
    
       std::string getValue() const
       {
          return std::to_string(m_nSingleValue);
       }
    private:
       int m_nSingleValue;
    };
    
    #ifndef STRONG_TYPEDEF
    #define STRONG_TYPEDEF(o,n) typedef o n
    #endif
    
    STRONG_TYPEDEF(SingleValue, RoadType);
    STRONG_TYPEDEF(SingleValue, RoadSubType);
    STRONG_TYPEDEF(SingleValue, FunctionalClass);
    
    inline RoadType make_road() { return RoadType(); }
    FunctionalClass make_func() { return FunctionalClass(); }
    

    Which means the interface file can simply become:

    %module test
    
    %{
    #include "test.h"
    %}
    
    %include <std_string.i>
    
    #define STRONG_TYPEDEF(o, n) struct n : o {};
    
    %include "test.h"
    

    This works partly because SingleValue is a class so the strong typedef can become a sub-class in the Java type system for it to enforce checking on. If the type isn’t a class you can still do the same thing without using inheritance, for example the first part of an answer I gave on a similar problem using D would work – you’d want to elaborate on the empty structs though if there isn’t any inheritance in play.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written the code like below public class SplashScreen extends BaseActivity{ public void
I have some code that works like the below. I was wondering if its
I have some HTML and CSS code like below <html> <head> <style type=text/css> img.normal
I have a wxPython application with some code like below. I want to set
I have seen some code like below, and it is strange that the __get
What I have below is some notification code. What I would like to achieve
I have code like below to perform some conditional validation on fields in my
i have code like below. Base is the base class and D1, D2, D3
Ok I have some code that boils down to a pattern like this class
I have some code below. This code is a basic push/pop stack class that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.