Skip to content

Commit

Permalink
serialization error yields in an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed Feb 21, 2018
1 parent 6928d63 commit 8a119a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 76 deletions.
97 changes: 28 additions & 69 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -363,110 +363,69 @@ void CSGObject::print_serializable(const char* prefix)
}

bool CSGObject::save_serializable(CSerializableFile* file,
const char* prefix)
const char* prefix) throw (ShogunException)
{
SG_DEBUG("START SAVING CSGObject '%s'\n", get_name())
try
{
save_serializable_pre();
}
catch (ShogunException& e)
{
SG_SWARNING("%s%s::save_serializable_pre(): ShogunException: "
"%s\n", prefix, get_name(),
e.get_exception_string());
return false;
}
save_serializable_pre();

if (!m_save_pre_called)
{
SG_SWARNING("%s%s::save_serializable_pre(): Implementation "
"error: BASE_CLASS::SAVE_SERIALIZABLE_PRE() not "
"called!\n", prefix, get_name());
return false;
std::ostringstream err;
err << prefix << get_name() << "::save_serializable_pre():"
<< "Implementation error: BASE_CLASS::SAVE_SERIALIZABLE_PRE() not "
<< "called!" << std::endl;
throw ShogunException(err.str());
}

if (!m_parameters->save(file, prefix))
return false;
throw ShogunException("Error while serializing the object!");

try
{
save_serializable_post();
}
catch (ShogunException& e)
{
SG_SWARNING("%s%s::save_serializable_post(): ShogunException: "
"%s\n", prefix, get_name(),
e.get_exception_string());
return false;
}
save_serializable_post();

if (!m_save_post_called)
{
SG_SWARNING("%s%s::save_serializable_post(): Implementation "
"error: BASE_CLASS::SAVE_SERIALIZABLE_POST() not "
"called!\n", prefix, get_name());
return false;
std::ostringstream err;
err << prefix << get_name() << "::save_serializable_post():"
<< "Implementation error: BASE_CLASS::SAVE_SERIALIZABLE_POST() not "
<< "called!" << std::endl;
throw ShogunException(err.str());
}

if (prefix == NULL || *prefix == '\0')
file->close();

SG_DEBUG("DONE SAVING CSGObject '%s' (%p)\n", get_name(), this)

return true;
}

bool CSGObject::load_serializable(CSerializableFile* file,
const char* prefix)
const char* prefix) throw (ShogunException)
{
REQUIRE(file != NULL, "Serializable file object should be != NULL\n");

SG_DEBUG("START LOADING CSGObject '%s'\n", get_name())
try
{
load_serializable_pre();
}
catch (ShogunException& e)
{
SG_SWARNING("%s%s::load_serializable_pre(): ShogunException: "
"%s\n", prefix, get_name(),
e.get_exception_string());
return false;
}
load_serializable_pre();
if (!m_load_pre_called)
{
SG_SWARNING("%s%s::load_serializable_pre(): Implementation "
"error: BASE_CLASS::LOAD_SERIALIZABLE_PRE() not "
"called!\n", prefix, get_name());
return false;
std::ostringstream err;
err << prefix << get_name() << "::load_serializable_pre():"
<< "Implementation error: BASE_CLASS::LOAD_SERIALIZABLE_PRE() not "
<< "called!" << std::endl;
throw ShogunException(err.str());
}

if (!m_parameters->load(file, prefix))
return false;

try
{
load_serializable_post();
}
catch (ShogunException& e)
{
SG_SWARNING("%s%s::load_serializable_post(): ShogunException: "
"%s\n", prefix, get_name(),
e.get_exception_string());
return false;
}
throw ShogunException("Error while deserializing the object!");

load_serializable_post();
if (!m_load_post_called)
{
SG_SWARNING("%s%s::load_serializable_post(): Implementation "
"error: BASE_CLASS::LOAD_SERIALIZABLE_POST() not "
"called!\n", prefix, get_name());
return false;
std::ostringstream err;
err << prefix << get_name() << "::load_serializable_post():"
<< "Implementation error: BASE_CLASS::LOAD_SERIALIZABLE_POST() not "
<< "called!" << std::endl;
throw ShogunException(err.str());
}
SG_DEBUG("DONE LOADING CSGObject '%s' (%p)\n", get_name(), this)

return true;
}

void CSGObject::load_serializable_pre() throw (ShogunException)
Expand Down
13 changes: 6 additions & 7 deletions src/shogun/base/SGObject.h
Expand Up @@ -218,22 +218,21 @@ class CSGObject
* @param file where to save the object; will be closed during
* returning if PREFIX is an empty string.
* @param prefix prefix for members
* @return TRUE if done, otherwise FALSE
* @exception ShogunException will be thrown if an error occurs.
*/
virtual bool save_serializable(CSerializableFile* file,
const char* prefix="");
virtual void save_serializable(CSerializableFile* file,
const char* prefix="") throw (ShogunException);

/** Load this object from file. If it will fail (returning FALSE)
* then this object will contain inconsistent data and should not
* be used!
*
* @param file where to load from
* @param prefix prefix for members
*
* @return TRUE if done, otherwise FALSE
* @exception ShogunException will be thrown if an error occurs.
*/
virtual bool load_serializable(CSerializableFile* file,
const char* prefix="");
virtual void load_serializable(CSerializableFile* file,
const char* prefix="") throw (ShogunException);

/** set the io object
*
Expand Down

0 comments on commit 8a119a3

Please sign in to comment.