FastLED 3.10.8
Loading...
Searching...
No Matches
asset.h
Go to the documentation of this file.
1#pragma once
2
29
30#include "fl/stl/int.h"
31#include "fl/stl/noexcept.h"
33#include "fl/stl/string.h"
34#include "fl/stl/string_view.h"
35#include "fl/stl/url.h"
36
37namespace fl {
38
39namespace asset_detail {
40
43constexpr fl::size clen(const char* s, fl::size n = 0) FL_NO_EXCEPT {
44 return s[n] == '\0' ? n : clen(s, n + 1);
45}
46
52constexpr bool is_parent_at(const char* p, fl::size i) FL_NO_EXCEPT {
53 // Segment start: either we're at the beginning, or preceded by a separator.
54 return (i == 0 || p[i - 1] == '/' || p[i - 1] == '\\')
55 && p[i] == '.' && p[i + 1] == '.'
56 && (p[i + 2] == '\0' || p[i + 2] == '/' || p[i + 2] == '\\');
57}
58
60constexpr bool path_has_parent_segment_at(const char* p, fl::size i) FL_NO_EXCEPT {
61 return p[i] == '\0'
62 ? false
63 : (is_parent_at(p, i) ? true : path_has_parent_segment_at(p, i + 1));
64}
65
75constexpr bool path_has_parent_segment(const char* p) FL_NO_EXCEPT {
76 return (p == nullptr) ? false : path_has_parent_segment_at(p, 0);
77}
78
79} // namespace asset_detail
80
86class asset_ref {
87 public:
90 constexpr asset_ref(const char* path, fl::size length) FL_NO_EXCEPT
91 : mPath(path), mLength(length) {}
92
94 constexpr asset_ref() FL_NO_EXCEPT : mPath(nullptr), mLength(0) {}
95
99 asset_ref(const asset_ref&) FL_NO_EXCEPT = default;
101
103 constexpr explicit operator bool() const FL_NO_EXCEPT {
104 return mPath != nullptr && mLength > 0;
105 }
106
111
113 constexpr const char* c_str() const FL_NO_EXCEPT { return mPath; }
114 constexpr fl::size size() const FL_NO_EXCEPT { return mLength; }
115
116 private:
117 const char* mPath;
118 fl::size mLength;
119};
120
130constexpr asset_ref asset(const char* path) FL_NO_EXCEPT {
132 ? asset_ref()
133 : asset_ref(path, asset_detail::clen(path));
134}
135
139#define FL_ASSET(LITERAL_PATH) \
140 ([]() -> ::fl::asset_ref { \
141 FL_STATIC_ASSERT( \
142 !::fl::asset_detail::path_has_parent_segment(LITERAL_PATH), \
143 "fl::asset path must not contain '..' segments"); \
144 return ::fl::asset_ref( \
145 (LITERAL_PATH), ::fl::asset_detail::clen(LITERAL_PATH)); \
146 }())
147
166fl::url resolve_asset(const asset_ref& a) FL_NO_EXCEPT;
167
178
179} // namespace fl
asset_ref & operator=(const asset_ref &) FL_NO_EXCEPT=default
constexpr asset_ref() FL_NO_EXCEPT
Default-constructed handle refers to no asset.
Definition asset.h:94
const char * mPath
Definition asset.h:117
fl::string_view path() const FL_NO_EXCEPT
The relative asset path as a string view (e.g. "data/track.mp3").
Definition asset.h:108
constexpr fl::size size() const FL_NO_EXCEPT
Definition asset.h:114
constexpr const char * c_str() const FL_NO_EXCEPT
Pointer accessor — useful for JSON serialization.
Definition asset.h:113
asset_ref(const asset_ref &) FL_NO_EXCEPT=default
Copy/move: trivial — pointer + length.
constexpr asset_ref(const char *path, fl::size length) FL_NO_EXCEPT
Construct from a pointer to a null-terminated string with known length.
Definition asset.h:90
fl::size mLength
Definition asset.h:118
Opaque handle to a sketch-local asset.
Definition asset.h:86
Definition url.h:15
fl::UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
constexpr bool path_has_parent_segment_at(const char *p, fl::size i) FL_NO_EXCEPT
Recursive constexpr walk looking for any ".." segment.
Definition asset.h:60
constexpr bool is_parent_at(const char *p, fl::size i) FL_NO_EXCEPT
Compile-time check: does position i in p start a ".." segment?
Definition asset.h:52
constexpr fl::size clen(const char *s, fl::size n=0) FL_NO_EXCEPT
Compile-time length of a null-terminated C string.
Definition asset.h:43
constexpr bool path_has_parent_segment(const char *p) FL_NO_EXCEPT
Compile-time check: returns true if the string contains a ".." segment.
Definition asset.h:75
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
void register_asset(fl::string_view path, const fl::url &u) FL_NO_EXCEPT
Public helper: plug an asset mapping at runtime.
fl::url resolve_asset(const asset_ref &a) FL_NO_EXCEPT
Resolve an asset handle to a URL (or local file path) at runtime.
constexpr asset_ref asset(const char *path) FL_NO_EXCEPT
Construct an asset handle from a relative sketch path at runtime.
Definition asset.h:130
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NO_EXCEPT
Portable compile-time assertion wrapper.
Lightweight URL parser for embedded environments.