Refactored the way the ReadResult/WriteResult lists are handled, with them now being sorted so that the Read/WriteResult with highest numerical value ReadStatus/WriteStatus is returned.

Changed the enum order of ReadStatus/WriteStatus to ensure that the more relevant errors are last and with the highest numerical value
This commit is contained in:
Robert Osfield 2013-06-11 09:05:35 +00:00
parent bdfd18dc03
commit b0a28a5b2c
2 changed files with 70 additions and 121 deletions

View File

@ -109,9 +109,9 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
NOT_IMPLEMENTED, //!< read*() method not implemented in concrete ReaderWriter.
FILE_NOT_HANDLED, //!< File is not appropriate for this file reader, due to some incompatibility, but *not* a read error.
FILE_NOT_FOUND, //!< File could not be found or could not be read.
ERROR_IN_READING_FILE, //!< File found, loaded, but an error was encountered during processing.
FILE_LOADED, //!< File successfully found, loaded, and converted into osg.
FILE_LOADED_FROM_CACHE, //!< File found in cache and returned.
ERROR_IN_READING_FILE, //!< File found, loaded, but an error was encountered during processing.
FILE_REQUESTED, //!< Asynchronous file read has been requested, but returning immediately, keep polling plugin until file read has been completed.
INSUFFICIENT_MEMORY_TO_LOAD //!< File found but not loaded because estimated required memory surpasses available memory.
};
@ -123,6 +123,8 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
ReadResult(const ReadResult& rr):_status(rr._status),_message(rr._message),_object(rr._object) {}
ReadResult& operator = (const ReadResult& rr) { if (this==&rr) return *this; _status=rr._status; _message=rr._message;_object=rr._object; return *this; }
bool operator < (const ReadResult& rhs) const { return _status < rhs._status; }
osg::Object* getObject();
osg::Image* getImage();
osg::HeightField* getHeightField();
@ -171,8 +173,8 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
{
NOT_IMPLEMENTED, //!< write*() method not implemented in concrete ReaderWriter.
FILE_NOT_HANDLED,
FILE_SAVED,
ERROR_IN_WRITING_FILE
ERROR_IN_WRITING_FILE,
FILE_SAVED
};
WriteResult(WriteStatus status=FILE_NOT_HANDLED):_status(status) {}
@ -181,6 +183,8 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
WriteResult(const WriteResult& rr):_status(rr._status),_message(rr._message) {}
WriteResult& operator = (const WriteResult& rr) { if (this==&rr) return *this; _status=rr._status; _message=rr._message; return *this; }
bool operator < (const WriteResult& rhs) const { return _status < rhs._status; }
std::string& message() { return _message; }
const std::string& message() const { return _message; }

View File

