Description
Four functions have been added to the IPB_Session interface to enable you to get the value of a variable of type Any.
Usage
The function prototypes are:
virtual IPB_Value* GetPBAnyField( pbobject obj, pbfieldID fid, pbboolean& isNull )
virtual IPB_Value* GetPBAnySharedVar( pbgroup group, pbfieldID fid, pbboolean& isNull )
virtual IPB_Value* GetPBAnyGlobalVar( pbfieldID fid, pbboolean& isNull )
virtual IPB_Value* GetPBAnyArrayItem( pbarray array, pblong dim[], pbboolean& isNull )
The value you retrieve must be of datatype Any to use these functions; that is, the variable associated with the function must be declared as a variable of type Any in the development environment. If it is not, the function returns a null pointer and the value of isNull is set to true.
The functions return a pointer to an IPB_Value instance. When one of these functions is called, memory is allocated for the returned IPB_Value instance, and the pointer is recorded in the current local frame. The pointer is deleted automatically when the current local frame is popped, which occurs when the current local function returns (you can also call PopLocalFrame to force the frame to be popped).
If you want to use the value returned by any of these functions, you must save the value pointed to by the IPB_Value instance (not the IPB_Value instance itself) before the frame is popped. If you save the pointer itself, the value is only valid until the original value is destroyed.
You can use the AcquireValue function to save the value, or one of the IPB_Value Get<type> functions. For example, the following code saves the string value in the IPB_Value instance ivalue into the string str. The value in str can be used after the local frame is popped and ivalue is deleted:
IPB_Value* ivalue = session->GetPBAnyField(vobj, vfid, isNull); pbstring str = ivalue->GetString();
If you do not know the actual datatype of the Any variable, use the IPB_Value GetType function to get its datatype first, then use the appropriate get function to get its value.
IPB_Value holds a reference to the original value The value in the IPB_Value instance is a reference to the original value. If you change the actual value of the returned IPB_Value, the original value is also changed. If you use the AcquireValue function to save the value, it clones a new IPB_Value and resets the existing IPB_Value pointer.
Examples
This example tests all the functions, using PushLocalFrame and PopLocalFrame to simulate the scope of a function call:
session->PushLocalFrame(); pbgroup vgroup = session->FindGroup("n_test", pbgroup_userobject); pbclass vcls = session->FindClass(vgroup, "n_test"); pbobject vobj = session->NewObject(vcls); pbboolean isNull;
pbfieldID vfid = session->GetFieldID(vcls, "i_a"); IPB_Value* value = session->GetPBAnyField(vobj, vfid, isNull); pbstring str = value->GetString(); // save actual value
vfid = session->GetSharedVarID(vgroup, "s_a"); value = session->GetPBAnySharedVar(vgroup, vfid, isNull); //Get the actual value here.
vfid = session->GetGlobalVarID("g_a"); value = session->GetPBAnyGlobalVar(vfid, isNull); //Get the actual value here.
vfid = session->GetFieldID(vcls, "i_array"); pbarray arr = session->GetArrayField(vobj, vfid, isNull); //Get the any array first.
long dim = 1; value = session->GetPBAnyArrayItem(arr, &dim, isNull); //Get the actual value here.
session->PopLocalFrame();
|