Variables of type String

This page regroups the different ways to handle strings with AntTweakBar.

C-Static sized String

Use of TW_TYPE_CSSTRING variable.

Direct binding

char s1[64] = "a static string"; // sizeof(s1) is 64
 
TwAddVarRW(bar, "s1", TW_TYPE_CSSTRING(sizeof(s1)), s1, ""); // must pass s1 (not &s1)

Callback access

char s1[64] = "a static string";
 
void TW_CALL SetMyCSStringCB(const void *value, void *clientData)
{
  const char *src = (const char *)value;
  strncpy(s1, src, sizeof(s1));
  s1[sizeof(s1)-1] = '\0'; // ensure that it is always null-terminated
}
void TW_CALL GetMyCSStringCB(void *value, void *clientData)
{
  char *dest = (char *)value;
  strncpy(dest, s1, sizeof(s1));
  dest[sizeof(s1)-1] = '\0';  // ensure that it is always null-terminated
}
 
// ...
 
TwAddVarCB(bar, "s1", TW_TYPE_CSSTRING(sizeof(s1)), SetMyCSStringCB, GetMyCSStringCB, clientData, ""); // set clientData to NULL if no client data

C-Dynamic String

Use of TW_TYPE_CDSTRING variable.

Direct binding

Function TwCopyCDStringToClientFunc must be called prior to any TwAddVarRW/RO (see TW_TYPE_CDSTRING).

// Function called by AntTweakBar to copy the content of a C-Dynamic String (src) handled
// by the AntTweakBar library to a C-Dynamic string (*destPtr) handled by your application
void TW_CALL CopyCDStringToClient(char **destPtr, const char *src)
{
  size_t srcLen = (src!=NULL) ? strlen(src) : 0;
  size_t destLen = (*destPtr!=NULL) ? strlen(*destPtr) : 0;
 
  // alloc or realloc dest memory block if needed
  if( *destPtr==NULL )
    *destPtr = (char *)malloc(srcLen+1);
  else if( srcLen>destLen )
    *destPtr = (char *)realloc(*destPtr, srcLen+1);
 
  // copy src
  if( srcLen>0 )
    strncpy(*destPtr, src, srcLen);
  (*destPtr)[srcLen] = '\0'; // null-terminated string
}
 
// ...
 
TwCopyCDStringToClientFunc(CopyCDStringToClient); // must be called once (just after TwInit for instance)
 
char *s2 = strdup("a C-string allocated dynamically");
 
TwAddVarRW(bar, "s2", TW_TYPE_CDSTRING, &s2, ""); // pass the pointer to s2
 
// ...
 
free(s2);

Callback access

Function TwCopyCDStringToLibrary must be called in the get callback to copy the string handled by your application to AntTweakBar (see TW_TYPE_CDSTRING).

char *s2;
 
void TW_CALL SetMyCDStringCB(const void *value, void *clientData)
{
  // Set: copy the value of s2 from AntTweakBar
  const char *src = *(const char **)value;
  CopyCDStringToClient(&s2, src); // CopyCDStringToClient implementation is given above (see direct handling)
}
void TW_CALL GetMyCDStringCB(void *value, void *clientData)
{
  // Get: return the value of s2 to AntTweakBar
  char **destPtr = (char **)value;
  TwCopyCDStringToLibrary(destPtr, s2); // use TwCopyCDStringToLibrary to copy the string
}
 
// ...
 
s2 = strdup("a C-string allocated dynamically");
 
TwAddVarCB(bar, "s2", TW_TYPE_CDSTRING, SetMyCDStringCB, GetMyCDStringCB, clientData, ""); // set clientData to NULL if no client data
 
// ...
 
free(s2);

C++ STD String

Use of TW_TYPE_STDSTRING variable.

Direct binding

Function TwCopyStdStringToClientFunc must be called prior to any TwAddVarRW/RO (see TW_TYPE_STDSTRING).

// Function called by AntTweakBar to copy the content of a std::string handled
// by the AntTweakBar library to a std::string handled by your application
void TW_CALL CopyStdStringToClient(std::string& destinationClientString, const std::string& sourceLibraryString)
{
  destinationClientString = sourceLibraryString;
}
 
// ...
 
TwCopyStdStringToClientFunc(CopyStdStringToClient); // must be called once (just after TwInit for instance)
 
std::string s3 = "a STL string";
 
TwAddVarRW(bar, "s3", TW_TYPE_STDSTRING, &s3, "");

Callback access

Function TwCopyStdStringToLibrary must be called in the get callback to copy the string handled by your application to AntTweakBar (see TW_TYPE_STDSTRING).

std::string s3 = "a STL string";
 
void TW_CALL SetMyStdStringCB(const void *value, void *clientData)
{
  // Set: copy the value of s3 from AntTweakBar
  const std::string *srcPtr = static_cast<const std::string *>(value);
  s3 = *srcPtr;
}
void TW_CALL GetMyStdStringCB(void *value, void * /*clientData*/)
{
  // Get: copy the value of s3 to AntTweakBar
  std::string *destPtr = static_cast<std::string *>(value);
  TwCopyStdStringToLibrary(*destPtr, s3);
}
 
// ...
 
TwAddVarCB(bar, "s3", TW_TYPE_STDSTRING, SetMyStdStringCB, GetMyStdStringCB, clientData, ""); // set clientData to NULL if no client data