@ -1166,52 +1166,6 @@ ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor)
}
}
if (!results.empty())
{
unsigned int num_FILE_NOT_HANDLED = 0;
unsigned int num_FILE_NOT_FOUND = 0;
unsigned int num_ERROR_IN_READING_FILE = 0;
Results::iterator ritr;
for(ritr=results.begin();
ritr!=results.end();
++ritr)
{
if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_HANDLED) ++num_FILE_NOT_HANDLED;
else if (ritr->status()==ReaderWriter::ReadResult::NOT_IMPLEMENTED) ++num_FILE_NOT_HANDLED;//Freetype and others
else if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND) ++num_FILE_NOT_FOUND;
else if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE) ++num_ERROR_IN_READING_FILE;
}
if (num_FILE_NOT_HANDLED!=results.size())
{
for(ritr=results.begin(); ritr!=results.end(); ++ritr)
{
if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE)
{
// OSG_NOTICE<<"Warning: error reading file \""<<readFunctor._filename<<"\""<<std::endl;
return *ritr;
}
}
//If the filename is a URL, don't return FILE_NOT_FOUND until the CURL plugin is given a chance
if (!osgDB::containsServerAddress(readFunctor._filename))
{
for(ritr=results.begin(); ritr!=results.end(); ++ritr)
{
if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND)
{
//OSG_NOTICE<<"Warning: could not find file \""<<readFunctor._filename<<"\""<<std::endl;
return *ritr;
}
}
}
}
}
results.clear();
// now look for a plug-in to load the file.
std::string libraryName = createLibraryNameForFile(readFunctor._filename);
if (loadLibrary(libraryName)!=NOT_LOADED)
@ -1243,49 +1197,27 @@ ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor)
}
}
if (!results.empty())
{
unsigned int num_FILE_NOT_HANDLED = 0;
unsigned int num_FILE_NOT_FOUND = 0;
unsigned int num_ERROR_IN_READING_FILE = 0;
Results::iterator ritr;
for(ritr=results.begin();
ritr!=results.end();
++ritr)
{
if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_HANDLED) ++num_FILE_NOT_HANDLED;
else if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND) ++num_FILE_NOT_FOUND;
else if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE) ++num_ERROR_IN_READING_FILE;
}
if (num_FILE_NOT_HANDLED!=results.size())
{
for(ritr=results.begin(); ritr!=results.end(); ++ritr)
{
if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE)
{
// OSG_NOTICE<<"Warning: error reading file \""<<readFunctor._filename<<"\""<<std::endl;
return *ritr;
}
}
for(ritr=results.begin(); ritr!=results.end(); ++ritr)
{
if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND)
{
// OSG_NOTICE<<"Warning: could not find file \""<<readFunctor._filename<<"\""<<std::endl;
return *ritr;
}
}
}
}
else
if (results.empty())
{
return ReaderWriter::ReadResult("Warning: Could not find plugin to read objects from file \""+readFunctor._filename+"\".");
}
return results.front();
// sort the results so the most relevant (i.e. ERROR_IN_READING_FILE is more relevant than FILE_NOT_FOUND) results get placed at the end of the results list.
std::sort(results.begin(), results.end());
ReaderWriter::ReadResult result = results.back();
if (result.message().empty())
{
switch(result.status())
{
case(ReaderWriter::ReadResult::FILE_NOT_HANDLED): result.message() = "Warning: reading \""+readFunctor._filename+"\" not supported."; break;
case(ReaderWriter::ReadResult::FILE_NOT_FOUND): result.message() = "Warning: could not find file \""+readFunctor._filename+"\"."; break;
case(ReaderWriter::ReadResult::ERROR_IN_READING_FILE): result.message() = "Warning: Error in reading to \""+readFunctor._filename+"\"."; break;
default: break;
}
}
return result;
}
ReaderWriter::ReadResult Registry::readImplementation(const ReadFunctor& readFunctor,Options::CacheHintOptions cacheHint)
@ -1392,17 +1324,21 @@ ReaderWriter::WriteResult Registry::writeObjectImplementation(const Object& obj,
return ReaderWriter::WriteResult("Warning: Could not find plugin to write objects to file \""+fileName+"\".");
}
if (results.front().message().empty())
// sort the results so the most relevant (i.e. ERROR_IN_WRITING_FILE is more relevant than FILE_NOT_FOUND) results get placed at the end of the results list.
std::sort(results.begin(), results.end());
ReaderWriter::WriteResult result = results.back();
if (result.message().empty())
{
switch(results.front().status())
switch(result.status())
{
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): results.front().message() = "Warning: Write to \""+fileName+"\" not supported."; break;
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): results.front().message() = "Warning: Error in writing to \""+fileName+"\"."; break;
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): result.message() = "Warning: Write to \""+fileName+"\" not supported."; break;
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): result.message() = "Warning: Error in writing to \""+fileName+"\"."; break;
default: break;
}
}
return results.front();
return result;
}
@ -1427,8 +1363,6 @@ ReaderWriter::WriteResult Registry::writeImageImplementation(const Image& image,
else results.push_back(rr);
}
results.clear();
// now look for a plug-in to save the file.
std::string libraryName = createLibraryNameForFile(fileName);
if (loadLibrary(libraryName)==LOADED)
@ -1446,17 +1380,21 @@ ReaderWriter::WriteResult Registry::writeImageImplementation(const Image& image,
return ReaderWriter::WriteResult("Warning: Could not find plugin to write image to file \""+fileName+"\".");
}
if (results.front().message().empty())
// sort the results so the most relevant (i.e. ERROR_IN_WRITING_FILE is more relevant than FILE_NOT_FOUND) results get placed at the end of the results list.
std::sort(results.begin(), results.end());
ReaderWriter::WriteResult result = results.back();
if (result.message().empty())
{
switch(results.front().status())
switch(result.status())
{
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): results.front().message() = "Warning: Write to \""+fileName+"\" not supported."; break;
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): results.front().message() = "Warning: Error in writing to \""+fileName+"\"."; break;
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): result.message() = "Warning: Write to \""+fileName+"\" not supported."; break;
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): result.message() = "Warning: Error in writing to \""+fileName+"\"."; break;
default: break;
}
}
return results.front();
return result;
}
@ -1480,8 +1418,6 @@ ReaderWriter::WriteResult Registry::writeHeightFieldImplementation(const HeightF
else results.push_back(rr);
}
results.clear();
// now look for a plug-in to save the file.
std::string libraryName = createLibraryNameForFile(fileName);
if (loadLibrary(libraryName)==LOADED)
@ -1499,17 +1435,21 @@ ReaderWriter::WriteResult Registry::writeHeightFieldImplementation(const HeightF
return ReaderWriter::WriteResult("Warning: Could not find plugin to write HeightField to file \""+fileName+"\".");
}
if (results.front().message().empty())
// sort the results so the most relevant (i.e. ERROR_IN_WRITING_FILE is more relevant than FILE_NOT_FOUND) results get placed at the end of the results list.
std::sort(results.begin(), results.end());
ReaderWriter::WriteResult result = results.back();
if (result.message().empty())
{
switch(results.front().status())
switch(result.status())
{
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): results.front().message() = "Warning: Write to \""+fileName+"\" not supported."; break;
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): results.front().message() = "Warning: Error in writing to \""+fileName+"\"."; break;
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): result.message() = "Warning: Write to \""+fileName+"\" not supported."; break;
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): result.message() = "Warning: Error in writing to \""+fileName+"\"."; break;
default: break;
}
}
return results.front();
return result;
}
@ -1545,12 +1485,9 @@ ReaderWriter::WriteResult Registry::writeNodeImplementation(const Node& node,con
else results.push_back(rr);
}
results.clear();
// now look for a plug-in to save the file.
std::string libraryName = createLibraryNameForFile(fileName);
if (loadLibrary(libraryName)==LOADED)
{
for(;itr.valid();++itr)
@ -1567,17 +1504,21 @@ ReaderWriter::WriteResult Registry::writeNodeImplementation(const Node& node,con
return ReaderWriter::WriteResult("Warning: Could not find plugin to write nodes to file \""+fileName+"\".");
}
if (results.front().message().empty())
// sort the results so the most relevant (i.e. ERROR_IN_WRITING_FILE is more relevant than FILE_NOT_FOUND) results get placed at the end of the results list.
std::sort(results.begin(), results.end());
ReaderWriter::WriteResult result = results.back();
if (result.message().empty())
{
switch(results.front().status())
switch(result.status())
{
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): results.front().message() = "Warning: Write to \""+fileName+"\" not supported."; break;
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): results.front().message() = "Warning: Error in writing to \""+fileName+"\"."; break;
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): result.message() = "Warning: Write to \""+fileName+"\" not supported."; break;
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): result.message() = "Warning: Error in writing to \""+fileName+"\"."; break;
default: break;
}
}
return results.front();
return result;
}
ReaderWriter::ReadResult Registry::readShaderImplementation(const std::string& fileName,const Options* options)
@ -1619,17 +1560,21 @@ ReaderWriter::WriteResult Registry::writeShaderImplementation(const Shader& shad
return ReaderWriter::WriteResult("Warning: Could not find plugin to write shader to file \""+fileName+"\".");
}
if (results.front().message().empty())
// sort the results so the most relevant (i.e. ERROR_IN_WRITING_FILE is more relevant than FILE_NOT_FOUND) results get placed at the end of the results list.
std::sort(results.begin(), results.end());
ReaderWriter::WriteResult result = results.back();
if (result.message().empty())
{
switch(results.front().status())
switch(result.status())
{
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): results.front().message() = "Warning: Write to \""+fileName+"\" not supported."; break;
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): results.front().message() = "Warning: Error in writing to \""+fileName+"\"."; break;
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): result.message() = "Warning: Write to \""+fileName+"\" not supported."; break;
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): result.message() = "Warning: Error in writing to \""+fileName+"\"."; break;
default: break;
}
}
return results.front();
return result;
}
void Registry::addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp)