diff --git a/code/glTFAsset.h b/code/glTFAsset.h index 1511d28ea..6f407bf7f 100644 --- a/code/glTFAsset.h +++ b/code/glTFAsset.h @@ -754,6 +754,13 @@ namespace glTF virtual void WriteObjects(AssetWriter& writer) = 0; }; + //! (Stub class that is specialized in glTFAssetWriter.h) + template + struct LazyDictWriter + { + static void Write(T& d, AssetWriter& w) {} + }; + //! Manages lazy loading of the glTF top-level objects, and keeps a reference to them by ID //! It is the owner the loaded objects, so when it is destroyed it also deletes them template @@ -774,10 +781,8 @@ namespace glTF void AttachToDocument(Document& doc); void DetachFromDocument(); - void WriteObjectsImpl(AssetWriter& writer); - void WriteObjects(AssetWriter& writer) - { WriteObjectsImpl(writer); } + { LazyDictWriter< LazyDict >::Write(*this, writer); } Ref Add(T* obj); diff --git a/code/glTFAssetWriter.h b/code/glTFAssetWriter.h index 65d05320c..f934288b5 100644 --- a/code/glTFAssetWriter.h +++ b/code/glTFAssetWriter.h @@ -57,6 +57,9 @@ using rapidjson::MemoryPoolAllocator; class AssetWriter { + template + friend struct LazyDictWriter; + private: void WriteBinaryData(IOStream* outfile, size_t sceneLength); @@ -64,6 +67,9 @@ private: void WriteMetadata(); void WriteExtensionsUsed(); + template + void WriteObjects(LazyDict& d); + public: Document mDoc; Asset& mAsset; diff --git a/code/glTFAssetWriter.inl b/code/glTFAssetWriter.inl index 174565ef3..1d067809c 100644 --- a/code/glTFAssetWriter.inl +++ b/code/glTFAssetWriter.inl @@ -441,49 +441,57 @@ namespace glTF { mDoc.AddMember("extensionsUsed", exts, mAl); } - template - void LazyDict::WriteObjectsImpl(AssetWriter& w) + void AssetWriter::WriteObjects(LazyDict& d) { - if (mObjs.empty()) return; + if (d.mObjs.empty()) return; - Value* container = &w.mDoc; + Value* container = &mDoc; - if (mExtId) { - Value* exts = FindObject(w.mDoc, "extensions"); + if (d.mExtId) { + Value* exts = FindObject(mDoc, "extensions"); if (!exts) { - w.mDoc.AddMember("extensions", Value().SetObject().Move(), w.mDoc.GetAllocator()); - exts = FindObject(w.mDoc, "extensions"); + mDoc.AddMember("extensions", Value().SetObject().Move(), mDoc.GetAllocator()); + exts = FindObject(mDoc, "extensions"); } - if (!(container = FindObject(*exts, mExtId))) { - exts->AddMember(StringRef(mExtId), Value().SetObject().Move(), w.mDoc.GetAllocator()); - container = FindObject(*exts, mExtId); + if (!(container = FindObject(*exts, d.mExtId))) { + exts->AddMember(StringRef(d.mExtId), Value().SetObject().Move(), mDoc.GetAllocator()); + container = FindObject(*exts, d.mExtId); } } Value* dict; - if (!(dict = FindObject(*container, mDictId))) { - container->AddMember(StringRef(mDictId), Value().SetObject().Move(), w.mDoc.GetAllocator()); - dict = FindObject(*container, mDictId); + if (!(dict = FindObject(*container, d.mDictId))) { + container->AddMember(StringRef(d.mDictId), Value().SetObject().Move(), mDoc.GetAllocator()); + dict = FindObject(*container, d.mDictId); } - for (size_t i = 0; i < mObjs.size(); ++i) { - if (mObjs[i]->IsSpecial()) continue; + for (size_t i = 0; i < d.mObjs.size(); ++i) { + if (d.mObjs[i]->IsSpecial()) continue; Value obj; obj.SetObject(); - if (!mObjs[i]->name.empty()) { - obj.AddMember("name", StringRef(mObjs[i]->name.c_str()), w.mAl); + if (!d.mObjs[i]->name.empty()) { + obj.AddMember("name", StringRef(d.mObjs[i]->name.c_str()), mAl); } - Write(obj, *mObjs[i], w); + Write(obj, *d.mObjs[i], *this); - dict->AddMember(StringRef(mObjs[i]->id), obj, w.mAl); + dict->AddMember(StringRef(d.mObjs[i]->id), obj, mAl); } } + template + struct LazyDictWriter< LazyDict > + { + static void Write(LazyDict& d, AssetWriter& w) + { + w.WriteObjects(d); + } + }; + }