Tilt Five Unity API  1.3.0
Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
TiltFive.T5_StringUTF8 Struct Reference

Represents a string value. More...

Inheritance diagram for TiltFive.T5_StringUTF8:

Public Member Functions

 T5_StringUTF8 (string text)
 
void Dispose ()
 Safely disposes of this T5_StringUTF8 and any unmanaged memory allocated during its construction. More...
 

Static Public Member Functions

static implicit operator string (T5_StringUTF8 t5_StringUTF8)
 
static implicit operator T5_StringUTF8 (string text)
 

Static Private Member Functions

static string ToString (T5_StringUTF8 t5_StringUTF8)
 

Private Attributes

UInt32 maxBufferSize
 
UInt32 length
 
IntPtr pStringBytesUTF8
 

Detailed Description

Represents a string value.

This struct exists primarily to guarantee a common memory layout when marshaling string values to/from the native plugin. Note that it implements IDisposable, and that it should be wrapped in a "using" statement/block to avoid leaking memory.

Definition at line 407 of file NativePlugin.cs.

Constructor & Destructor Documentation

◆ T5_StringUTF8()

TiltFive.T5_StringUTF8.T5_StringUTF8 ( string  text)

Definition at line 413 of file NativePlugin.cs.

414  {
415  pStringBytesUTF8 = IntPtr.Zero;
416  length = 0;
417  maxBufferSize = 16 * 1024;
418 
419  if (text != null)
420  {
421  // Allocate enough unmanaged memory to store the string
422  byte[] textBytesUTF8 = System.Text.Encoding.UTF8.GetBytes(text);
423 
424  // If the string is too long to fit in the unmanaged buffer, truncate it.
425  length = (UInt32)Math.Min(textBytesUTF8.Length, maxBufferSize);
426  pStringBytesUTF8 = Marshal.AllocHGlobal((int)maxBufferSize);
427 
428  // Store the string data
429  Marshal.Copy(textBytesUTF8, 0, pStringBytesUTF8, (int)length);
430  }
431  }

References TiltFive.T5_StringUTF8.length, TiltFive.T5_StringUTF8.maxBufferSize, and TiltFive.T5_StringUTF8.pStringBytesUTF8.

Member Function Documentation

◆ Dispose()

void TiltFive.T5_StringUTF8.Dispose ( )

Safely disposes of this T5_StringUTF8 and any unmanaged memory allocated during its construction.

Definition at line 461 of file NativePlugin.cs.

462  {
463  // Don't forget to free that unmanaged memory we allocated.
464  // Marshal.FreeHGlobal() will safely do nothing if IntPtr.Zero is passed in.
465  Marshal.FreeHGlobal(pStringBytesUTF8);
466  pStringBytesUTF8 = IntPtr.Zero;
467  }

References TiltFive.T5_StringUTF8.pStringBytesUTF8.

Referenced by TiltFive.Glasses.GlassesCore.TryGetFriendlyName().

◆ operator string()

static implicit TiltFive.T5_StringUTF8.operator string ( T5_StringUTF8  t5_StringUTF8)
static

◆ operator T5_StringUTF8()

static implicit TiltFive.T5_StringUTF8.operator T5_StringUTF8 ( string  text)
static

◆ ToString()

static string TiltFive.T5_StringUTF8.ToString ( T5_StringUTF8  t5_StringUTF8)
staticprivate

Definition at line 438 of file NativePlugin.cs.

439  {
440  if (t5_StringUTF8.pStringBytesUTF8 == IntPtr.Zero)
441  {
442  return null;
443  }
444 
445  var managedBytes = new byte[t5_StringUTF8.length];
446  try
447  {
448  Marshal.Copy(t5_StringUTF8.pStringBytesUTF8, managedBytes, 0, (int)t5_StringUTF8.length);
449  return System.Text.Encoding.UTF8.GetString(managedBytes);
450  }
451  catch (Exception e)
452  {
453  Debug.LogError($"Failed to copy string from unmanaged memory: {e}");
454  return null;
455  }
456  }

References TiltFive.T5_StringUTF8.length, and TiltFive.T5_StringUTF8.pStringBytesUTF8.

Member Data Documentation

◆ length

UInt32 TiltFive.T5_StringUTF8.length
private

◆ maxBufferSize

UInt32 TiltFive.T5_StringUTF8.maxBufferSize
private

Definition at line 409 of file NativePlugin.cs.

Referenced by TiltFive.T5_StringUTF8.T5_StringUTF8().

◆ pStringBytesUTF8

IntPtr TiltFive.T5_StringUTF8.pStringBytesUTF8
private

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