/** * \file storage_array.h * \brief CStorageArray * \date 2012-08-21 11:33GMT * \author Jan Boon (Kaetemi) * CStorageArray */ /* * Copyright (C) 2012 by authors * * This file is part of RYZOM CORE PIPELINE. * RYZOM CORE PIPELINE is free software: you can redistribute it * and/or modify it under the terms of the GNU Affero General Public * License as published by the Free Software Foundation, either * version 3 of the License, or (at your option) any later version. * * RYZOM CORE PIPELINE is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public * License along with RYZOM CORE PIPELINE. If not, see * . */ #ifndef PIPELINE_STORAGE_ARRAY_H #define PIPELINE_STORAGE_ARRAY_H #include // STL includes #include // NeL includes #include #include // Project includes #include "storage_object.h" namespace PIPELINE { namespace MAX { //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// /** * \brief CStorageArray * \date 2012-08-21 11:33GMT * \author Jan Boon (Kaetemi) * WARNING: sizeof(TType) should match the serialized size, * otherwise you must specialize the getSize and setSize functions! */ template class CStorageArray : public IStorageObject { public: // public data typedef T TType; typedef std::vector TTypeArray; TTypeArray Value; // inherited virtual std::string className() const; virtual void serial(NLMISC::IStream &stream); virtual void toString(std::ostream &ostream, const std::string &pad = "") const; public: // should be protected but that doesn't compile, nice c++! // Sets size when reading virtual void setSize(sint32 size); // Gets the size when writing, return false if unknown virtual bool getSize(sint32 &size) const; }; /* class CStorageArray */ template std::string CStorageArray::className() const { return "StorageArray"; } template void CStorageArray::serial(NLMISC::IStream &stream) { for (typename TTypeArray::iterator it = Value.begin(), end = Value.end(); it != end; ++it) { stream.serial(*it); } } template void CStorageArray::toString(std::ostream &ostream, const std::string &pad) const { ostream << "(" << className() << ") [" << Value.size() << "] { "; // << s << " } "; uint i = 0; for (typename TTypeArray::const_iterator it = Value.begin(), end = Value.end(); it != end; ++it) { std::string s = NLMISC::toString(*it); //ostream << "\n" << pad << i << ": " << s; ostream << "{ " << s << " } "; ++i; } ostream << "} "; } template void CStorageArray::setSize(sint32 size) { if (size % (sizeof(TType)) != 0) nlerror("Size %i is not a multiple of value type size %i", size, sizeof(TType)); Value.resize(size / sizeof(TType)); } template bool CStorageArray::getSize(sint32 &size) const { size = Value.size() * sizeof(TType); return true; } //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// template class CStorageArraySizePre : public CStorageArray { public: virtual std::string className() const; virtual void serial(NLMISC::IStream &stream); virtual void setSize(sint32 size); virtual bool getSize(sint32 &size) const; }; template std::string CStorageArraySizePre::className() const { return "StorageArraySizePre"; } template void CStorageArraySizePre::serial(NLMISC::IStream &stream) { uint32 size = this->Value.size(); stream.serial(size); nlassert(this->Value.size() == size); CStorageArray::serial(stream); } template void CStorageArraySizePre::setSize(sint32 size) { CStorageArray::setSize(size - sizeof(uint32)); } template bool CStorageArraySizePre::getSize(sint32 &size) const { size = CStorageArray::getSize(size) + sizeof(uint32); return true; } //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// /// Same as CStorageArraySizePre but with no sizeof checks. /// Use when serializing variable sizes in type T. template class CStorageArrayDynSize : public CStorageArray { public: virtual std::string className() const; virtual void serial(NLMISC::IStream &stream); virtual void setSize(sint32 size); virtual bool getSize(sint32 &size) const; }; template std::string CStorageArrayDynSize::className() const { return "StorageArrayDynSize"; } template void CStorageArrayDynSize::serial(NLMISC::IStream &stream) { uint32 size = this->Value.size(); stream.serial(size); this->Value.resize(size); CStorageArray::serial(stream); } template void CStorageArrayDynSize::setSize(sint32 size) { // Nothing to do here! } template bool CStorageArrayDynSize::getSize(sint32 &size) const { // Nothing to do here! return false; } //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// } /* namespace MAX */ } /* namespace PIPELINE */ #endif /* #ifndef PIPELINE_STORAGE_ARRAY_H */ /* end of file */