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 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

Related Questions

I have created some Python code which is really complicated but it basically asks
I have this code to create an interface and some buttons (python in maya)
I have some code using libxml2's SAX2 interface. I want to be able to
I have some code which takes strings representing hexadecimal numbers - hex colors, actually
I have a Python script which uses the MySQLdb interface to load various CSV
I have some python module, which has a class ModuleClass and I can't modify
I have the following setup: GDAL library with Python bindings (SWIG) Some glue code
I have some code similar to the following: public interface IMyClass { MyEnum Value
I am still very new to python, but I need to interface with some
I have a collection of variables in Python, some of which are normal Python

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.