diff --git a/components/nif/base.cpp b/components/nif/base.cpp index 8c63cd1a2a..ed440cd96d 100644 --- a/components/nif/base.cpp +++ b/components/nif/base.cpp @@ -19,14 +19,14 @@ namespace Nif if (nif->getVersion() < NIFStream::generateVersion(10, 0, 1, 0)) extra.read(nif); else - extralist.read(nif); + readRecordList(nif, extralist); controller.read(nif); } void Named::post(Reader& nif) { extra.post(nif); - extralist.post(nif); + postRecordList(nif, extralist); controller.post(nif); } } diff --git a/components/nif/controller.cpp b/components/nif/controller.cpp index 33e720cfe8..f05c6bcd7e 100644 --- a/components/nif/controller.cpp +++ b/components/nif/controller.cpp @@ -209,7 +209,7 @@ namespace Nif void NiMultiTargetTransformController::post(Reader& nif) { NiInterpController::post(nif); - mExtraTargets.post(nif); + postRecordList(nif, mExtraTargets); } void NiAlphaController::read(NIFStream* nif) @@ -251,7 +251,7 @@ namespace Nif { if (nif->getVersion() <= NIFFile::NIFVersion::VER_OB) { - mInterpolators.read(nif); + readRecordList(nif, mInterpolators); if (nif->getVersion() >= NIFStream::generateVersion(10, 2, 0, 0) && nif->getBethVersion() > 9) { unsigned int numUnknown = nif->getUInt(); @@ -279,7 +279,7 @@ namespace Nif { NiInterpController::post(nif); mData.post(nif); - mInterpolators.post(nif); + postRecordList(nif, mInterpolators); } void NiVisController::read(NIFStream* nif) @@ -304,13 +304,13 @@ namespace Nif timeStart = nif->getFloat(); mDelta = nif->getFloat(); } - mSources.read(nif); + readRecordList(nif, mSources); } void NiFlipController::post(Reader& nif) { NiFloatInterpController::post(nif); - mSources.post(nif); + postRecordList(nif, mSources); } void bhkBlendController::read(NIFStream* nif) diff --git a/components/nif/data.cpp b/components/nif/data.cpp index eb66cba5a8..7a6ea7b540 100644 --- a/components/nif/data.cpp +++ b/components/nif/data.cpp @@ -13,7 +13,7 @@ namespace Nif if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 101)) partitions.read(nif); root.read(nif); - bones.read(nif); + readRecordList(nif, bones); } void NiSkinInstance::post(Reader& nif) @@ -21,20 +21,19 @@ namespace Nif data.post(nif); partitions.post(nif); root.post(nif); - bones.post(nif); + postRecordList(nif, bones); if (data.empty() || root.empty()) throw Nif::Exception("NiSkinInstance missing root or data", nif.getFilename()); - size_t bnum = bones.length(); - if (bnum != data->bones.size()) + if (bones.size() != data->bones.size()) throw Nif::Exception("Mismatch in NiSkinData bone count", nif.getFilename()); - for (size_t i = 0; i < bnum; i++) + for (auto& bone : bones) { - if (bones[i].empty()) + if (bone.empty()) throw Nif::Exception("Oops: Missing bone! Don't know how to handle this.", nif.getFilename()); - bones[i]->setBone(); + bone->setBone(); } } diff --git a/components/nif/node.cpp b/components/nif/node.cpp index 51f822d079..66065aec32 100644 --- a/components/nif/node.cpp +++ b/components/nif/node.cpp @@ -84,7 +84,7 @@ namespace Nif if (nif->getVersion() <= NIFStream::generateVersion(4, 2, 2, 0)) velocity = nif->getVector3(); if (nif->getBethVersion() <= NIFFile::BethVersion::BETHVER_FO3) - props.read(nif); + readRecordList(nif, props); if (nif->getVersion() <= NIFStream::generateVersion(4, 2, 2, 0)) hasBounds = nif->getBoolean(); @@ -102,7 +102,7 @@ namespace Nif void Node::post(Reader& nif) { Named::post(nif); - props.post(nif); + postRecordList(nif, props); collision.post(nif); } @@ -114,9 +114,9 @@ namespace Nif void NiNode::read(NIFStream* nif) { Node::read(nif); - children.read(nif); + readRecordList(nif, children); if (nif->getBethVersion() < NIFFile::BethVersion::BETHVER_FO4) - effects.read(nif); + readRecordList(nif, effects); // Discard transformations for the root node, otherwise some meshes // occasionally get wrong orientation. Only for NiNode-s for now, but @@ -131,14 +131,14 @@ namespace Nif void NiNode::post(Reader& nif) { Node::post(nif); - children.post(nif); - effects.post(nif); + postRecordList(nif, children); + postRecordList(nif, effects); - for (size_t i = 0; i < children.length(); i++) + for (auto& child : children) { // Why would a unique list of children contain empty refs? - if (!children[i].empty()) - children[i]->parents.push_back(this); + if (!child.empty()) + child->parents.push_back(this); } } diff --git a/components/nif/physics.cpp b/components/nif/physics.cpp index 5ba080b845..9b58585c36 100644 --- a/components/nif/physics.cpp +++ b/components/nif/physics.cpp @@ -202,14 +202,14 @@ namespace Nif mGrowBy = nif->getUInt(); if (nif->getVersion() >= NIFStream::generateVersion(10, 1, 0, 0)) mScale = nif->getVector4(); - mData.read(nif); + readRecordList(nif, mData); unsigned int numFilters = nif->getUInt(); nif->getUInts(mFilters, numFilters); } void bhkNiTriStripsShape::post(Reader& nif) { - mData.post(nif); + postRecordList(nif, mData); } void bhkPackedNiTriStripsShape::read(NIFStream* nif) @@ -301,7 +301,7 @@ namespace Nif void bhkListShape::read(NIFStream* nif) { - mSubshapes.read(nif); + readRecordList(nif, mSubshapes); mHavokMaterial.read(nif); mChildShapeProperty.read(nif); mChildFilterProperty.read(nif); @@ -315,7 +315,7 @@ namespace Nif { bhkEntity::read(nif); mInfo.read(nif); - mConstraints.read(nif); + readRecordList(nif, mConstraints); if (nif->getBethVersion() < 76) mBodyFlags = nif->getUInt(); else diff --git a/components/nif/recordptr.hpp b/components/nif/recordptr.hpp index 3807382ee6..379fa8eaeb 100644 --- a/components/nif/recordptr.hpp +++ b/components/nif/recordptr.hpp @@ -85,39 +85,25 @@ namespace Nif implementation. */ template - class RecordListT + using RecordListT = std::vector>; + + template + void readRecordList(NIFStream* nif, RecordListT& list) { - typedef RecordPtrT Ptr; - std::vector list; + const int length = nif->getInt(); - public: - RecordListT() = default; + list.resize(static_cast(length)); - RecordListT(std::vector list) - : list(std::move(list)) - { - } + for (auto& value : list) + value.read(nif); + } - void read(NIFStream* nif) - { - int len = nif->getInt(); - list.resize(len); - - for (size_t i = 0; i < list.size(); i++) - list[i].read(nif); - } - - void post(Reader& nif) - { - for (size_t i = 0; i < list.size(); i++) - list[i].post(nif); - } - - const Ptr& operator[](size_t index) const { return list.at(index); } - Ptr& operator[](size_t index) { return list.at(index); } - - size_t length() const { return list.size(); } - }; + template + void postRecordList(Reader& nif, RecordListT& list) + { + for (auto& value : list) + value.post(nif); + } struct Node; struct Extra; diff --git a/components/nifbullet/bulletnifloader.cpp b/components/nifbullet/bulletnifloader.cpp index 7b6649f32a..fa3a8def6a 100644 --- a/components/nifbullet/bulletnifloader.cpp +++ b/components/nifbullet/bulletnifloader.cpp @@ -284,14 +284,9 @@ namespace NifBullet if (const Nif::NiNode* ninode = dynamic_cast(&node)) { const Nif::NodeList& list = ninode->children; - for (size_t i = 0; i < list.length(); i++) - { - if (!list[i].empty()) - { - if (findBoundingBox(list[i].get(), filename)) - return true; - } - } + for (const auto& child : list) + if (!child.empty() && findBoundingBox(child.get(), filename)) + return true; } return false; } @@ -300,12 +295,11 @@ namespace NifBullet { if (const Nif::NiNode* ninode = dynamic_cast(&rootNode)) { - const Nif::NodeList& list = ninode->children; - for (size_t i = 0; i < list.length(); i++) + for (const auto& child : ninode->children) { - if (list[i].empty()) + if (child.empty()) continue; - if (list[i].getPtr()->recType == Nif::RC_RootCollisionNode) + if (child.getPtr()->recType == Nif::RC_RootCollisionNode) return true; } } @@ -316,17 +310,16 @@ namespace NifBullet { if (const Nif::NiNode* ninode = dynamic_cast(&rootNode)) { - const Nif::NodeList& list = ninode->children; - for (size_t i = 0; i < list.length(); i++) + for (const auto& child : ninode->children) { - if (list[i].empty()) + if (child.empty()) continue; - const Nif::Node* childNode = list[i].getPtr(); + const Nif::Node* childNode = child.getPtr(); if (childNode->recType != Nif::RC_RootCollisionNode) continue; const Nif::NiNode* niChildnode = static_cast(childNode); // RootCollisionNode is always a NiNode - if (childNode->hasBounds || niChildnode->children.length() > 0) + if (childNode->hasBounds || niChildnode->children.size() > 0) return false; } } @@ -412,13 +405,13 @@ namespace NifBullet { const Nif::NodeList& list = ninode->children; const Nif::Parent currentParent{ *ninode, parent }; - for (size_t i = 0; i < list.length(); i++) + for (const auto& child : list) { - if (list[i].empty()) + if (child.empty()) continue; - assert(std::find(list[i]->parents.begin(), list[i]->parents.end(), ninode) != list[i]->parents.end()); - handleNode(fileName, list[i].get(), ¤tParent, flags, isCollisionNode, isAnimated, autogenerated, + assert(std::find(child->parents.begin(), child->parents.end(), ninode) != child->parents.end()); + handleNode(fileName, child.get(), ¤tParent, flags, isCollisionNode, isAnimated, autogenerated, avoid, visualCollisionType); } } diff --git a/components/nifosg/controller.cpp b/components/nifosg/controller.cpp index c2102a5345..ce5485ac53 100644 --- a/components/nifosg/controller.cpp +++ b/components/nifosg/controller.cpp @@ -199,7 +199,7 @@ namespace NifOsg GeomMorpherController::GeomMorpherController(const Nif::NiGeomMorpherController* ctrl) { - if (ctrl->mInterpolators.length() == 0) + if (ctrl->mInterpolators.size() == 0) { if (!ctrl->mData.empty()) { @@ -209,10 +209,10 @@ namespace NifOsg return; } - mKeyFrames.resize(ctrl->mInterpolators.length()); + mKeyFrames.resize(ctrl->mInterpolators.size()); mWeights = ctrl->mWeights; - for (size_t i = 0; i < ctrl->mInterpolators.length(); ++i) + for (std::size_t i = 0, n = ctrl->mInterpolators.size(); i < n; ++i) { if (!ctrl->mInterpolators[i].empty() && ctrl->mInterpolators[i]->recType == Nif::RC_NiFloatInterpolator) { diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index a295f3b280..2ad7a7ef09 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -76,9 +76,9 @@ namespace if (const Nif::NiNode* ninode = dynamic_cast(node)) { outIndices.push_back(ninode->recIndex); - for (unsigned int i = 0; i < ninode->children.length(); ++i) - if (!ninode->children[i].empty()) - getAllNiNodes(ninode->children[i].getPtr(), outIndices); + for (const auto& child : ninode->children) + if (!child.empty()) + getAllNiNodes(child.getPtr(), outIndices); } } @@ -102,18 +102,17 @@ namespace { if (parent != nullptr) collectDrawableProperties(&parent->mNiNode, parent->mParent, out); - const Nif::PropertyList& props = nifNode->props; - for (size_t i = 0; i < props.length(); ++i) + for (const auto& property : nifNode->props) { - if (!props[i].empty()) + if (!property.empty()) { - switch (props[i]->recType) + switch (property->recType) { case Nif::RC_NiMaterialProperty: case Nif::RC_NiVertexColorProperty: case Nif::RC_NiSpecularProperty: case Nif::RC_NiAlphaProperty: - out.push_back(props[i].getPtr()); + out.push_back(property.getPtr()); break; default: break; @@ -375,19 +374,17 @@ namespace NifOsg SceneUtil::CompositeStateSetUpdater* composite, Resource::ImageManager* imageManager, std::vector& boundTextures, int animflags) { - const Nif::PropertyList& props = nifNode->props; - bool hasStencilProperty = false; - for (size_t i = 0; i < props.length(); ++i) + for (const auto& property : nifNode->props) { - if (props[i].empty()) + if (property.empty()) continue; - if (props[i].getPtr()->recType == Nif::RC_NiStencilProperty) + if (property.getPtr()->recType == Nif::RC_NiStencilProperty) { const Nif::NiStencilProperty* stencilprop - = static_cast(props[i].getPtr()); + = static_cast(property.getPtr()); if (stencilprop->data.enabled != 0) { hasStencilProperty = true; @@ -396,24 +393,24 @@ namespace NifOsg } } - for (size_t i = 0; i < props.length(); ++i) + for (const auto& property : nifNode->props) { - if (!props[i].empty()) + if (!property.empty()) { // Get the lowest numbered recIndex of the NiTexturingProperty root node. // This is what is overridden when a spell effect "particle texture" is used. if (nifNode->parents.empty() && !mFoundFirstRootTexturingProperty - && props[i].getPtr()->recType == Nif::RC_NiTexturingProperty) + && property.getPtr()->recType == Nif::RC_NiTexturingProperty) { - mFirstRootTextureIndex = props[i].getPtr()->recIndex; + mFirstRootTextureIndex = property.getPtr()->recIndex; mFoundFirstRootTexturingProperty = true; } - else if (props[i].getPtr()->recType == Nif::RC_NiTexturingProperty) + else if (property.getPtr()->recType == Nif::RC_NiTexturingProperty) { - if (props[i].getPtr()->recIndex == mFirstRootTextureIndex) + if (property.getPtr()->recIndex == mFirstRootTextureIndex) applyTo->setUserValue("overrideFx", 1); } - handleProperty(props[i].getPtr(), applyTo, composite, imageManager, boundTextures, animflags, + handleProperty(property.getPtr(), applyTo, composite, imageManager, boundTextures, animflags, hasStencilProperty); } } @@ -463,13 +460,13 @@ namespace NifOsg const Nif::NiFltAnimationNode* niFltAnimationNode = static_cast(nifNode); osg::ref_ptr sequenceNode(new osg::Sequence); sequenceNode->setName(niFltAnimationNode->name); - if (niFltAnimationNode->children.length() != 0) + if (!niFltAnimationNode->children.empty()) { if (niFltAnimationNode->swing()) sequenceNode->setDefaultTime( - niFltAnimationNode->mDuration / (niFltAnimationNode->children.length() * 2)); + niFltAnimationNode->mDuration / (niFltAnimationNode->children.size() * 2)); else - sequenceNode->setDefaultTime(niFltAnimationNode->mDuration / niFltAnimationNode->children.length()); + sequenceNode->setDefaultTime(niFltAnimationNode->mDuration / niFltAnimationNode->children.size()); } return sequenceNode; } @@ -630,12 +627,9 @@ namespace NifOsg for (Nif::ExtraPtr e = nifNode->extra; !e.empty(); e = e->next) extraCollection.emplace_back(e); - for (size_t i = 0; i < nifNode->extralist.length(); ++i) - { - Nif::ExtraPtr e = nifNode->extralist[i]; - if (!e.empty()) - extraCollection.emplace_back(e); - } + for (const auto& extraNode : nifNode->extralist) + if (!extraNode.empty()) + extraCollection.emplace_back(extraNode); for (const auto& e : extraCollection) { @@ -805,20 +799,16 @@ namespace NifOsg if (ninode) { const Nif::NodeList& effects = ninode->effects; - for (size_t i = 0; i < effects.length(); ++i) - { - if (!effects[i].empty()) - handleEffect(effects[i].getPtr(), currentNode, imageManager); - } + for (const auto& effect : effects) + if (!effect.empty()) + handleEffect(effect.getPtr(), currentNode, imageManager); const Nif::NodeList& children = ninode->children; const Nif::Parent currentParent{ *ninode, parent }; - for (size_t i = 0; i < children.length(); ++i) - { - if (!children[i].empty()) - handleNode(children[i].getPtr(), ¤tParent, currentNode, imageManager, boundTextures, - animflags, skipMeshes, hasMarkers, hasAnimatedParents, textKeys, rootNode); - } + for (const auto& child : children) + if (!child.empty()) + handleNode(child.getPtr(), ¤tParent, currentNode, imageManager, boundTextures, animflags, + skipMeshes, hasMarkers, hasAnimatedParents, textKeys, rootNode); } if (nifNode->recType == Nif::RC_NiFltAnimationNode) @@ -1012,13 +1002,12 @@ namespace NifOsg wrapT = inherit->getWrap(osg::Texture2D::WRAP_T); } - for (unsigned int i = 0; i < flipctrl->mSources.length(); ++i) + for (const auto& source : flipctrl->mSources) { - Nif::NiSourceTexturePtr st = flipctrl->mSources[i]; - if (st.empty()) + if (source.empty()) continue; - osg::ref_ptr image(handleSourceTexture(st.getPtr(), imageManager)); + osg::ref_ptr image(handleSourceTexture(source.getPtr(), imageManager)); osg::ref_ptr texture(new osg::Texture2D(image)); if (image) texture->setTextureSize(image->s(), image->t()); @@ -1480,7 +1469,7 @@ namespace NifOsg const Nif::NiSkinInstance* skin = static_cast(nifNode)->skin.getPtr(); const Nif::NiSkinData* data = skin->data.getPtr(); const Nif::NodeList& bones = skin->bones; - for (size_t i = 0; i < bones.length(); i++) + for (std::size_t i = 0, n = bones.size(); i < n; ++i) { std::string boneName = Misc::StringUtils::lowerCase(bones[i].getPtr()->name);