jplus-0.4.7
jengine Class Reference

Interface to dynamically loaded J engine. More...

#include <src/jengine.h>

Inheritance diagram for jengine:

Public Types

typedef jarray(* monad) (jarray y)
 Pointer to function, implementing monadic variant of a verb.
 
typedef jarray(* dyad) (jarray x, jarray y)
 Pointer to function, implementing dyadic variant of a verb.
 
typedef jarray(* amonad) (monad um, dyad ud, jarray y)
 Pointer to function, implementing monadic variant of an adverb.
 
typedef jarray(* adyad) (monad um, dyad ud, jarray x, jarray y)
 Pointer to function, implementing dyadic variant of an adverb.
 

Public Member Functions

 jengine ()
 Initializes J engine.
 
 ~jengine ()
 Frees J engine memory.
 
bool doJ (const std::string s)
 Executes J sentence.
 
const jarray get (const std::string name)
 Retrieves a named array from J.
 
bool set (const std::string name, jarray &value)
 Assigns J name to the specified array.
 
int getError ()
 J error code for the last unsuccessful operation.
 
bool ok ()
 Conveniently check that there was no error.
 
bool defVerb (std::string name, monad mf, dyad df, int mr=RMAX, int lr=RMAX, int rr=RMAX)
 Defines J verb, calling one (or both) of specified C++ functions.
 
bool defAdverb (std::string name, amonad mf, adyad df)
 defines J adverb, calling one (or both) of specified C++ functions.
 
bool defScript (std::string name, int type, std::string code, int mr=jengine::RMAX, int lr=jengine::RMAX, int rr=jengine::RMAX)
 Defines a J script, given by a (possibly multi-line) string.
 
bool isBuiltin (std::string name) const
 Checks if given name was defined via defScript.
 
std::set< std::string > getBuiltins () const
 Returns set of all names defined via defScript.
 
jarray::I PROLOG ()
 Returns the top of J garbage collection stack.
 
jarray::I EPILOG (jarray::I oldtop)
 Frees all memory, allocated since ttop was recorded.
 
voidEPILOG (jarray::I oldtop, void *hdr)
 Frees all memory, allocated since ttop was recorded.
 

Static Public Member Functions

static void initJlibrary (std::ostream &)
 Loads and links J dynamic library, must be called once before this class is instantiated.
 

Static Public Attributes

static const int RMAX =10000
 Constant to denote the infinite rank.
 

Protected Member Functions

voidGA (const jarray::I t, const jarray::I n, const jarray::I r, const jarray::I *s)
 Allocates J array.
 
void FR (void *hdr)
 Frees J array memory.
 

Friends

class jarray
 

Detailed Description

Interface to dynamically loaded J engine.

Member Typedef Documentation

◆ adyad

typedef jarray(* jengine::adyad) (monad um, dyad ud, jarray x, jarray y)

Pointer to function, implementing dyadic variant of an adverb.

◆ amonad

typedef jarray(* jengine::amonad) (monad um, dyad ud, jarray y)

Pointer to function, implementing monadic variant of an adverb.

◆ dyad

typedef jarray(* jengine::dyad) (jarray x, jarray y)

Pointer to function, implementing dyadic variant of a verb.

◆ monad

typedef jarray(* jengine::monad) (jarray y)

Pointer to function, implementing monadic variant of a verb.

Constructor & Destructor Documentation

◆ jengine()

jengine::jengine ( )

Initializes J engine.

◆ ~jengine()

jengine::~jengine ( )

Frees J engine memory.

Member Function Documentation

◆ defAdverb()

bool jengine::defAdverb ( std::string  name,
amonad  mf,
adyad  df 
)

defines J adverb, calling one (or both) of specified C++ functions.

Works similarly to verb definition, except the functions may now call the verb argument of an adverb monadically or dyadically. Adverbs always have infinite ranks.

Parameters
namename of the new adverb (may already be defined).
mffunction to be called when adverb is invoked monadically.
dffunction to be called when adverb is invoked dyadically.
Returns
"true" on success.

◆ defScript()

bool jengine::defScript ( std::string  name,
int  type,
std::string  code,
int  mr = jengine::RMAX,
int  lr = jengine::RMAX,
int  rr = jengine::RMAX 
)

