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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:54:30+00:00 2026-05-15T12:54:30+00:00

I have some code to interface Python to C++ which works fine but every

  • 0

I have some code to interface Python to C++ which works fine but every time I look at it I think there must be a better way to do it. On the C++ side there is a ‘variant’ type that can deal with a fixed range of basic types – int, real, string, vector of variants, etc. I have some code using the Python API to convert from the equivalent Python types. It looks something like this:

variant makeVariant(PyObject* value)
{  
  if (PyString_Check(value)) {
    return PyString_AsString(value);
  }
  else if (value == Py_None) {
    return variant();
  }
  else if (PyBool_Check(value)) {
    return value == Py_True;
  }
  else if (PyInt_Check(value)) {
    return PyInt_AsLong(value);
  }
  else if (PyFloat_Check(value)) {
    return PyFloat_AsDouble(value);
  }
  // ... etc

The problem is the chained if-else ifs. It seems to be calling out for a switch statement, or a table or map of creation functions which is keyed by a type identifier. In other words I want to be able to write something like:

  return createFunMap[typeID(value)](value);

Based on a skim of the API docs it wasn’t obvious what the best way is to get the ‘typeID’ here directly. I see I can do something like this:

  PyTypeObject* type = value->ob_type;

This apparently gets me quickly to the type information but what is the cleanest way to use that to relate to the limited set of types I am interested in?

  • 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-15T12:54:30+00:00Added an answer on May 15, 2026 at 12:54 pm

    In a way, I think you’ve answered your own question.

    Somewhere, you’re going to have to select functionality based on data. The way to do this in C is to use function pointers.

    Create a map of object_type->function mappers… where each function has a clearly-defined interface.

    variant PyBoolToVariant(PyObject *value) {
        return value == Py_True;
    }
    
    Map<PyTypeObject*,variant* (PyObject*)> function_map;
    
    function_map.add(PyBool, PyBoolToVariant);
    

    Now your makeVariant can look like this.

    variant makeVariant(PyObject *value) {
        return (*function_map.get(value->ob_type))(value);
    }
    

    The hard part is going to be getting the syntax right for the Map object. Also, I’m assuming there is a Map object you can use that takes type parameters (<PyTypeObject*, variant*(PyObject*)).

    I probably have not quite gotten the syntax correct for the second type of the Map. It should be a pointer to a function which takes one pointer and returns a pointer to a variant.

    I hope this is helpful.

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

Sidebar

Ask A Question

Stats

  • Questions 489k
  • Answers 489k
  • 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 If you use a dot matrix printer in text mode,… May 16, 2026 at 8:55 am
  • Editorial Team
    Editorial Team added an answer I found a solution that worked, though it may not… May 16, 2026 at 8:55 am
  • Editorial Team
    Editorial Team added an answer Have a look at the GD library functions: http://www.php.net/manual/en/ref.image.php EDIT:… May 16, 2026 at 8:55 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

Related Questions

No related questions found

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.