mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Rewrite STObject::setType for improved performance
This commit is contained in:
@@ -154,30 +154,35 @@ bool STObject::setType (const SOTemplate& type)
|
||||
|
||||
mType = &type;
|
||||
|
||||
for (SOTemplate::value_type const& elem : type.peek ())
|
||||
SerializedType** array = mData.c_array();
|
||||
std::size_t count = mData.size ();
|
||||
|
||||
for (auto const& elem : type.peek ())
|
||||
{
|
||||
// Loop through all the fields in the template
|
||||
bool match = false;
|
||||
|
||||
for (boost::ptr_vector<SerializedType>::iterator it = mData.begin (); it != mData.end (); ++it)
|
||||
if (it->getFName () == elem->e_field)
|
||||
for (std::size_t i = 0; i < count; ++i)
|
||||
if ((array[i] != nullptr) && (array[i]->getFName () == elem->e_field))
|
||||
{
|
||||
// matching entry, move to new vector
|
||||
// matching entry in the object, move to new vector
|
||||
match = true;
|
||||
|
||||
if ((elem->flags == SOE_DEFAULT) && it->isDefault ())
|
||||
if ((elem->flags == SOE_DEFAULT) && array[i]->isDefault ())
|
||||
{
|
||||
WriteLog (lsWARNING, STObject) << "setType( " << getFName ().getName () << ") invalid default "
|
||||
<< elem->e_field.fieldName;
|
||||
valid = false;
|
||||
}
|
||||
|
||||
newData.push_back (mData.release (it).release ()); // CAUTION: This renders 'it' invalid
|
||||
newData.push_back (array[i]);
|
||||
array[i] = nullptr;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!match)
|
||||
{
|
||||
// no match found
|
||||
// no match found in the object for an entry in the template
|
||||
if (elem->flags == SOE_REQUIRED)
|
||||
{
|
||||
WriteLog (lsWARNING, STObject) << "setType( " << getFName ().getName () << ") invalid missing "
|
||||
@@ -185,22 +190,26 @@ bool STObject::setType (const SOTemplate& type)
|
||||
valid = false;
|
||||
}
|
||||
|
||||
// Make a default object
|
||||
newData.push_back (makeNonPresentObject (elem->e_field).release ());
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_FOREACH (const SerializedType & t, mData)
|
||||
for (std::size_t i = 0; i < count; ++i)
|
||||
{
|
||||
// Anything left over must be discardable
|
||||
if (!t.getFName ().isDiscardable ())
|
||||
// Anything left over in the object must be discardable
|
||||
if ((array[i] != nullptr) && !array[i]->getFName ().isDiscardable ())
|
||||
{
|
||||
WriteLog (lsWARNING, STObject) << "setType( " << getFName ().getName () << ") invalid leftover "
|
||||
<< t.getFName ().getName ();
|
||||
<< array[i]->getFName ().getName ();
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Swap the template matching data in for the old data,
|
||||
// freeing any leftover junk
|
||||
mData.swap (newData);
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user