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 799863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:12:00+00:00 2026-05-14T23:12:00+00:00

I have written some physics simulation code in C++ and parsing the input text

  • 0

I have written some physics simulation code in C++ and parsing the input text files is a bottleneck of it. As one of the input parameters, the user has to specify a math function which will be evaluated many times at run-time. The C++ code has some pre-defined function classes for this (they are actually quite complex on the math side) and some limited parsing capability but I am not satisfied with this construction at all.

What I need is that both the algorithm and the function evaluation remain speedy, so it is advantageous to keep them both as compiled code (and preferrably, the math functions as C++ function objects). However I thought of glueing the whole simulation together with Python: the user could specify the input parameters in a Python script, while also implementing storage, visualization of the results (matplotlib) and GUI, too, in Python.

I know that most of the time, exposing C++ classes can be done, e.g. with SWIG but I still have a question concerning the parsing of the user defined math function in Python:

Is it possible to somehow to construct a C++ function object in Python and pass it to the C++ algorithm?
E.g. when I call

f = WrappedCPPGaussianFunctionClass(sigma=0.5)
WrappedCPPAlgorithm(f)

in Python, it would return a pointer to a C++ object which would then be passed to a C++ routine requiring such a pointer, or something similar… (don’t ask me about memory management in this case, though :S)

The point is that no callback should be made to Python code in the algorithm. Later I would like to extend this example to also do some simple expression parsing on the Python side, such as sum or product of functions, and return some compound, parse-tree like C++ object but let’s stay at the basics for now.

Sorry for the long post and thx for the suggestions in advance.

  • 1 1 Answer
  • 2 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-14T23:12:01+00:00Added an answer on May 14, 2026 at 11:12 pm

    I do things similar to this all the time. The simplest solution, and the one I usually pick because, if nothing else, I’m lazy, is to flatten your API to a C-like API and then just pass pointers to and from Python (or your other language of choice).

    First create your classes

    class MyFunctionClass
    {
      public:
        MyFunctionClass(int Param)
        ...
    };
    
    class MyAlgorithmClass
    {
      public:
        MyAlgorithmClass(myfunctionclass& Func)
        ...
    };
    

    Then create a C-style api of functions that creates and destroys those classes. I usually flatted in out to pass void* around becuase the languages I use don’t keep type safety anyway. It’s just easier that way. Just make sure to cast back to the right type before you actually use the void*

        void* CreateFunction(int Param)
        {
          return new MyFunctionClass(Param);
        }
    
        void DeleteFunction(void* pFunc)
        {
            if (pFunc)
                delete (MyFunctionClass*)pFunc;
        }
    
        void* CreateAlgorithm(void* pFunc)
        {
          return new MyAlgorithmClass(*(MyFunctionClass*)pFunc)
        }
    
        void DelteAlgorithm(void* pAlg)
        {
           if (pAlg)
               delete (MyAlgorithmClass*)pAlg;
        }
    

    No all you need to do is make python call those C-style function. In fact, they can (and probably should) be extern “c” functions to make the linking that much easier.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why don't you have a wrapper and still make it… May 15, 2026 at 7:37 am
  • Editorial Team
    Editorial Team added an answer So, what does it show then if not the labels… May 15, 2026 at 7:37 am
  • Editorial Team
    Editorial Team added an answer Most computers these days use two's complement for signed integers,… May 15, 2026 at 7:37 am

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.