How to Rename SWIG Generated Proxy Java classes created from C Structures - swig

I have a few C structures like below that are generated by SWIG into sample_struct_t.java since the C function declares it as sample_struct_t. What would I need to add to the SWIG interface file to generate the sample_struct_t structure as Sample.java?
typedef struct sample_struct_t_ {
char *sample;
uint8_t example;
ios_boolean remove;
} sample_struct_t;

You need to use %rename with the non-typedef'd (i.e. original) name, before this is first seen:
%module test
%rename (Sample) sample_struct_t_;
typedef struct sample_struct_t_ {
char *sample;
uint8_t example;
ios_boolean remove;
} sample_struct_t;

Related

passing structure to a file in c

I've seen some answers on StackOverflow regarding this. In my c project, I am using the main function and a child function. The child function is written in a separate .c file, with its header file included in the main file.
#include<stdio.h>
struct student
{
int t1;
float e1;
};
typedef struct student stu;
#include"struct_demo.h"
void main()
{
stu s1;
stu *r1=&s1;
s1.t1=10;
s1.e1=172.1;
struct_demo(r1);
}
And the function struct_demo.c is as follows
#include"stdio.h"
void struct_demo(stu *s1)
{
s1->e1=9;
printf("%d",s1->e1);
}
The header file for the function struct_demo is
#ifndef STRUCT_DEMO_H_
#define STRUCT_DEMO_H_
void struct_demo(stu *s1);
#endif /* STRUCT_DEMO_H_ */
My compiler is showing errors in the child function
expression must have pointer-to-struct-or-union type
identifier stu is undefined
The same program when executed without the use of separate .c files (with functions written in a single .c file under separate function) works. Where am I going wrong?
struct student
{
int t1;
float e1;
};
typedef struct student stu;
Move this code struct_demo.h since the struct_demo.h doesn't know what is stu
for parsing the address you need to do
myStructure* myFunction(structure* myStructure)
{
//some opperation
return myStructure;
}
this way you are parsing the address of that structure but be careful when initializing it you will have to use calloc to make room in memory for it because at this point that pointer can go anywhere.

How to change the default code generated by SWIG for the allocation of memory for a C structure?

I am using a flexible array in the structure. So I want to change the memory allocated for that structure with some of my own code. Basically I want to change the new_structname() and structname_variable_set() functions.
typedef struct vector{
int x;
char y;
int arr[0];
} vector;
here, SWIG generated new_vector() function to allocate memory by calling calloc(1,sizeof(struct vector)) where swig will not handle these type of structure in a special manner. So we need to modify the swig generated new_vector() in order to allocate memory for the flexible array. So is there any way to handle this?
There are a few ways you can do this. What you're looking for though is %extend. That lets us define new constructors and implement them as we see fit. (It even works with a C compiler, they're only constructors from the perspective of the target language).
Using your vector as a starting point we can illustrate this:
%module test
%include <stdint.i>
%inline %{
typedef struct vector{ int x; char y; int arr[0]; }vector;
%}
%extend vector {
vector(const size_t len) {
vector *v = calloc(1, sizeof *v + len);
v->x = len;
return v;
}
}
With this SWIG synthesises a new_vector function in the generated module code as you'd hoped.
I also assumed that you want to record the length inside the struct as one of its members. If that's not the case you can simply delete the assignment I made.

What's the correct syntax for a template class member fucntion to return a struct type?

In Storing C++ template function definitions in a .CPP file one can learn how to store a template implementation in .cpp. However I failed to do it if the return type is a struct which is defined inside the class. See,
template<typename T>
class C1
{
public:
struct s {
int x;
};
s GetS();
private:
s m_sInt;
};
in its .cpp, the below code will generate syntax errors.
template<typename T>
C1<T>::s C1<T>::GetS()
{
return m_sInt;
}
Wonder what the right syntax should be in this case.
template<typename T>
typename C1<T>::s C1<T>::GetS()
{
return m_sInt;
}
The behaviour of compiler is explained in other questions about nested templates with dependent scope, e.g. Nested templates with dependent scope or Dependent scope and nested templates

Use SWIG to apply multiple Java data types for same C data type

I have two C functions that I'm exposing through SWIG to my Java layer and both have an input param with a const void * data type ("val) that needs to be a uint8_t for the addCategory function but a char for the addAttribute function. I'm currently, in the SWIG Interface file, using the %apply to map the const void * C type to a short on the Java side. Is there a way to modify the SWIG interface file to support both a char (String) and a uint8_t (short) for the const void * input parameter?
C Functions from header file:
int
addCategory(query_t *query, type_t type, const void *val);
int
addAttribute(query_t *query, type_t type, const void *val);
SWIG Interface File:
%module Example
%include "stdint.i"
void setPhy_idx(uint32_t value);
%include "arrays_java.i"
void setId(unsigned char *value);
%{
#include "Example.h"
%}
%apply char * { unsigned char * };
%apply char * { void * };
%apply uint8_t { const void * }
%apply int32_t { int32_t * }
%include "Example.h"
You can't directly do this - what type would be used in this place in Java? You need to help SWIG decide that in some way.
You have (at least) three possible solutions:
Use a type hierarchy - The base type will be what the function takes, the subclasses will get wrapped also. You could do this on the C++ side, or on the Java side using SWIG's typemap facilities. I think this is needlessly complicated though, so I've not made an example here.
Use overloads (or even different functions, with different names altogether - you could use %rename to make them back into overloads in Java even if they have different names in C)
Use a union. This will get wrapped with set and get functions by SWIG:
%module test
union values {
unsigned char *string;
void *generic;
uint8_t someOtherThing;
uint32_t number;
};
void func(values v);
This results in a Java class called values, which func() takes and can pass one of the members of the union through. Clearly you'd want to %apply appropriate typemaps for the members of the union.

Cython recursive struct declarations

I'm trying to use a C struct in Cython, that defines a linked list:
typedef struct {
struct query_result* next_result;
char* result;
} query_result;
As you can see I'm using the query_result type inside its own definition.
Using this as is, in Cython gives me compiler errors:
cdef extern from 'c_wrapper.h':
struct query_result:
struct query_result*
char*
Any ideas about how to properly handle this recursive definition in Cython?
You shouldn't use the struct keyword when you are referring to the type:
cdef extern from 'c_wrapper.h':
struct query_result:
query_result* more
char* data