Leviathan  0.8.0.0
Leviathan game engine
Leviathan::ResourceRefreshHandler Class Reference

Allows various resource loaders to get notified when the file on disk changes. More...

#include <ResourceRefreshHandler.h>

+ Inheritance diagram for Leviathan::ResourceRefreshHandler:

Public Member Functions

DLLEXPORT ResourceRefreshHandler ()
 
virtual DLLEXPORT ~ResourceRefreshHandler ()
 
virtual DLLEXPORT bool Init ()
 
virtual DLLEXPORT void Release ()
 
DLLEXPORT bool ListenForFileChanges (const std::vector< const std::string *> &filestowatch, std::function< void(const std::string &, ResourceFolderListener &)> notifyfunction, int &createdid)
 Starts listening for changes made to filestowatch. More...
 
DLLEXPORT void StopListeningForFileChanges (int idoflistener)
 Stops a listener with a specific id. More...
 
DLLEXPORT void CheckFileStatus ()
 Called by Engine to check are updated files available. More...
 
DLLEXPORT void MarkListenersAsNotUpdated (const std::vector< int > &ids)
 Marks all files in all listeners matching any of the IDs as not updated. More...
 
- Public Member Functions inherited from Leviathan::ThreadSafeGeneric< MutexType >
DLLEXPORT ThreadSafeGeneric ()
 
DLLEXPORT ~ThreadSafeGeneric ()
 
FORCE_INLINE void VerifyLock (RecursiveLock &guard) const
 
FORCE_INLINE void VerifyLock (Lock &lockit) const
 

Static Public Member Functions

static DLLEXPORT ResourceRefreshHandlerGet ()
 

Protected Attributes

std::vector< std::unique_ptr< ResourceFolderListener > > ActiveFileListeners
 Holds all the active listeners. More...
 
WantedClockType::time_point NextUpdateTime
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Static Protected Attributes

static ResourceRefreshHandlerStaticaccess = NULL
 

Additional Inherited Members

- Public Types inherited from Leviathan::ThreadSafeGeneric< MutexType >
using LockT = typename LockTypeResolver< MutexType >::LType
 

Detailed Description

Allows various resource loaders to get notified when the file on disk changes.

Mainly used for quickly reloading GUI files after minor changes

Note
This class has lots of platform specific features which might not be available on non-windows platforms
Todo:
Combine listeners with the same file into a single thing

Definition at line 117 of file ResourceRefreshHandler.h.

Constructor & Destructor Documentation

◆ ResourceRefreshHandler()

DLLEXPORT Leviathan::ResourceRefreshHandler::ResourceRefreshHandler ( )

Definition at line 22 of file ResourceRefreshHandler.cpp.

22  {
23 
24 }

◆ ~ResourceRefreshHandler()

DLLEXPORT Leviathan::ResourceRefreshHandler::~ResourceRefreshHandler ( )
virtual

Definition at line 26 of file ResourceRefreshHandler.cpp.

26  {
28  "ResourceRefreshHandler should have been released before destructor");
29 }
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:92
static ResourceRefreshHandler * Staticaccess

Member Function Documentation

◆ CheckFileStatus()

DLLEXPORT void Leviathan::ResourceRefreshHandler::CheckFileStatus ( )

Called by Engine to check are updated files available.

Definition at line 102 of file ResourceRefreshHandler.cpp.

102  {
103  GUARD_LOCK();
104 
106  // Update all the file listeners //
107  for(size_t i = 0; i < ActiveFileListeners.size(); i++){
108 
109  ActiveFileListeners[i]->CheckUpdatesEnded();
110  }
111 
112  // Set new update time //
114  }
115 }
std::chrono::duration< int64_t, std::milli > MillisecondDuration
Definition: TimeIncludes.h:13
static DLLEXPORT WantedClockType::time_point GetThreadSafeSteadyTimePoint()
WantedClockType::time_point NextUpdateTime
std::vector< std::unique_ptr< ResourceFolderListener > > ActiveFileListeners
Holds all the active listeners.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ Get()

DLLEXPORT ResourceRefreshHandler * Leviathan::ResourceRefreshHandler::Get ( )
static

Definition at line 31 of file ResourceRefreshHandler.cpp.

31  {
32  return Staticaccess;
33 }
static ResourceRefreshHandler * Staticaccess

◆ Init()

DLLEXPORT bool Leviathan::ResourceRefreshHandler::Init ( )
virtual

Definition at line 37 of file ResourceRefreshHandler.cpp.

37  {
38  // Set the next update time //
40 
41  Staticaccess = this;
42  return true;
43 }
std::chrono::duration< int64_t, std::milli > MillisecondDuration
Definition: TimeIncludes.h:13
static DLLEXPORT WantedClockType::time_point GetThreadSafeSteadyTimePoint()
WantedClockType::time_point NextUpdateTime
static ResourceRefreshHandler * Staticaccess

