Conversion in swift (id)initWithPreset:(int [])preset - iphone

Can u convert - (id)initWithPreset:(int [])preset;
in Swift
int *type = nil;
if (_selectedIndex == 0) {
type = PRESET_PHONE;
}
extern int PRESET_FM[];
extern int PRESET_CD[];
extern int PRESET_STUDIO[];
extern int PRESET_VOICE[];
extern int PRESET_PHONE[];
extern int PRESET_TAPE[];
extern int PRESET_HIFI[];
In Objective c
MP3Converter *mp3Converter = [[MP3Converter alloc] initWithPreset:type];
But I am using in Swift
var mp3Converter : MP3Converter!
mp3Converter = MP3Converter(preset:PRESET_VOICE )
How we give there preset:PRESET_VOICE?. It gives the error Unsafe Immutable Pointer..

The problem seems to be the way you have defined the constant arrays.
You will need to define them as integer pointers, than arrays. Check:
//const int PRESET_FM[] = {1, 2, 4, 5}; //<< This won't get exported to swift
const int * PRESET_FM = {1, 2, 4, 5}; //Swift can see this fine
And in your swift code, you will need to properly typecast this C constant to UnsafeMutablePointer as Swift will see the C pointers so.
var mp3Converter : mp3Converter = MP3Converter(preset:UnsafeMutablePointer<Int32>( PRESET_FM)(PRESET_VOICE))
Also ensure the objective C interface to use int * than int[]:
- (id)initWithPreset:(int *)preset
HTH.

Related

Arduino C++ classes needing each other

Here an example relating to my problem:
class classA
{
private:
int Value = 100;
public:
int get_Value()
{
return Value;
}
void set_Value(int p)
{
if (p == 0) Value = B.get_Value();
else Value = 5;
}
};
classA A = classA();
class classB
{
private:
int Value = 200;
public:
int get_Value()
{
return Value;
}
void set_Value(int p)
{
if (p == 0) Value = A.get_Value();
else Value = 5;
}
};
classB B = classB();
The problem is that class A can't access class B because it's definition is below it's own. My question is how can I define it so that class A has access to class B.
I tried to something like class classB; or class classB; classB B = classB; before classA begins.
Because I'm relatively new to programming I don't know how to solve such (probably easy) problems. Hope for some help!
This is a problem with circular dependencies. Luckily this one is pretty easy to fix: Just put the declaration and definition into separate files.
ClassA.h:
// Include guards to prevent double includes
#ifndef CLASS_A_H
#define CLASS_A_H
class classA
{
private:
int Value = 100;
public:
// Here we only declare, but not define the functions
int get_Value();
void set_Value(int p);
};
#endif // CLASS_A_H
ClassB.h:
// Include guards to prevent double includes
#ifndef CLASS_B_H
#define CLASS_B_H
class classB
{
private:
int Value = 200;
public:
// Here we only declare, but not define the functions
int get_Value();
void set_Value(int p);
};
#endif // CLASS_B_H
And now we need to actually define the functions, but in different files
ClassA.cpp:
#include "A.h"
#include "B.h"
// Use extern to tell the compiler that this declaration can be
// found in any of the other files and it will not complain as
// long as it is found somewhere
extern classB B;
int classA::get_Value()
{
return Value;
}
void classA::set_Value(int p)
{
if (p == 0) Value = B.get_Value();
else Value = 5;
}
ClassB.cpp:
#include "A.h"
#include "B.h"
// Use extern to tell the compiler that this declaration can be
// found in any of the other files and it will not complain as
// long as it is found somewhere
extern classA A;
int classB::get_Value()
{
return Value;
}
void classB::set_Value(int p)
{
if (p == 0) Value = A.get_Value();
else Value = 5;
}
Then, in your main file just include the two headers and you're good to go:
#include "A.h"
#include "B.h"
classB B = classB();
classA A = classA();
By the way, when you're on Arduino you can just create new .h and .cpp files and put them next to the .ino file and it should be found. You can even put them in folders, but then the include paths must be relative to each files, for example: #include "../include/file.h"

How to define the operator= in a class to make a variable in it to be assigned to an outside variable

I have example code and classs:
class a{
int x;
a(){
this->x = 335; /* example number*/
}
public:
void operator=(int);
};
void a::operator=(int source){
this->x = source;
}
main(){
int i = 100;
a example_class;
example_class = i; //works fine!
i = example_class; /*this is what I want to do.*/
}
the problem with this hole thing is that I can't
make the operator= be a friend function
therefore the command: "i = example_class"
can't be done because I can't create a function in //the the int class like I normally would with my own classes.
Finally:
How can I complete the command:
"i = example_class" when the
operator= can't have more than 1
parameter?
notes:
I know the code doesn't do anything.
And is only an example. The point
Is what actually matters.
Also, I need to make it clear that I
cannot create any functions in the
Target class(in this case int). Only in
the source class(in this case a).
I also want to make clear that I know
that it's impossible to declare the
operator= as a friend function.
I know that I could just create a function
to get a reference to int x or make
int x public but I didn't want to do that
because the real code involves complex
functions for converting between types
so it's vary important to me to be able
to write: "i = example_class;".
Thanks,
Ronen.
Working example.
#include <iostream>
class a {
int x = 355;
public:
void operator=(int);
operator int();
};
void a::operator=(int source){
x = source;
}
a::operator int() {
return x;
}
int main(int, char**) {
int i = 100;
a example_class;
example_class = i;
int j = example_class;
std::cout << j << std::endl;
}

MATLAB input struct with unsigned char into MEX file

I tried to input this struct from MATLAB into my MEX file: struct('speed',{100.3},'nr',{55.4},'on',{54}), but the last value which is defined in my MEX file as unsigned char reads out as zero before calling my C function? The two double values works like intended.
struct post_TAG
{
double speed;
double nr;
unsigned char on;
};
const char *keys[] = { "speed", "nr", "on" };
void testmex(post_TAG *post)
{
...
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
post_TAG post;
int numFields, i;
const char *fnames[3];
mxArray *tmp;
double *a,*b;
unsigned char *c;
numFields=mxGetNumberOfFields(prhs[0]);
for(i=0;i<numFields;i++)
fnames[i] = mxGetFieldNameByNumber(prhs[0],i);
tmp = mxGetField(prhs[0],0,fnames[0]);
a=(double*)mxGetData(tmp);
tmp = mxGetField(prhs[0],0,fnames[1]);
b=(double*)mxGetData(tmp);
tmp = mxGetField(prhs[0],0,fnames[2]);
c=(unsigned char*)mxGetData(tmp);
mexPrintf("POST0, speed=%f, nr=%f, on=%u\n",*a,*b,*c);
post.speed = *a;
post.nr = *b;
post.on = *c;
testmex(&post);
}
In a struct defined as struct('speed',{100.3},'nr',{55.4},'on',{54}), the field on is a double. Pass as a uint8 from MATLAB:
struct('speed',{100.3},'nr',{55.4},...
'on',{uint8(54)}),
Any numeric value without a specified type in MATLAB is a double.
Also note that for reading a scalar value, the problem is simplified somewhat by mxGetScalar. It will return one double value for any underlying data type.
unsigned char s = (unsigned char) mxGetScalar(...); // cast a double to unsigned char

Returning structs inside a class

I am converting my application to an OOP type but I am having problem with helper functions. What I did was place helper functions inside the class as private. How should I declare a function "addSection" to return a struct
This is for my .cpp file
section_t XIniFile::*addSection(ini_file_t *n, char *d)
{
section_t *s = (section_t *)malloc(sizeof(section_t *);
//add sections
return s;
}
This is for my .h file
class XIniFile
{
public:
int open(const char *);
int readString(const char *, const char *, char *);
int readInt(const char *, const char *, int *);
int writeString(const char *, const char *, char *);
int writeInt(const char *, const char *, int *);
int close();
private:
typedef struct key_tag {
char *name;
char *value;
key_tag *next;
} key_tag_t;
typedef struct sections_tag {
char *name;
sections_tag *next;
key_tag_t *keys;
} section_t;
typedef struct {
char *name;
section_t *sections;
int modified;
} ini_file_t;
section_t *add_section(ini_file_t *, char *);
key_tag_t *add_key(section_t *, char *, char *);
};
There are a lot of errors but I would like to start first from the first error which is error: ‘section_t’ does not name a type
turn add_section into:
XIniFile::section_t * XIniFile::add_section(ini_file_t *n, char *d)
since section_t is a member of XIniFile
Your struct section_t and other structs need to be declared above the functions that use them, like add_section. C++ type declarations are forward visible, meaning their visibility extend from the point they are first declared onward, but not behind. Order matters, it is an inherited trait of the C language, based on "textual" treatment of include files as opposed to true modules or multi file type metadata, where the compiler traditionally notes each type as it is declared syntactically, stores it in a symbol table, where it is immediately available.
This differs from Java or C# compilers which parse the whole source file first, then resolve types and symbols later, removing order from the equation.
Another error, your * is in the wrong place, move it to directly after section_t:
section_t XIniFile::*addSection(ini_file_t *n, char *d)
Change to:
XIniFile::section_t* XIniFile::addSection(ini_file_t *n, char *d)
Also, in C++ you don't have to use typedef for structs as in C. You can simply say:
struct key_tag_t {
char *name;
char *value;
key_tag_t *next;
};
struct section_t {
char *name;
sections_tag *next;
key_tag_t *keys;
};
section_t *add_section(ini_file_t *, char *);
key_tag_t *add_key(section_t *, char *, char *);
structs and classes are implicitly "typedefed" in C++, though there is a minor difference, it isn't relevant.

member function as callback

I would like to pass a member function of an instantiated object to another function. Example code is below. I am open for any strategy that works, including calling functional() from another function inside memberfuncpointertestclass using something like lambda or std::bind. Please note that I did not understand most of the threads I found with google about lambda or std::bind, so please, if possible, keep it simple. Also note that my cluster does not have C++ 11 and I would like to keep functional() as simple as it is. Thank you!
int functional( int (*testmem)(int arg) )
{
int a = 4;
int b = testmem(a);
return b;
}
class memberfuncpointertestclass
{
public:
int parm;
int member( int arg )
{
return(arg + parm);
}
};
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
int (*testf)(int) = &a.member;
std::cout << functional(testf);
}
int main()
{
funcpointertest();
return 0;
}
You cannot invoke a method on an object without an instance to refer to. So, you need to pass in both the instance as well as the method you want to invoke.
Try changing functional to:
template <typename T, typename M>
int functional(T *obj, M method)
{
int a = 4;
int b = (obj->*(method))(a);
return b;
}
And your funcpointertest to:
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
std::cout << functional(&a, &memberfuncpointertestclass::member);
}
This is a job for std::function, a polymorphic function wrapper. Pass to functional(...) such a function object:
#include <functional>
typedef std::tr1::function<int(int)> CallbackFunction;
int functional(CallbackFunction testmem)
{
int a = 4;
int b = testmem(a);
return b;
}
then use std::bind to create a function object of the same type that wraps memberfuncpointertestclass::method() of instance a:
void funcpointertest()
{
memberfuncpointertestclass a;
a.parm = 3;
CallbackFunction testf = std::bind(&memberfuncpointertestclass::member, &a, std::placeholders::_1);
std::cout << functional(testf);
}
Check this item for more details.