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

  • SEARCH
  • Home
  • 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 661649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:17:26+00:00 2026-05-13T23:17:26+00:00

In a pure C++ world we can generate interfacing or glue code between different

  • 0

In a pure C++ world we can generate interfacing or glue code between different components or interfaces at compile time, using a combination of template-based compile-time and runtime-techniques (to e.g. mostly automatically marshall to/from calls using legacy types).

When having to interface C++ applications with Objective-C/Cocoa for GUI, system integration or IPC though, things become harder due to the less strict typing – yet often not more then a flat repitive interface layer is needed: thin bridging delegates have to be defined or conversion code to language bridging calls has to be written.

If you have to deal with interfaces of non-trivial size and want to avoid script-based code generation this quickly becomes cumbersome and is just a pain every time refactorings have to take place. Using a combination of (template) metaprogramming and the Objective-C runtime library, it should be possible to reduce the amount of code considerably…

Before i go to reinvent the wheel (and possibly waste time), does anyone know about techniques, best-practices or examples in that direction?


As for an example, lets say we need a delegate that supports this informal protocol:

- (NSString*)concatString:(NSString*)s1 withString:(NSString*)s2;
- (NSNumber*)     indexOf:(CustomClass*)obj;

Instead of implementing an Obj-C class now that explicitly bridges to a C++-instance, i’d like to do something like this instead:

class CppObj {
    ObjcDelegate m_del;
public:
    CppObj() : m_del(this) 
    {
        m_del.addHandler
            <NSString* (NSString*, NSString*)>
            ("concatString", &CppObj::concat);
        m_del.addHandler
            <NSNumber* (CustomClass*)>
            ("indexOf", &CppObj::indexOf);
    }

    std::string concat(const std::string& s1, const std::string& s2) {
        return s1.append(s2);
    }

    size_t indexOf(const ConvertedCustomClass& obj) {
        return 42;
    }
};

All that should be needed from the user to support additional types would be to specialize a conversion template function:

template<class To, class From> To convert(const From&);

template<> 
NSString* convert<NSString*, std::string>(const std::string& s) { 
    // ...
}

// ...

The example above of course does ignore support for formal protocols etc. but should get the point across. Also, due to the type-information for Objc-runtime-types being mostly decayed into some-native-types or class-type i don’t think the explicit specification of parameter and return types for the delegate-methods can be avoided.

  • 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-05-13T23:17:26+00:00Added an answer on May 13, 2026 at 11:17 pm

    I didn’t find anything satisfactory and came up with a prototype that, given the following informal protocol:

    - (NSString*)concatString:(NSString*)s1 withString:(NSString*)s2;
    

    and this C++ code:

    struct CppClass {
        std::string concatStrings(const std::string& s1, const std::string& s2) const {
            return s1+s2;
        }
    };
    
    std::string concatStrings(const std::string& s1, const std::string& s2) {
        return s1+s2;
    }
    

    allows creating and passing a delegate:

    CppClass cpp;
    og::ObjcClass objc("MyGlueClass");
    objc.add_handler<NSString* (NSString*, NSString*)>
        ("concatString:withString:", &cpp, &CppClass::concatStrings);
    // or using a free function:
    objc.add_handler<NSString* (NSString*, NSString*)>
        ("concatString:withString:", &concatStrings);
    [someInstance setDelegate:objc.get_instance()];
    

    which can then be used:

    NSString* result = [delegate concatString:@"abc" withString:@"def"];
    assert([result compare:@"abcdef"] == NSOrderedSame);
    

    Boost.Function objects can also be passed, which means Boost.Bind can easily be used as well.

    While the basic idea works, this is still a prototype. I did a short blog post on the subject and the prototype source is available via bitbucket. Constructive input and ideas welcome.

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

Sidebar

Ask A Question

Stats

  • Questions 384k
  • Answers 384k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From the mysql docs: If you insert multiple rows using… May 14, 2026 at 11:18 pm
  • Editorial Team
    Editorial Team added an answer You could just call the command line C++ compiler (cl.exe)… May 14, 2026 at 11:18 pm
  • Editorial Team
    Editorial Team added an answer I found the answer: chartArea.AxisX.MajorGrid.Interval = 1; chartArea.AxisX.MajorTickMark.Interval = 1;… May 14, 2026 at 11:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.