Tilt Five™ Unity API  1.3.0
 
Loading...
Searching...
No Matches
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.
 

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 410 of file NativePlugin.cs.

Constructor & Destructor Documentation

◆ T5_StringUTF8()

TiltFive.T5_StringUTF8.T5_StringUTF8 ( string  text)

Definition at line 416 of file NativePlugin.cs.

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

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 464 of file NativePlugin.cs.

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

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 441 of file NativePlugin.cs.

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

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 412 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: