SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLIOWriterLocalStorage Class Reference

SLIOStream implementation for writing to browser local storage. More...

#include <SLIOLocalStorage.h>

Inheritance diagram for SLIOWriterLocalStorage:
[legend]

Public Member Functions

 SLIOWriterLocalStorage (std::string path)
 
void flush ()
 
- Public Member Functions inherited from SLIOWriterMemory
 SLIOWriterMemory (std::string path)
 
size_t write (const void *buffer, size_t size)
 
size_t tell ()
 
bool seek (size_t offset, Origin origin)
 
void flush ()
 
- Public Member Functions inherited from SLIOStream
virtual ~SLIOStream ()=default
 
virtual size_t read (void *buffer, size_t size)
 
virtual size_t size ()
 

Additional Inherited Members

- Public Types inherited from SLIOStream
enum  Origin { IOO_beg , IOO_cur , IOO_end }
 
- Protected Attributes inherited from SLIOWriterMemory
std::string _path
 
size_t _position
 

Detailed Description

SLIOStream implementation for writing to browser local storage.

When calling flush, the memory is written from memory to local storage using the JavaScript API provided by the web browser.

Definition at line 41 of file SLIOLocalStorage.h.

Constructor & Destructor Documentation

◆ SLIOWriterLocalStorage()

SLIOWriterLocalStorage::SLIOWriterLocalStorage ( std::string  path)

Definition at line 56 of file SLIOLocalStorage.cpp.

57  : SLIOWriterMemory(path)
58 {
59 }
SLIOWriterMemory(std::string path)
Definition: SLIOMemory.cpp:81

Member Function Documentation

◆ flush()

void SLIOWriterLocalStorage::flush ( )
virtual

Reimplemented from SLIOStream.

Definition at line 61 of file SLIOLocalStorage.cpp.

62 {
63  std::vector<char>& buffer = SLIOMemory::get(_path);
64  const char* data = buffer.data();
65  size_t length = buffer.size();
66 
67  // clang-format off
68  MAIN_THREAD_EM_ASM({
69  let path = UTF8ToString($0);
70  let section = HEAPU8.subarray($1, $1 + $2);
71 
72  let array = new Uint8Array(section);
73  let binary = "";
74  for(let i = 0; i < array.byteLength; i++)
75  binary += String.fromCharCode(array[i]);
76 
77  let key = "SL:" + path;
78  localStorage.setItem(key, btoa(binary));
79  }, _path.c_str(), data, length);
80  // clang-format on
81 }
std::string _path
Definition: SLIOMemory.h:52
std::vector< char > & get(std::string path)
Definition: SLIOMemory.cpp:24

The documentation for this class was generated from the following files: