Structure as init argument in a mex file - c++

I have a mask in simulink that has an init argument field. The init argument in my case is a structure. Now I want to use this structure in the .ccp (to make a mex file).
void init()
{
mxArray *initarg = GetInitArg();
...
}
The GetInitArg() is :
#ifndef GET_INIT_ARG
#define GET_INIT_ARG
mxArray *GetInitArg() {
return rtsys->initArg;
}
#endif
When the initarg is an int, I can call it this way in the void init():
int arg = (int)mxGetPr(initarg)[0];
Now, how would I do if initarg is a Matlab structure?
EDIT
I tried using #remus answer.
My struct look like this :
typedef struct
{
const char *task;
aaa_type aaa;
bbb_type bbb;
ccc_type ccc;
} arg_t;
The struct aaa_type, bbb_type and ccc_type are defined like this :
typedef struct
{
double p1;
double p2;
double p3;
double p4;
double p5;
double p6;
} aaa_type;
I try to get the init arg like this :
void init()
{
mxArray *initarg = GetInitArg();
arg_t arg* = (arg_t*)mxGetPr(initarg);
...
}
But at the arg_t line i'm getting two compilation errors:
error C2143: syntax error : missing ';' before '*'
error C2059: syntax error : '='

The list of MEX functions related to accessing structures is below:
mxGetField
mxSetField
mxGetNumberOfFields
mxGetFieldNameByNumber
mxGetFieldNumber
mxGetFieldByNumber
mxSetFieldByNumber
If you have a 1x1 structure, here's how you'd get one of the values:
mxArray *field_name = mxGetField(initArg, 0, "field_name");
Note that the result is another mxArray. From there you want the usual mxGetPr() for double arrays, or other mxGet... for other datatypes.
See the MEX documentation section on C/C++ Matrix Library for API details on these functions: http://www.mathworks.com/help/matlab/cc-mx-matrix-library.html

