Open scorpioluck20 opened 1 year ago
This is correct, my guess is, but you should investigate, is that there could be a singleton manager IPFSLoaderManager that allows loading from any filesystem (ipfs, harddisk, websockets) that would use something like this, it may already be in the code somewhere, but not sure.
class LoaderManager {
// this will require a compare function for string comparisons added to this map STL template
private map<std::string prefix, FileLoaderInterface *> fileLoaders;
private map<std::string suffix, ParserInterface *> parsers;
// create class a singleton manager. here
// register a class that loads based on prefix
void registerLoader(std::string prefix, FileLoaderInterface *fli) {
fileLoaders[prefix] = fli;
}
// register a ParserInterface class that handles this suffix based files
void registerParser(std::string suffix, ParserInterface *pi) {
parsers[suffix] = pi;
}
void *Load(std::string filename, bool parse = false;) {
std::string prefix;
std::string suffix;
std::string fileName;
splitFilename(filename, &prefix, &fileName, &suffix); // scan for substring before :// and split to get prefix, filename.suffix and suffix by itself
void *retData = NULL;
FileLoaderInterface::iterator flii = fileLoaders.find(prefix); // this find will need to add a comparison operator to the map or else it will check the address or reference to the string and not do a strcmp, check this as it might be OK for std::string and just not a char *
if (flii == fileLoaders.end()) {
logError("don't save a Loader for this system %s", prefix);
or return NULL;
} else {
FileLoaderInterface *fli = *flii;
retData = flii.Load(fileName);
}
if (parse && (retData != null)) {
ParserInterface::iterator pii = parsers.find(suffix);
if (pii == parsers.end())
{
LogError(blah);
} else {
ParseInterface *pi = *pii;
retData = pi->Parse(retData);
}
}
class FileLoaderInterface {
virtual void *Load(std::string filename) = 0;
}
class ParserInterface {
virtual void *Parse(void *inData) = 0;
}
IFPSLoader.h
class IPFSLoader : public FileLoaderInterface {
}
IPFSLoader.cpp
IPFSLoader::IPFSLoader() {
FileLoaderManager::getInstance().registerLoader("ipfs", this);
}
void *IPFSLoader::Load(char *filename) {
}
MNNParser.h
class MNNParser : public ParserInterface {
}
MNNParser.cpp
MNNParser::MNNParser() {
FileLoaderManager::getInstance()->RegisterParse("mnn", this);
}
void *MNNParser::Parse(void *dataIn) {
// parse mnn data to a binary format.
}
// "ftp://filename.xxx",
// "ipfs://filename.xxx",
// "ws://filename.xxx"
// just load
void *mnnData = IPFSLoaderManager::getInstance()->Load("ws://something.mnn");
// load and parse
void *mnnData = IPFSLoaderManager::getInstance()->Load("ipfs://something.mnn", true);
// create a Save Manager singleton class
class SaveManager() {}
// Now you can even do the same thing for SaveData() and give it a destination like.
Save("mnn://filename.mnn", void *data);
// The save then can just write the mnn data directly to the GPU or load it through the MNN library or
Here's a singleton class if you don't know what one is.
And here's how we did it in a 3d graphics engine. But this can be much simpler
https://github.com/Super-Genius/ElementalEngine2/tree/master/Plugins2/LoadSavers