From Jannik Heller and Robert Osfield, introduced ReadResult/WriteResult::statusMessage() method that creates a std::string from the stutus value and message string.

This commit is contained in:
Robert Osfield 2016-01-19 17:39:03 +00:00
parent 72cc10e309
commit 4deca2d38b
8 changed files with 99 additions and 105 deletions

View File

@ -161,6 +161,9 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
std::string& message() { return _message; }
const std::string& message() const { return _message; }
/// report the ReadResult's status, and message (if any). Useful for reporting of errors to users.
std::string statusMessage() const;
ReadStatus status() const { return _status; }
bool success() const { return _status==FILE_LOADED || _status==FILE_LOADED_FROM_CACHE ; }
bool loadedFromCache() const { return _status==FILE_LOADED_FROM_CACHE; }
@ -200,6 +203,9 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
std::string& message() { return _message; }
const std::string& message() const { return _message; }
/// Report the WriteResult's status, and message (if any). Useful for reporting of errors to users.
std::string statusMessage() const;
WriteStatus status() const { return _status; }
bool success() const { return _status==FILE_SAVED; }
bool error() const { return _status==ERROR_IN_WRITING_FILE; }

View File

@ -912,8 +912,7 @@ void DatabasePager::DatabaseThread::run()
osg::ref_ptr<osg::Node> loadedModel;
if (rr.validNode()) loadedModel = rr.getNode();
if (rr.error()) OSG_WARN<<"Error in reading file "<<fileName<<" : "<<rr.message() << std::endl;
if (rr.notEnoughMemory()) OSG_INFO<<"Not enought memory to load file "<<fileName << std::endl;
if (!rr.success()) OSG_WARN<<"Error in reading file "<<fileName<<" : "<<rr.statusMessage() << std::endl;
if (loadedModel.valid() &&
fileCache.valid() &&

View File

@ -813,7 +813,7 @@ osg::ref_ptr<osg::Image> InputStream::readImage(bool readFromExternal)
else
{
OSG_WARN << "InputStream::readImage(): "
<< rr.message() << std::endl;
<< rr.statusMessage() << std::endl;
}
}
else
@ -843,7 +843,7 @@ osg::ref_ptr<osg::Image> InputStream::readImage(bool readFromExternal)
}
else
{
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << rr.statusMessage() << std::endl;
}
if ( !image && _forceReadingImage ) image = new osg::Image;

View File

@ -34,7 +34,7 @@ Object* osgDB::readObjectFile(const std::string& filename,const Options* options
{
ReaderWriter::ReadResult rr = Registry::instance()->readObject(filename,options);
if (rr.validObject()) return rr.takeObject();
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
@ -43,7 +43,7 @@ Image* osgDB::readImageFile(const std::string& filename,const Options* options)
{
ReaderWriter::ReadResult rr = Registry::instance()->readImage(filename,options);
if (rr.validImage()) return rr.takeImage();
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
@ -51,7 +51,7 @@ Shader* osgDB::readShaderFile(const std::string& filename,const Options* options
{
ReaderWriter::ReadResult rr = Registry::instance()->readShader(filename,options);
if (rr.validShader()) return rr.takeShader();
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
@ -60,7 +60,7 @@ HeightField* osgDB::readHeightFieldFile(const std::string& filename,const Option
{
ReaderWriter::ReadResult rr = Registry::instance()->readHeightField(filename,options);
if (rr.validHeightField()) return rr.takeHeightField();
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
@ -69,8 +69,7 @@ Node* osgDB::readNodeFile(const std::string& filename,const Options* options)
{
ReaderWriter::ReadResult rr = Registry::instance()->readNode(filename,options);
if (rr.validNode()) return rr.takeNode();
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (rr.notEnoughMemory()) OSG_INFO << "Not enought memory to load file "<<filename << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
@ -89,7 +88,7 @@ Script* osgDB::readScriptFile(const std::string& filename,const Options* options
{
ReaderWriter::ReadResult rr = Registry::instance()->readScript(filename,options);
if (rr.validScript()) return rr.takeScript();
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
#endif
@ -98,7 +97,7 @@ osg::ref_ptr<osg::Object> osgDB::readRefObjectFile(const std::string& filename,c
{
ReaderWriter::ReadResult rr = Registry::instance()->readObject(filename,options);
if (rr.validObject()) return osg::ref_ptr<osg::Object>(rr.getObject());
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
@ -106,7 +105,7 @@ osg::ref_ptr<osg::Image> osgDB::readRefImageFile(const std::string& filename,con
{
ReaderWriter::ReadResult rr = Registry::instance()->readImage(filename,options);
if (rr.validImage()) return osg::ref_ptr<osg::Image>(rr.getImage());
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
@ -114,7 +113,7 @@ osg::ref_ptr<osg::Shader> osgDB::readRefShaderFile(const std::string& filename,c
{
ReaderWriter::ReadResult rr = Registry::instance()->readShader(filename,options);
if (rr.validShader()) return osg::ref_ptr<osg::Shader>(rr.getShader());
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
@ -122,7 +121,7 @@ osg::ref_ptr<osg::HeightField> osgDB::readRefHeightFieldFile(const std::string&
{
ReaderWriter::ReadResult rr = Registry::instance()->readHeightField(filename,options);
if (rr.validHeightField()) return osg::ref_ptr<osg::HeightField>(rr.getHeightField());
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
@ -130,7 +129,7 @@ osg::ref_ptr<osg::Node> osgDB::readRefNodeFile(const std::string& filename,const
{
ReaderWriter::ReadResult rr = Registry::instance()->readNode(filename,options);
if (rr.validNode()) return osg::ref_ptr<osg::Node>(rr.getNode());
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}
@ -138,7 +137,7 @@ osg::ref_ptr<osg::Script> osgDB::readRefScriptFile(const std::string& filename,c
{
ReaderWriter::ReadResult rr = Registry::instance()->readScript(filename,options);
if (rr.validScript()) return osg::ref_ptr<osg::Script>(rr.getScript());
if (rr.error()) OSG_WARN << rr.message() << std::endl;
if (!rr.success()) OSG_WARN << "Error reading file " << filename << ": " << rr.statusMessage() << std::endl;
return NULL;
}

View File

@ -35,6 +35,66 @@ osgDB::Archive* ReaderWriter::ReadResult::takeArchive() { osgDB::Archive* archiv
osg::Shader* ReaderWriter::ReadResult::takeShader() { osg::Shader* shader=dynamic_cast<osg::Shader*>(_object.get()); if (shader) { shader->ref(); _object=NULL; shader->unref_nodelete(); } return shader; }
osg::Script* ReaderWriter::ReadResult::takeScript() { osg::Script* script=dynamic_cast<osg::Script*>(_object.get()); if (script) { script->ref(); _object=NULL; script->unref_nodelete(); } return script; }
std::string ReaderWriter::ReadResult::statusMessage() const
{
std::string description;
switch (_status)
{
case NOT_IMPLEMENTED:
description += "not implemented";
break;
case FILE_NOT_HANDLED:
description += "file not handled";
break;
case FILE_NOT_FOUND:
description += "file not found";
break;
case ERROR_IN_READING_FILE:
description += "read error";
break;
case FILE_LOADED:
description += "file loaded";
break;
case FILE_LOADED_FROM_CACHE:
description += "file loaded from cache";
break;
case FILE_REQUESTED:
description += "file requested";
break;
case INSUFFICIENT_MEMORY_TO_LOAD:
description += "insufficient memory to load";
break;
}
if (!_message.empty())
description += " (" + _message + ")";
return description;
}
std::string ReaderWriter::WriteResult::statusMessage() const
{
std::string description;
switch (_status)
{
case NOT_IMPLEMENTED:
description += "not implemented";
break;
case FILE_NOT_HANDLED:
description += "file not handled";
break;
case ERROR_IN_WRITING_FILE:
description += "write error";
break;
case FILE_SAVED:
description += "file saved";
break;
}
if (!_message.empty())
description += " (" + _message + ")";
return description;
}
ReaderWriter::~ReaderWriter()
{
}

View File

@ -1216,30 +1216,19 @@ ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor)
}
else
{
return ReaderWriter::ReadResult("Warning: Could not find the .curl plugin to read from server.");
return ReaderWriter::ReadResult("Could not find the .curl plugin to read from server.");
}
}
if (results.empty())
{
return ReaderWriter::ReadResult("Warning: Could not find plugin to read objects from file \""+readFunctor._filename+"\".");
return ReaderWriter::ReadResult("Could not find plugin to read objects from file \""+readFunctor._filename+"\".");
}
// 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;
}
@ -1356,23 +1345,13 @@ ReaderWriter::WriteResult Registry::writeObjectImplementation(const Object& obj,
if (results.empty())
{
return ReaderWriter::WriteResult("Warning: Could not find plugin to write objects to file \""+fileName+"\".");
return ReaderWriter::WriteResult("Could not find plugin to write objects to file \""+fileName+"\".");
}
// 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(result.status())
{
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 result;
}
@ -1412,23 +1391,13 @@ ReaderWriter::WriteResult Registry::writeImageImplementation(const Image& image,
if (results.empty())
{
return ReaderWriter::WriteResult("Warning: Could not find plugin to write image to file \""+fileName+"\".");
return ReaderWriter::WriteResult("Could not find plugin to write image to file \""+fileName+"\".");
}
// 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(result.status())
{
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 result;
}
@ -1467,23 +1436,13 @@ ReaderWriter::WriteResult Registry::writeHeightFieldImplementation(const HeightF
if (results.empty())
{
return ReaderWriter::WriteResult("Warning: Could not find plugin to write HeightField to file \""+fileName+"\".");
return ReaderWriter::WriteResult("Could not find plugin to write HeightField to file \""+fileName+"\".");
}
// 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(result.status())
{
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 result;
}
@ -1536,23 +1495,13 @@ ReaderWriter::WriteResult Registry::writeNodeImplementation(const Node& node,con
if (results.empty())
{
return ReaderWriter::WriteResult("Warning: Could not find plugin to write nodes to file \""+fileName+"\".");
return ReaderWriter::WriteResult("Could not find plugin to write nodes to file \""+fileName+"\".");
}
// 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(result.status())
{
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 result;
}
@ -1592,22 +1541,13 @@ ReaderWriter::WriteResult Registry::writeShaderImplementation(const Shader& shad
if (results.empty())
{
return ReaderWriter::WriteResult("Warning: Could not find plugin to write shader to file \""+fileName+"\".");
return ReaderWriter::WriteResult("Could not find plugin to write shader to file \""+fileName+"\".");
}
// 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(result.status())
{
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 result;
}
@ -1646,23 +1586,13 @@ ReaderWriter::WriteResult Registry::writeScriptImplementation(const Script& imag
if (results.empty())
{
return ReaderWriter::WriteResult("Warning: Could not find plugin to write image to file \""+fileName+"\".");
return ReaderWriter::WriteResult("Could not find plugin to write image to file \""+fileName+"\".");
}
// 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(result.status())
{
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 result;
}

View File

@ -27,7 +27,7 @@ using namespace osgDB;
bool osgDB::writeObjectFile(const Object& object,const std::string& filename, const Options* options )
{
ReaderWriter::WriteResult wr = Registry::instance()->writeObject( object, filename, options );
if (wr.error()) OSG_WARN << "Error writing file " << filename << ": " << wr.message() << std::endl;
if (!wr.success()) OSG_WARN << "Error writing file " << filename << ": " << wr.statusMessage() << std::endl;
return wr.success();
}
@ -35,7 +35,7 @@ bool osgDB::writeObjectFile(const Object& object,const std::string& filename, co
bool osgDB::writeImageFile(const Image& image,const std::string& filename, const Options* options )
{
ReaderWriter::WriteResult wr = Registry::instance()->writeImage( image, filename, options );
if (wr.error()) OSG_WARN << "Error writing file " << filename << ": " << wr.message() << std::endl;
if (!wr.success()) OSG_WARN << "Error writing file " << filename << ": " << wr.statusMessage() << std::endl;
return wr.success();
}
@ -43,28 +43,28 @@ bool osgDB::writeImageFile(const Image& image,const std::string& filename, const
bool osgDB::writeHeightFieldFile(const HeightField& HeightField,const std::string& filename, const Options* options )
{
ReaderWriter::WriteResult wr = Registry::instance()->writeHeightField( HeightField, filename, options );
if (wr.error()) OSG_WARN << "Error writing file " << filename << ": " << wr.message() << std::endl;
if (!wr.success()) OSG_WARN << "Error writing file " << filename << ": " << wr.statusMessage() << std::endl;
return wr.success();
}
bool osgDB::writeNodeFile(const Node& node,const std::string& filename, const Options* options )
{
ReaderWriter::WriteResult wr = Registry::instance()->writeNode( node, filename, options );
if (wr.error()) OSG_WARN << "Error writing file " << filename << ": " << wr.message() << std::endl;
if (!wr.success()) OSG_WARN << "Error writing file " << filename << ": " << wr.statusMessage() << std::endl;
return wr.success();
}
bool osgDB::writeShaderFile(const Shader& shader,const std::string& filename, const Options* options )
{
ReaderWriter::WriteResult wr = Registry::instance()->writeShader( shader, filename, options );
if (wr.error()) OSG_WARN << "Error writing file " << filename << ": " << wr.message() << std::endl;
if (!wr.success()) OSG_WARN << "Error writing file " << filename << ": " << wr.statusMessage() << std::endl;
return wr.success();
}
bool osgDB::writeScriptFile(const Script& image,const std::string& filename, const Options* options )
{
ReaderWriter::WriteResult wr = Registry::instance()->writeScript( image, filename, options );
if (wr.error()) OSG_WARN << "Error writing file " << filename << ": " << wr.message() << std::endl;
if (!wr.success()) OSG_WARN << "Error writing file " << filename << ": " << wr.statusMessage() << std::endl;
return wr.success();
}

View File

@ -151,9 +151,9 @@ osgText::Font* osgText::readFontStream(std::istream& stream, const osgDB::Reader
osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension("ttf");
if (reader == 0) return 0;
osgDB::ReaderWriter::ReadResult rr = reader->readObject(stream, userOptions ? userOptions : localOptions.get());
if (rr.error())
if (!rr.success())
{
OSG_WARN << rr.message() << std::endl;
OSG_WARN << rr.statusMessage() << std::endl;
return 0;
}
if (!rr.validObject()) return 0;
@ -211,9 +211,9 @@ osg::ref_ptr<Font> osgText::readRefFontStream(std::istream& stream, const osgDB:
osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension("ttf");
if (reader == 0) return 0;
osgDB::ReaderWriter::ReadResult rr = reader->readObject(stream, userOptions ? userOptions : localOptions.get());
if (rr.error())
if (!rr.success())
{
OSG_WARN << rr.message() << std::endl;
OSG_WARN << rr.statusMessage() << std::endl;
return 0;
}
if (!rr.validObject()) return 0;