Create a SPUPad

Within the UDF evaluate method, you create a named pad of type SPUPad for your function. The following example creates a SPUPad named stringpad that is a string storage pad. The UDF called string_pad_create takes an input string and length, then saves each character in the string in an array. Note that this SPUPad uses shared memory because it calls the getPad() function. If you want to create a file-backed memory SPUPad, use the getFilePad() function instead.
class StringPadCreate: public Udf
{
private:

public:
  static Udf* instantiate();
  virtual ReturnValue evaluate()
   {
   CPad* pad = getPad("stringpad");
   Root *ro = (Root*) pad->getRootObject(sizeof(Root)); //Line 4. 
   if(!ro) // If false, stringpad does not exist; safe to create it.
   {
     ro=PAD_NEW(pad,Root);
     int32 size = int32Arg(0);
     StringArg* a = stringArg(1);
     int32 stringSize=a->length;
     if (size<1 || size > 64000) 
     {
      throwUdxException("Given size is out of range.");
     }
     if(stringSize > size)
     {
      throwUdxException("Given string bigger than given size.");
     }
     ro->size=size;
     ro->data=PAD_NEW(pad,char)[size]; // PAD_NEW creates an array to
                                       // hold each character in the
                                       // input string.
     for(int i=0; i<size; i++)
     {
      if(i<stringSize)
       {
         ro->data[i]=a->data[i];
       }
       else
       {
         ro->data[i]=' ';
       }
     }
     pad->setRootObject(ro, sizeof(Root));         //Line 34. 
     NZ_UDX_RETURN_BOOL(true);
   }
   else // stringpad already exists; stop processing the create task.
   {
     NZ_UDX_RETURN_BOOL(false);
   }
}
Udf* StringPadCreate::instantiate()
{
  return new StringPadCreate;
}

In the sample code, keep in mind the following points:

  • On Line 4, there is a call to getRootObject that verifies whether stringpad already exists. If the pad does not exist, the program creates it and returns true; otherwise the function does not create another pad and returns false. Your UDX should check for the presence of the SPUPad before it creates the SPUPad.
  • On Line 34, the program sets the root object for the SPUPad so that subsequent calls or functions can reference the SPUPad.