title: SWIG call function pointers stored within struct
body: <p>I have a struct as follows:</p>
  
  <pre><code>struct power_model {
      int64_t (*estimate_energy)(statistics *stats, statistics *scaled_stats, parameters *from, parameters *to, energy_container *energy_container);
      int64_t (*estimate_performance)(statistics *stats, parameters *params);
      uint32_t (*freq_to_volt)(uint32_t freq);
  };
  </code></pre>
  
  <p>There are multiple power models that my code contains.
  I would like to wrap these models with SWIG and expose them to Python so that I can run my unit tests.</p>
  
  <p>While the SWIG documentation talks about exposing function pointers, it does not talk about function pointers contained within structs.
  I tried to encapsulate the calls in my interface file</p>
  
  <pre><code>%{
  #include "models.h"
  %}
  
  %include "models.h"
  
  %extend power_model {
    %pythoncallback;
    int64_t (*estimate_energy)(statistics *stats, statistics *scaled_stats, parameters *from, parameters *to, energy_container *energy_container);
    int64_t (*estimate_performance)(statistics *stats, parameters *params);
    uint32_t (*freq_to_volt)(uint32_t freq);
    %nopythoncallback;
  }
  </code></pre>
  
  <p>I also tried prefixing the field names with <code>%constant</code>.</p>
  
  <p>With these approaches, I always end up with the same error: </p>
  
  <pre><code>In [3]: model.estimate_energy()
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-3-b2e3ace2fc9b&gt; in &lt;module&gt;()
  ----&gt; 1 model.estimate_energy()
  
  TypeError: 'SwigPyObject' object is not callable
  </code></pre>
  
  <p>How can I call the underlying functions referenced by the function pointers contained within <code>struct power_model</code>?</p>
  
  <p><strong>Edit</strong>:
  To elaborate on my setup, I am also sources of two additional files to better explain the setup I'm trying to achieve with the <code>power_model</code> interface.</p>
  
  <p><strong>nexus5.c</strong></p>
  
  <pre><code>static int64_t estimate_energy(statistics *stats, statistics *scaled_stats, parameters *from, parameters *to, energy_container *energy) {
      ...
  }
  static int64_t estimate_performance(statistics *stats, parameters *params) {
      ...
  }
  static uint32_t freq_to_volt(uint32_t freq) {
      ...
  }
  
  struct power_model nexus5_power_model = {
       .estimate_energy = estimate_energy,
       .estimate_performance = estimate_performance,
       .freq_to_volt = freq_to_volt,
  };
  </code></pre>
  
  <p><strong>galaxy_s.c</strong></p>
  
  <pre><code>static int64_t estimate_energy(statistics *stats, statistics *scaled_stats, parameters *from, parameters *to, energy_container *energy) {
      ...
  }
  static int64_t estimate_performance(statistics *stats, parameters *params) {
      ...
  }
  static uint32_t freq_to_volt(uint32_t freq) {
      ...
  }
  
  struct power_model galaxy_s_power_model = {
       .estimate_energy = estimate_energy,
       .estimate_performance = estimate_performance,
       .freq_to_volt = freq_to_volt,
  };
  </code></pre>