B4R Code Snippet Useful defines

I use these quite often and they may be of use to others.
B4X:
// gets the lenth of a c array
#define lengthOf(a) std::end(a) - std::begin(a)

// gets the object type inside a B4R::Object ---- Look in rCore.h for the type numbers 
#define OBJType(a)    (int)  a->type

// gets the value inside a B4R::Object (simple objects not arrays)
#define OBJtoInt(a)   (int)  a->data.LongField
#define OBJtoLong(a)  (long) a->data.LongField
#define OBJtoShort(a) (short)a->data.LongField

// object value to int or long  else return long value
#define OBJtoNumber(o) o->type==3?OBJtoInt(o):o->type==5?OBJtoLong(o):o->data.LongField

// returns a const char * from inside a string array inside an object variable.
#define OBJtoStringfromArray(a,idx) (((B4R::B4RString**)((B4R::Array*)a)->getData((UInt) (idx)))[B4R::Array::staticIndex])->data

Example usage:

lengthOf
B4X:
    const char* s[] = {"one","two","three","four","five"};
   
    for(int a = 0; a < lengthOf(s); a++){
        Serial.println(s[a]);
    }

OBJtoLong etc
B4X:
void listAdder(B4R::Object* o){
    long v = OBJtoLong(o);
...

OBJtoStringfromArray
B4X:
    Dim g() As String = Array As String("aaaaa","bbbb","ccccc")
...
    RunNative("arrayLister",g)
...


void arrayLister(B4R::Object * o){
// params are object, index you want
// following line Object is o  and the index is 1
    const char * thisString = OBJtoStringfromArray(o,1);
    Serial.println(thisString);
}
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Another thing I find of use is (assuming you have VS Code Editor installed) changing the ctrl+click line to
B4X:
'Ctrl+Click to open the .CPP code file in editor: ide://run?File=%WINDIR%\explorer.exe&Args=/separate%2c/root%2c%PROJECT%\Objects\Src\b4r_mod1.cpp

It will open the .cpp source file in the VS Code Editor (on Windows 11 for me), I just add 1 line per module as I add them to a project, as I find it easier to read the source.

** The %2c are there because explorer wants a comma between arguments, but you cant put a comma in the line as it breaks the link.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…