Skip to content

Instantly share code, notes, and snippets.

@veryjos
Created June 26, 2018 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veryjos/a1e2c3bc54e54c286b137ea2f9553194 to your computer and use it in GitHub Desktop.
Save veryjos/a1e2c3bc54e54c286b137ea2f9553194 to your computer and use it in GitHub Desktop.
#include "BabyDI.h"
#include "ResourceDaemon.h"
#include "resources/ImageResourceFactory.h"
#include <fstream>
using namespace tdrp;
ResourceDataProvider* firstDataProvider = new ResourceDataProvider {
[](const IResourceRecord* record) {
return std::async(std::launch::async, []() {
auto meta = ResourceMeta {
1
};
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
return meta;
});
},
[](const IResourceRecord* record) {
return std::async(std::launch::async, [=]() {
auto is = std::ifstream(record->path, std::ifstream::in | std::ifstream::binary);
is.seekg(0, is.end);
int length = is.tellg();
is.seekg(0, is.beg);
uint8_t* data = new uint8_t[length];
is.read((char*)data, length);
return ResourceData(data, length);
});
}
};
ResourceDataProvider* updatedDataProvider = new ResourceDataProvider {
[](const IResourceRecord* record) {
return std::async(std::launch::async, []() {
auto meta = ResourceMeta {
2
};
using namespace std::chrono_literals;
std::this_thread::sleep_for(2s);
return meta;
});
},
[](const IResourceRecord* record) {
return std::async(std::launch::async, [=]() {
auto is = std::ifstream(record->path + "2", std::ifstream::in | std::ifstream::binary);
is.seekg(0, is.end);
int length = is.tellg();
is.seekg(0, is.beg);
uint8_t* data = new uint8_t[length];
is.read((char*)data, length);
return ResourceData(data, length);
});
}
};
void ConfigureDI() {
auto daemon = new ResourceDaemon(
{
firstDataProvider,
updatedDataProvider
},
{
new ImageResourceFactory()
}
);
PROVIDE(ResourceDaemon, daemon);
}
int main(int argc, char* argv[]) {
ConfigureDI();
// Get a reference to the resource daemon
auto resourceDaemon = BabyDI::Get<ResourceDaemon>();
auto imageHandle = resourceDaemon->Load<Image>("shit.png");
// Wait while the image is being loaded/deserialized
while (!imageHandle.IsValid()) {};
while (true) {
using namespace std::chrono_literals;
auto img = imageHandle.Lock();
if (img) {
printf("%d : %d\n", img->width, img->height);
std::this_thread::sleep_for(1s);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment