libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
fileformatfactory.h
Go to the documentation of this file.
1
8#pragma once
9
11#include <promeki/core/string.h>
13#include <promeki/core/list.h>
14#include <promeki/core/result.h>
15#include <promeki/core/logger.h>
16#include <promeki/core/util.h>
17
19
20class IODevice;
21
47template<typename Product>
49 public:
62
69 if(factory == nullptr) return -1;
70 auto &list = factoryList();
71 int ret = list.size();
72 list += factory;
73 promekiInfo("Registered FileFormatFactory %s",
74 factory->name().cstr());
75 return ret;
76 }
77
86 static const FileFormatFactory *lookup(const Context &ctx) {
87 auto &list = factoryList();
88 for(auto *f : list) {
89 if(f->canDoOperation(ctx)) return f;
90 }
91 return nullptr;
92 }
93
95 FileFormatFactory() = default;
96
98 virtual ~FileFormatFactory() {}
99
104 String name() const { return _name; }
105
110 const StringList &extensions() const { return _exts; }
111
117 bool isExtensionSupported(const String &filename) const {
118 size_t dot = filename.rfind('.');
119 if(dot == String::npos || dot + 1 >= filename.size()) return false;
120 String ext = filename.mid(dot + 1).toLower();
121 for(const auto &item : _exts) {
122 if(ext == item) return true;
123 }
124 return false;
125 }
126
132 bool isHintSupported(const String &hint) const {
133 String lower = hint.toLower();
134 for(const auto &item : _exts) {
135 if(lower == item) return true;
136 }
137 return false;
138 }
139
148 virtual bool canDoOperation(const Context &ctx) const {
149 return false;
150 }
151
162
163 protected:
166
167 private:
168 static List<FileFormatFactory *> &factoryList() {
170 return list;
171 }
172};
173
179#define PROMEKI_REGISTER_FILE_FORMAT_FACTORY(product, name) \
180 [[maybe_unused]] static int \
181 PROMEKI_CONCAT(__promeki_fff_, PROMEKI_UNIQUE_ID) = \
182 FileFormatFactory<product>::registerFactory(new name);
183
@ NotImplemented
Feature is not implemented.
Definition error.h:55
Generic factory template for file-format-specific product creation.
Definition fileformatfactory.h:48
static const FileFormatFactory * lookup(const Context &ctx)
Looks up a factory that can handle the given context.
Definition fileformatfactory.h:86
bool isExtensionSupported(const String &filename) const
Returns true if the filename has a supported extension.
Definition fileformatfactory.h:117
virtual bool canDoOperation(const Context &ctx) const
Returns true if this factory can handle the given context.
Definition fileformatfactory.h:148
static int registerFactory(FileFormatFactory *factory)
Registers a factory instance in the global registry.
Definition fileformatfactory.h:68
virtual Result< Product > createForOperation(const Context &ctx) const
Creates a Product configured for the given context.
Definition fileformatfactory.h:159
String _name
Human-readable factory name.
Definition fileformatfactory.h:164
bool isHintSupported(const String &hint) const
Returns true if the hint matches a supported extension.
Definition fileformatfactory.h:132
FileFormatFactory()=default
Default constructor.
virtual ~FileFormatFactory()
Virtual destructor.
Definition fileformatfactory.h:98
StringList _exts
List of supported file extensions (no dots).
Definition fileformatfactory.h:165
String name() const
Returns the human-readable name of this factory.
Definition fileformatfactory.h:104
const StringList & extensions() const
Returns the list of supported file extensions.
Definition fileformatfactory.h:110
Abstract base class for all I/O devices.
Definition iodevice.h:29
Dynamic array container wrapping std::vector.
Definition list.h:40
size_t size() const noexcept
Returns the number of elements in the list.
Definition list.h:301
Manages a list of strings.
Definition stringlist.h:21
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
String toLower() const
Returns a lowercase copy of this string.
Definition string.h:624
size_t size() const
Returns the number of characters in the string.
Definition string.h:292
static constexpr size_t npos
Sentinel value indicating "not found".
Definition string.h:91
String mid(size_t pos, size_t count=npos) const
Returns a substring starting at pos (alias for substr).
Definition string.h:425
size_t rfind(char val, size_t from=npos) const
Finds the last occurrence of a character.
Definition string.h:375
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
Context passed to factory methods for lookup and creation.
Definition fileformatfactory.h:56
String formatHint
Extension hint (e.g. "wav"), no dot.
Definition fileformatfactory.h:59
int operation
Product-specific operation (e.g. Reader/Writer).
Definition fileformatfactory.h:57
String filename
Full path, if available.
Definition fileformatfactory.h:58
IODevice * device
IODevice to operate on, if available.
Definition fileformatfactory.h:60