◆ ListenForFileChanges()

DLLEXPORT bool Leviathan::ResourceRefreshHandler::ListenForFileChanges ( const std::vector< const std::string *> &  filestowatch,
std::function< void(const std::string &, ResourceFolderListener &)>  notifyfunction,
int &  createdid 
)

Starts listening for changes made to filestowatch.

Returns
True when properly started, false otherwise
Parameters
notifyfunctionThe function to call when one of the files change
filestowatchA vector of files to listen changes on, the pointers will not be deleted. The first file should contain a path to the folder which contains the files
createdidWill contain the ID of the created listener, useful for stopping listening when the caller of this function is de-allocated
Warning
This function will not work properly if all the files aren't in the same folder
Todo:
Allow watching for files in subfolders

Definition at line 61 of file ResourceRefreshHandler.cpp.

65 {
66 
67  unique_ptr<ResourceFolderListener> tmpcreated(new ResourceFolderListener(filestowatch,
68  notifyfunction));
69 
70  createdid = tmpcreated->GetID();
71 
72  if(!tmpcreated->StartListening())
73  return false;
74 
75  GUARD_LOCK();
76 
77  // Add it //
78  ActiveFileListeners.push_back(move(tmpcreated));
79 
80  return true;
81 }
std::vector< std::unique_ptr< ResourceFolderListener > > ActiveFileListeners
Holds all the active listeners.
A file listener instance which listens for file changes in a folder.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ MarkListenersAsNotUpdated()

DLLEXPORT void Leviathan::ResourceRefreshHandler::MarkListenersAsNotUpdated ( const std::vector< int > &  ids)

Marks all files in all listeners matching any of the IDs as not updated.

Definition at line 117 of file ResourceRefreshHandler.cpp.

119 {
120 
121  GUARD_LOCK();
122 
123  // Find any listeners matching any of the ids //
124  auto end = ActiveFileListeners.end();
125  for(auto iter = ActiveFileListeners.begin(); iter != end; ++iter){
126 
127  const int& curid = (*iter)->GetID();
128 
129  // Check against all the ids //
130  auto end2 = ids.end();
131  for(auto iter2 = ids.begin(); iter2 != end2; ++iter2){
132  if(curid == *iter2){
133 
134  (*iter)->MarkAllAsNotUpdated();
135  break;
136  }
137  }
138  }
139 }
std::vector< std::unique_ptr< ResourceFolderListener > > ActiveFileListeners
Holds all the active listeners.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ Release()

DLLEXPORT void Leviathan::ResourceRefreshHandler::Release ( )
virtual

Definition at line 45 of file ResourceRefreshHandler.cpp.

45  {
46  GUARD_LOCK();
47 
48  Staticaccess = NULL;
49 
50  // Release all listeners //
51  auto end = ActiveFileListeners.end();
52  for(auto iter = ActiveFileListeners.begin(); iter != end; ++iter){
53 
54  (*iter)->StopThread();
55  }
56 
57 
58  ActiveFileListeners.clear();
59 }
std::vector< std::unique_ptr< ResourceFolderListener > > ActiveFileListeners
Holds all the active listeners.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
static ResourceRefreshHandler * Staticaccess

◆ StopListeningForFileChanges()

DLLEXPORT void Leviathan::ResourceRefreshHandler::StopListeningForFileChanges ( int  idoflistener)

Stops a listener with a specific id.

Todo:
Implement this
Parameters
idoflistenerThe ID returned by WatchForFileChanges in createdid variable
See also
WatchForFileChanges

Definition at line 83 of file ResourceRefreshHandler.cpp.

85 {
86 
87  GUARD_LOCK();
88 
89  // Find the specific listener //
90  auto end = ActiveFileListeners.end();
91  for(auto iter = ActiveFileListeners.begin(); iter != end; ++iter){
92 
93  if((*iter)->GetID() == idoflistener){
94 
95  (*iter)->StopThread();
96  ActiveFileListeners.erase(iter);
97  return;
98  }
99  }
100 }
std::vector< std::unique_ptr< ResourceFolderListener > > ActiveFileListeners
Holds all the active listeners.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

Member Data Documentation

◆ ActiveFileListeners

std::vector<std::unique_ptr<ResourceFolderListener> > Leviathan::ResourceRefreshHandler::ActiveFileListeners
protected

Holds all the active listeners.

Definition at line 161 of file ResourceRefreshHandler.h.

◆ NextUpdateTime

WantedClockType::time_point Leviathan::ResourceRefreshHandler::NextUpdateTime
protected

Definition at line 164 of file ResourceRefreshHandler.h.

◆ Staticaccess

ResourceRefreshHandler * Leviathan::ResourceRefreshHandler::Staticaccess = NULL
staticprotected

Definition at line 166 of file ResourceRefreshHandler.h.


The documentation for this class was generated from the following files: