@@ -25,6 +25,7 @@ namespace lol
class DefaultThreadManager : public BaseThreadManager
{
public:
char const *GetName() { return "<DefaultThreadManager>"; }
DefaultThreadManager(int thread_count)
: BaseThreadManager(thread_count, thread_count)
{ }
@@ -32,8 +33,6 @@ public:
: BaseThreadManager(thread_count, thread_min)
{ }
char const *GetName() { return "<DefaultThreadManager>"; }
//Work stuff
bool AddJob(ThreadJob* job);
bool GetWorkResult(array<ThreadJob*>& results);
@@ -70,7 +69,8 @@ protected:
bool m_updated = false;
};
//Test the files registered and warns when they update ------------------------
//FileUpdateTester ------------------------------------------------------------
//Test the files registered and warns when they update
class FileUpdateTester : public BaseThreadManager
{
typedef BaseThreadManager super;
@@ -80,9 +80,9 @@ public:
bool m_updated = false;
};
public:
char const *GetName() { return "<FileUpdateTester>"; }
FileUpdateTester() : BaseThreadManager(1) { }
~FileUpdateTester() { }
char const *GetName() { return "<FileUpdateTester>"; }
//-------------------------------------------------------------------------
FileUpdateTester::Status* RegisterFile(String const& path);
@@ -94,5 +94,97 @@ private:
map<String, Status*> m_files;
};
//AsyncImageJob ---------------------------------------------------------------
class AsyncImageJob : public ThreadJob
{
public:
AsyncImageJob()
: ThreadJob(ThreadJobType::NONE)
{
m_path = String();
}
AsyncImageJob(String path)
: ThreadJob(ThreadJobType::WORK_TODO)
{
m_path = path;
}
String const& GetPath() { return m_path; }
Image const& GetImage() { return m_image; }
protected:
virtual bool DoWork()
{
return m_image.Load(m_path.C());
}
String m_path;
Image m_image;
};
//AsyncImageLoader ------------------------------------------------------------
//Load images asynchronously, automatically updating the dummy image
class AsyncImageLoader : public BaseThreadManager
{
public:
char const *GetName() { return "<AsyncImageLoader>"; }
AsyncImageLoader(int thread_count)
: BaseThreadManager(thread_count, 0)
{
m_dummy_image.DummyFill();
}
~AsyncImageLoader()
{ }
//Returns a dummy image, and start a job to load the image on a thread
Image* Load(const lol::String& path)
{
//Create a job
AsyncImageJob* job = new AsyncImageJob(path);
//Create a dummy image
Image* image = new Image(m_dummy_image);
//Link the two
m_images[path] = image;
//Add job
AddWork(job);
//return Dummy image
return image;
}
bool CheckStatus(Image* image)
{
if (m_loaded_images.find(image) != INDEX_NONE)
{
m_loaded_images.remove_item(image);
return true;
}
return false;
}
protected:
virtual void TreatResult(ThreadJob* result)
{
AsyncImageJob* job = dynamic_cast<AsyncImageJob*>(result);
ASSERT(job);
//Copy image if work is OK
if (job->GetJobType() != ThreadJobType::WORK_FAILED)
{
Image* src = m_images[job->GetPath()];
m_images.remove(job->GetPath());
src->Copy(job->GetImage());
m_loaded_images.PushUnique(src);
}
else
{
Log::Error("AsyncImageJob FAILED. See load image error above.\n");
}
//Delete all that
delete(result);
}
private:
Image m_dummy_image;
map<String, Image*> m_images;
array<Image*> m_loaded_images;
};
} /* namespace lol */