If you have the structure definition then you could cast the parameter pointer to it (I haven't tested but it should work since the Matlab structure is a contiguous memory block). Let's say you define your structure somewhere in a .h file:
typedef struct {
double a;
double b;
} mystruct_t;
Then:
mystruct_t *arg = (mystruct_t*)mxGetPr(initarg);
And you can access its members:
if (arg->a == 1) // or whatever

Related

Create a pointer that points to a member function that takes a struct as an argument

I have main.cpp file where I defined a struct:
struct values {
string name1;
string name2;
int key ;
} a;
A class named Encrypt defined in a header file Encrypt.h and a file Encrypt.cpp where I define my functions...
Im trying to create a pointer that points to a function that has a struct type as parameter
here's how I did it:
in my header file Encrypt.h
#ifndef Encrypt_h
#define Encrypt_h
#include<iostream>
using namespace std;
class Encrypt {
public:
void print(void *);
};
#endif /* Encrypt_h */
in my Encrypt.cpp :
void Encrypt::print(void * args)
{
struct values *a = args;
string name= a.name1;
cout<<"I'm"<<name<<endl;
};
here's how I tried to create the pointer in main.cpp
void (Encrypt::* pointer_to_print) (void * args) = &Encrypt::print;
THE ERROR I GET :
" Cannot initialize a variable of type 'struct values *' with an lvalue of type 'void *'
"
in Encrypt.cpp
the line :
struct values *a = args;
REMARQUE
Im doing this because I want to pass a function with more than 2 parameters to the function :
pthread_create()
so my function print is just I simplified example.
The problem is that args is of type void* and a is of type values* so you get the mentioned error.
To solve this uou can use static_cast to cast args to values* if you're sure that the cast is allowed:
values *a = static_cast<values*>(args);
Additionally change a.name1 to a->name1.
Demo

Syntax Errors Testing Static Library in C++

I have a very simple setup to try to test a staticLibrary:
I get error Syntax error: Identifier MyDataT, which points me to the function declaration in MyLib.h? Any reason why struct isn't recognized in the second solution?
Here is my pseudo code:
//MSGDefs.h ==> header file only contains struct defs like this:
typedef struct __msg {
unsigned long dest;
unsigned long src;
}MsgT
typedef struct __mydata : public MsgT
{
TimeT time;
DateT date;
}MyDataT;
//======== Library generates staic lib MyLib.lib, this generates library fine=======
//MyLib.h
#include MSGdefs.h
class X{
void process(MyDataT *data);
}
//MyLib.cpp
void X::process(MyDataT *data) { // do processing here ...}
//========================================
//MyTestLibProj.cpp -- Another Solution links to MyLib.lib
#include MyLib.h ==> This causes error of identifier MyDataT ???
int main(){
// X x = new X();
}

How to assign a unique_ptr with a custom deleter

I am trying to pass a pointer to a function that then sets a unique_ptr inside a struct to the pointer passed in. However, I get the following compile error on the last line of the function.
error C2280: 'std::unique_ptr< ALLEGRO_BITMAP,std::default_delete< ALLEGRO_BITMAP>>::unique_ptr(const std::unique_ptr< ALLEGRO_BITMAP,std::default_delete< ALLEGRO_BITMAP>> &)' : attempting to reference a deleted function
c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(1486) : see declaration of 'std::unique_ptr< ALLEGRO_BITMAP,std::default_delete< ALLEGRO_BITMAP>>::unique_ptr'
This diagnostic occurred in the compiler generated function 'Skin::Skin(const Skin &)'
Judging from the errors I believe it has something to do with me adding the delete template for ALLEGRO_BITMAP to namespace std, but I don't know why or how to fix it.
using namespace std;
namespace std {
template<>
class default_delete < ALLEGRO_BITMAP > {
public:
void operator()(ALLEGRO_BITMAP* ptr) {
al_destroy_bitmap(ptr);
}
};
}
typedef struct {
unique_ptr<ALLEGRO_BITMAP> img;
} Skin;
typedef struct {
Skin skins[MAX_ENTITY_COUNT];
} World;
unsigned int createBlock(World world, ALLEGRO_BITMAP* img) {
unsigned int entity = newEntityIndex(world);
world.skins[entity].img = make_unique<ALLEGRO_BITMAP>(img);
return entity;
} // error on this line
Any help is appreciated. Thanks.
In your createBlock function you take World by value which means that it will be copied. However, you can't copy a unique_ptr so that is where your error comes from. This would also mean that setting the unqiue_ptr in the function wouldn't have any effect.
Instead you should take World by reference:
unsigned int createBlock(World& world, ALLEGRO_BITMAP* img) {
unsigned int entity = newEntityIndex(world);
world.skins[entity].img = make_unique<ALLEGRO_BITMAP>(img);
return entity;
}
Note that the same is true for the call to newEntityIndex and that the arguments to make_unique will be passed to the ALLEGRO_BITMAP constructor.
So what you probably want is:
world.skins[entity].img.reset(img);

Error in C++ : redefinition of class constructor

I've encountered these two error when trying to compile..
anyone knows whats wrong ?
Was thinking maybe I #include the wrong header file ?
the sample of the codes and error as per following:
Error:
Square.cpp:8: error: redefinition of ‘Square::Square(bool, Point*, std::string, int)’
Square.h:21: error: ‘Square::Square(bool, Point*, std::string, int)’ previously defined here
Square.cpp: In member function ‘Point Square::getCoord()’:
Square.cpp:22: error: expected primary-expression before ‘]’ token
Square.cpp: In member function ‘void Square::setCoord(Point*)’:
Square.cpp:32: error: expected primary-expression before ‘]’ token
Square.cpp:32: error: expected primary-expression before ‘]’ token
cpp file
#include "Square.h"`
#include <cmath>
using namespace std;
Square::Square(bool containsWarpSpace, Point coord[], string shapeName, int vertPoint):ShapeTwoD(shapeName, containsWarpSpace) {
vertPoint = vertPoint;
coord[] = coord[];
}
int Square::getVertPoint()
{
return vertPoint;
}
Point Square::getCoord()
{
return coord[];
}
void Square::setVertPoint(int verticleP)
{
vertPoint = verticleP;
}
void Square::setCoord(Point coord[])
{
coord[] = coord[];
}
header:
#include "ShapeTwoD.h"
class Square : public ShapeTwoD
{
private:
int vertPoint;
Point coord[];
public:
//Accessor
int getVertPoint();
Point getCoord();
//Mutator
void setVertPoint(int vertP);
void setCoord(Point coord[]);
//virtual member
virtual double computeArea(Point x, Point y);
Square(bool containsWarpSpace, Point coord[], std::string shapeName = "Square", int vertPoint = 4):ShapeTwoD(shapeName, containsWarpSpace){}
};
You are defining the constructor twice, once in the header and once in the implementation file. In the header, you just need to declare it like this:
Square(bool containsWarpSpace,
Point coord[],
std::string shapeName = "Square",
int vertPoint = 4);
You also need to fix the handling of coord, maybe something like changing coord to
Point* coord;
and use
Point* Square::getCoord()
{
return coord;
}
and
this->coord = coord;
in the constructor and setCoord().
Please note that your way of handling coord seems strange and dangerous to me, but without further information about what you are actually trying to do it's hard to give specific advise. Generally, consider using the standard containers over manual memory/array management.
The compiler clearly tells you the problem:
You defined the constructor twice once in header file and once in cpp file.
Also, What exactly do you intend to do with:
coord[] = coord[];
You should understand each and every statement of code that you write. Think about, What do you intend this statement to do? & then match it to the language grammar that you learnt.
Source File:
Square::Square(bool containsWarpSpace, Point coord[],
string shapeName, int vertPoint)
:ShapeTwoD(shapeName, containsWarpSpace)
{
vertPoint = vertPoint;
coord[] = coord[];
}
Header File:
Square(bool containsWarpSpace, Point coord[],
std::string shapeName = "Square", int vertPoint = 4)
:ShapeTwoD(shapeName, containsWarpSpace)
{}
Looks like two different version of the same function.
The one in the header file calls the base class constructor but does not have any code in the body of the constructor.

Uint8 - Missing Type specifier

I'm trying to write a singleton class to hold the state of inputs from the user (mouse/keyboard data). The SDL API returns keyboard data as Uint8 pointer array, however, why I try to create the Uint8 pointer, I get these errors at the line w/ the uint8:
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
I've used Uint8 as a data type without defining it before, so I'm not sure what is causing the issue here. Here is my code:
class InputState {
public:
InputState()
{};
~InputState()
{};
static InputState *getInputState(void)
{
static InputState *state = new InputState();
return state;
};
public:
Uint8 *keys;
struct MouseState
{
int LeftButtonDown;
int RightButtonDown;
int MiddleButtonDown;
int x;
int y;
MouseState ()
{
LeftButtonDown = 0;
RightButtonDown = 0;
MiddleButtonDown = 0;
x = 0;
y = 0;
}
};
MouseState *mouseState;
};
The type Uint8 is a typedef that is defined in one of the SDL header. If you want to use it, you need to include the SDL.h header in your file.
// You need this include if you want to use SDL typedefs
#include <SDL.h>
class InputState {
public:
InputState()
{};
~InputState()
{};
// ...
public:
Uint8 *keys;
// ...
};