[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
C++ and octave global variables
From: |
Matteo Bettella |
Subject: |
C++ and octave global variables |
Date: |
Mon, 15 Feb 1999 18:08:47 +0000 (GMT) |
Hi,
this is my cumbersome and not so smart solution
about accessing global workspace values from
dynamically linked C++ functions.
All I could do was to pass the address of an
allocated structure within the arguments.
At the beginning you have to call
addr=setglob ([...values...])
that allocates memory and returns the pointer.
Then, the first time you call the C++ function
getglob you need to pass the pointer.
Hopefully getglob is able to remember it, so
subsequent calls do not require it. See test.m.
The main limit is that the process is not
automatic: every time global variables change,
you need to execute setglob, before calling
getglob.
Hereby, the files.
"make" should compile successfully the two functions.
Then run "test" from Octave.
If anyone has a better idea...
A function like value=GetGlobalVarible(char *theName)
in liboctave might be useful.
bye,
Matteo
------------------------------------------------------
File "mystruc.h"
------------------------------------------------------
#ifndef __MYSTRUCT__
#define __MYSTRUCT__
// the global structure
struct mystruct {
double glob1;
double glob2;
double glob3;
};
typedef struct mystruct mystruct;
#endif
------------------------------------------------------
File "getglob.cc"
------------------------------------------------------
#include <octave/oct.h>
#include "mystruct.h"
#define INFO "g = getglob([m address])\n" \
"\taddress is the value returned by setglob\n" \
"\taddress may be omitted after the first call\n" \
"\tg = m*globals\n\n"
DEFUN_DLD (getglob, args, , INFO)
{
ColumnVector y = args(0).vector_value ();
ColumnVector f(3);
double m=y(0);
// define the global structure
static mystruct *p=NULL;
if (p==NULL) { // it's the first time, cast given address
unsigned long tmp=(unsigned long) y(1);
p = (mystruct *) tmp;
}
// put values in the return vector
f(0) = m*p->glob1;
f(1) = m*p->glob2;
f(2) = m*p->glob3;
return octave_value (f);
}
------------------------------------------------------
File "setglob.cc"
------------------------------------------------------
#include <octave/oct.h>
#include "mystruct.h"
#define INFO "setglob(v)\nv=[var1 var2 var3]\n"
DEFUN_DLD (setglob, args, , INFO)
{
ColumnVector y = args(0).vector_value ();
ColumnVector f(1);
static mystruct *p=NULL;
unsigned long fp;
// if this is the first time, allocate memory
if (p==NULL) {
p=(mystruct*)malloc(sizeof(mystruct));
}
// update the values
p->glob1=y(0);
p->glob2=y(1);
p->glob3=y(2);
// and return the address
fp = (unsigned long) p;
f(0)= fp;
return octave_value (f);
}
------------------------------------------------------
File "Makefile"
------------------------------------------------------
all: setglob.oct getglob.oct
clean:
-rm *.o *.oct
# check to have tab and not spaces before "mkoctfile"...
setglob.oct: setglob.cc mystruct.h Makefile
mkoctfile setglob.cc
getglob.oct: getglob.cc mystruct.h Makefile
mkoctfile getglob.cc
------------------------------------------------------
File "test.m"
------------------------------------------------------
addr = setglob([1 2 3])
getglob([1, addr]) % first time with address
getglob(1)
setglob(-[1 2 3])
getglob(1)
------------------------------------------------------
Matteo Bettella
School of Mechanical Engineering
Automotive Engineering Group
Cranfield University
- C++ and octave global variables,
Matteo Bettella <=