Skip to content

Commit

Permalink
Optimize AsyncHandler::RequestFile
Browse files Browse the repository at this point in the history
Also add some inlines
  • Loading branch information
mateofio committed Dec 21, 2019
1 parent 98a679d commit 6212152
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 49 deletions.
54 changes: 14 additions & 40 deletions src/async_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ namespace {
return nullptr;
}

void RegisterRequest(const std::string& path, const FileRequestAsync& request) {
async_requests[path] = request;
FileRequestAsync* RegisterRequest(std::string path, std::string directory, std::string file)
{
auto req = FileRequestAsync(path, std::move(directory), std::move(file));
auto p = async_requests.emplace(std::make_pair(std::move(path), std::move(req)));
return &p.first->second;
}

FileRequestBinding CreatePending() {
Expand Down Expand Up @@ -91,20 +94,17 @@ void AsyncHandler::CreateRequestMapping(const std::string& file) {
}

FileRequestAsync* AsyncHandler::RequestFile(const std::string& folder_name, const std::string& file_name) {
std::string path = FileFinder::MakePath(folder_name, file_name);
auto path = FileFinder::MakePath(folder_name, file_name);

FileRequestAsync* request = GetRequest(path);
auto* request = GetRequest(path);

if (request) {
return request;
}

FileRequestAsync req(folder_name, file_name);
RegisterRequest(path, req);

//Output::Debug("Waiting for %s", path.c_str());

return GetRequest(path);
return RegisterRequest(std::move(path), folder_name, file_name);
}

FileRequestAsync* AsyncHandler::RequestFile(const std::string& file_name) {
Expand Down Expand Up @@ -138,34 +138,12 @@ bool AsyncHandler::IsGraphicFilePending() {
return IsFilePending(false, true);
}

FileRequestAsync::FileRequestAsync(const std::string& folder_name, const std::string& file_name) :
directory(folder_name),
file(file_name) {
this->path = path = FileFinder::MakePath(folder_name, file_name);
this->important = false;
this->graphic = false;

state = State_WaitForStart;
}

FileRequestAsync::FileRequestAsync() {
}

bool FileRequestAsync::IsReady() const {
return state == State_DoneSuccess || state == State_DoneFailure;
}

bool FileRequestAsync::IsImportantFile() const {
return important;
}

void FileRequestAsync::SetImportantFile(bool important) {
this->important = important;
}

bool FileRequestAsync::IsGraphicFile() const {
return graphic;
}
FileRequestAsync::FileRequestAsync(std::string path, std::string directory, std::string file) :
directory(std::move(directory)),
file(std::move(file)),
path(std::move(path)),
state(State_WaitForStart)
{ }

void FileRequestAsync::SetGraphicFile(bool graphic) {
this->graphic = graphic;
Expand Down Expand Up @@ -252,10 +230,6 @@ void FileRequestAsync::UpdateProgress() {
#endif
}

const std::string& FileRequestAsync::GetPath() const {
return path;
}

FileRequestBinding FileRequestAsync::Bind(void(*func)(FileRequestResult*)) {
FileRequestBinding pending = CreatePending();

Expand Down
33 changes: 24 additions & 9 deletions src/async_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,13 @@ class FileRequestAsync {
State_Pending
};

/**
* Don't use this API directly. Use AsyncHandler::RequestFile.
*/
FileRequestAsync();

/**
* Don't use this API directly. Use AsyncHandler::RequestFile.
*
* @param folder_name folder where the file is stored.
* @param file_name Name of the requested file requested.
* @param path path to the file
* @return The async request.
*/
FileRequestAsync(const std::string& folder_name, const std::string& file_name);
FileRequestAsync(std::string path, std::string directory, std::string file);

/**
* Checks if a request finished.
Expand Down Expand Up @@ -215,7 +209,7 @@ class FileRequestAsync {
std::string directory;
std::string file;
std::string path;
int state;
int state = State_DoneFailure;
bool important = false;
bool graphic = false;
};
Expand All @@ -238,4 +232,25 @@ FileRequestBinding FileRequestAsync::Bind(void (T::*func)(FileRequestResult*, Ar
return Bind(f);
}

inline bool FileRequestAsync::IsReady() const {
return state == State_DoneSuccess || state == State_DoneFailure;
}

inline bool FileRequestAsync::IsImportantFile() const {
return important;
}

inline void FileRequestAsync::SetImportantFile(bool important) {
this->important = important;
}

inline bool FileRequestAsync::IsGraphicFile() const {
return graphic;
}

inline const std::string& FileRequestAsync::GetPath() const {
return path;
}


#endif

0 comments on commit 6212152

Please sign in to comment.