MLIB
Loading...
Searching...
No Matches
mlib::asset Class Reference

Class for storing asset files in Windows resource data. More...

#include <asset.h>

Public Member Functions

 asset (int id_, const std::filesystem::path &name_=std::filesystem::path(), bool persist=false)
 Constructor for an asset object.
 
 ~asset ()
 Destructor. Delete asset file if it exists and is not persistent.
 
const void * data ()
 Load asset data and return a pointer to it.
 
size_t size ()
 Return size of asset data or 0 if asset could not be loaded.
 
bool write (const std::filesystem::path &root_path)
 Write the asset to a folder.
 
bool remove ()
 Delete asset file from disk.
 

Data Fields

std::filesystem::path name
 Relative asset name.
 

Detailed Description

Class for storing asset files in Windows resource data.

This class provides a mechanism for embedding different "assets" like those needed by a HTTP server (pages, CSS files, images, etc.), inside a Windows executable or DLL.

Each asset is stored as a user-defined resource of type RESFILE (defined as 256) and is identified by its ID. An asset can be written to a file. When the asset object goes out of scope the file is deleted.

Sample Usage

The resource file (.rc):

IDR_INDEX_HTML RESFILE "index.html"
IDR_MAIN_CSS RESFILE "main.css"

Program file:

mlib::asset page (IDR_INDEX_HTML, "web/index.html");
mlib::asset css (IDR_MAIN_CSS, "web/css/main.css");
//....
page.write ("C:\\temp");
css.write ("C:\\temp");
Class for storing asset files in Windows resource data.
Definition asset.h:28

This has created the file c:\temp\web\index.html and c:\temp\web\css\main.css. When the variables page and css go out of scope, the two files will be deleted (however, the directory structure remains in place).

Constructor & Destructor Documentation

◆ asset()

mlib::asset::asset ( int id_,
const std::filesystem::path & name_ = std::filesystem::path (),
bool persist = false )
inline

Constructor for an asset object.

Parameters
name_asset file name
id_resource ID
persistif true do not delete disk file when asset object is destructed

Member Function Documentation

◆ data()

const void * mlib::asset::data ( )
inline

Load asset data and return a pointer to it.

Returns
pointer to asset data or 0 if an error occurs.

◆ remove()

bool mlib::asset::remove ( )
inline

Delete asset file from disk.

Note
File is deleted even if it is a persistent asset.
Returns
true if file was deleted
false if file was not written or doesn't exist

◆ size()

size_t mlib::asset::size ( )
inline

Return size of asset data or 0 if asset could not be loaded.

Returns
size of asset (in bytes)

◆ write()

bool mlib::asset::write ( const std::filesystem::path & path)

Write the asset to a folder.

Parameters
pathroot path for asset file
Returns
true if successful, false otherwise

Asset filename is obtained by appending the asset name to the root path. For persistent assets, if file already exists, it is not overwritten.


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