FastLED 3.9.15
Loading...
Searching...
No Matches
asset.h
Go to the documentation of this file.
1#pragma once
2
27
28#include "fl/stl/int.h"
29#include "fl/stl/noexcept.h"
31#include "fl/stl/string.h"
32#include "fl/stl/string_view.h"
33#include "fl/stl/url.h"
34
35namespace fl {
36
37namespace asset_detail {
38
41constexpr fl::size clen(const char* s, fl::size n = 0) FL_NOEXCEPT {
42 return s[n] == '\0' ? n : clen(s, n + 1);
43}
44
50constexpr bool is_parent_at(const char* p, fl::size i) FL_NOEXCEPT {
51 // Segment start: either we're at the beginning, or preceded by a separator.
52 return (i == 0 || p[i - 1] == '/' || p[i - 1] == '\\')
53 && p[i] == '.' && p[i + 1] == '.'
54 && (p[i + 2] == '\0' || p[i + 2] == '/' || p[i + 2] == '\\');
55}
56
58constexpr bool path_has_parent_segment_at(const char* p, fl::size i) FL_NOEXCEPT {
59 return p[i] == '\0'
60 ? false
61 : (is_parent_at(p, i) ? true : path_has_parent_segment_at(p, i + 1));
62}
63
73constexpr bool path_has_parent_segment(const char* p) FL_NOEXCEPT {
74 return (p == nullptr) ? false : path_has_parent_segment_at(p, 0);
75}
76
77} // namespace asset_detail
78
84class asset_ref {
85 public:
88 constexpr asset_ref(const char* path, fl::size length) FL_NOEXCEPT
89 : mPath(path), mLength(length) {}
90
92 constexpr asset_ref() FL_NOEXCEPT : mPath(nullptr), mLength(0) {}
93
97 asset_ref(const asset_ref&) FL_NOEXCEPT = default;
99
101 constexpr explicit operator bool() const FL_NOEXCEPT {
102 return mPath != nullptr && mLength > 0;
103 }
104
109
111 constexpr const char* c_str() const FL_NOEXCEPT { return mPath; }
112 constexpr fl::size size() const FL_NOEXCEPT { return mLength; }
113
114 private:
115 const char* mPath;
116 fl::size mLength;
117};
118
128constexpr asset_ref asset(const char* path) FL_NOEXCEPT {
130 ? asset_ref()
131 : asset_ref(path, asset_detail::clen(path));
132}
133
137#define FL_ASSET(LITERAL_PATH) \
138 ([]() -> ::fl::asset_ref { \
139 FL_STATIC_ASSERT( \
140 !::fl::asset_detail::path_has_parent_segment(LITERAL_PATH), \
141 "fl::asset path must not contain '..' segments"); \
142 return ::fl::asset_ref( \
143 (LITERAL_PATH), ::fl::asset_detail::clen(LITERAL_PATH)); \
144 }())
145
164fl::url resolve_asset(const asset_ref& a) FL_NOEXCEPT;
165
176
177} // namespace fl
asset_ref & operator=(const asset_ref &) FL_NOEXCEPT=default
fl::string_view path() const FL_NOEXCEPT
The relative asset path as a string view (e.g. "data/track.mp3").
Definition asset.h:106
const char * mPath
Definition asset.h:115
constexpr asset_ref() FL_NOEXCEPT
Default-constructed handle refers to no asset.
Definition asset.h:92
asset_ref(const asset_ref &) FL_NOEXCEPT=default
Copy/move: trivial — pointer + length.
constexpr asset_ref(const char *path, fl::size length) FL_NOEXCEPT
Construct from a pointer to a null-terminated string with known length.
Definition asset.h:88
constexpr const char * c_str() const FL_NOEXCEPT
Pointer accessor — useful for JSON serialization.
Definition asset.h:111
constexpr fl::size size() const FL_NOEXCEPT
Definition asset.h:112
fl::size mLength
Definition asset.h:116
Opaque handle to a sketch-local asset.
Definition asset.h:84
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_NOEXCEPT
Recursive constexpr walk looking for any ".." segment.
Definition asset.h:58
constexpr bool is_parent_at(const char *p, fl::size i) FL_NOEXCEPT
Compile-time check: does position i in p start a ".." segment?
Definition asset.h:50
constexpr fl::size clen(const char *s, fl::size n=0) FL_NOEXCEPT
Compile-time length of a null-terminated C string.
Definition asset.h:41
constexpr bool path_has_parent_segment(const char *p) FL_NOEXCEPT
Compile-time check: returns true if the string contains a ".." segment.
Definition asset.h:73
fl::url resolve_asset(const asset_ref &a) FL_NOEXCEPT
Resolve an asset handle to a URL (or local file path) at runtime.
constexpr asset_ref asset(const char *path) FL_NOEXCEPT
Construct an asset handle from a relative sketch path at runtime.
Definition asset.h:128
void register_asset(fl::string_view path, const fl::url &u) FL_NOEXCEPT
Public helper: plug an asset mapping at runtime.
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Portable compile-time assertion wrapper.
Lightweight URL parser for embedded environments.