Defines a J script, given by a (possibly multi-line) string.

Parameters
namename of the script.
typetype of the script (the left argument of ":" conjunction).
codecode of the script (possibly multi-line).
mrmonadic rank (infinite by default).
lrleft rank (infinite by default).
rrright rank (infinite by default).
Returns
"true" upon success.

◆ defVerb()

bool jengine::defVerb ( std::string  name,
monad  mf,
dyad  df,
int  mr = RMAX,
int  lr = RMAX,
int  rr = RMAX 
)

Defines J verb, calling one (or both) of specified C++ functions.

Either of mf or df can be NULL, meaning the absence of corresponding (monadic/dyadic) case. Ranks, equal to RMAX denote "infinite" rank.

Parameters
namename of the new verb (may already be defined).
mffunction to be called when verb is invoked monadically.
dffunction to be called when verb is invoked dyadically.
mrmonadic rank (infinite by default).
lrleft rank (infinite by default).
rrright rank (infinite by default).
Returns
"true" on success.

◆ doJ()

bool jengine::doJ ( const std::string  s)

Executes J sentence.

Parameters
ssentence to execute, must be single line.
Returns
0 upon success.

◆ EPILOG() [1/2]

jarray::I jengine::EPILOG ( jarray::I  oldtop)

Frees all memory, allocated since ttop was recorded.

This function, together with PROLOG allows for more explicit J memory management. Be careful that all jarrays, allocated between prolog and epilog are out of scope (so that their destructors have already been called), otherwise this function has potential to mess things up considerably.

Parameters
oldtopthe top of J garbage collection stack, recorded by PROLOG.
Returns
oldtop.

◆ EPILOG() [2/2]

void * jengine::EPILOG ( jarray::I  oldtop,
void hdr 
)

Frees all memory, allocated since ttop was recorded.

This function, together with PROLOG allows for more explicit J memory management. Be careful that all jarrays, allocated between prolog and epilog are out of scope (so that their destructors have already been called), otherwise this function has potential to mess things up considerably.

Parameters
oldtopthe top of J garbage collection stack, recorded by PROLOG.
hdrpointer to the array header (already in the temp stack), which should not be freed, but lifted to the top of new stack (this array usually contains the result of a verb).
Returns
hdr.

◆ FR()

void jengine::FR ( void hdr)
protected

Frees J array memory.

Parameters
hdrpointer to the array header.

◆ GA()

void * jengine::GA ( const jarray::I  t,
const jarray::I  n,
const jarray::I  r,
const jarray::I s 
)
protected

Allocates J array.

Parameters
ttype of the array.
nnumber of elements in the array.
rrank of the array.
sshape of the array.
Returns
pointer to header of newly allocated array.

◆ get()

const jarray jengine::get ( const std::string  name)

Retrieves a named array from J.

Parameters
namename of the array to retrieve
Returns
the J array with the specified name, invalid if name not defined.

◆ getBuiltins()

std::set< std::string > jengine::getBuiltins ( ) const

Returns set of all names defined via defScript.

Returns
set of all names, defined via defScript

◆ getError()

int jengine::getError ( )

J error code for the last unsuccessful operation.

Returns
J error code or 0 if no error

Referenced by ok().

◆ initJlibrary()

static void jengine::initJlibrary ( std::ostream &  )
static

Loads and links J dynamic library, must be called once before this class is instantiated.

◆ isBuiltin()

bool jengine::isBuiltin ( std::string  name) const

Checks if given name was defined via defScript.

Parameters
namename to check
Returns
true if the name was defined via defScript

◆ ok()

bool jengine::ok ( )
inline

Conveniently check that there was no error.

Returns
true if there was no error.

References getError().

◆ PROLOG()

jarray::I jengine::PROLOG ( )

Returns the top of J garbage collection stack.

Returns
top of garbage collection stack.

◆ set()

bool jengine::set ( const std::string  name,
jarray value 
)

Assigns J name to the specified array.

Parameters
nameis the name for the array (may already be defined).
valueis the array to be known under the specified name.
Returns
true if success.

Friends And Related Symbol Documentation

◆ jarray

Field Documentation

◆ RMAX

const int jengine::RMAX =10000
static

Constant to denote the infinite rank.


The documentation for this class was generated from the following file: