Added: #1440 Templates for storage values

--HG--
branch : build_pipeline_v3
hg/feature/build_pipeline_v3
kaetemi 12 years ago
parent cfb1d808cc
commit 274ed370af

@ -157,13 +157,17 @@ void CStorageContainer::serial(CStorageChunks &chunks)
while (chunks.enterChunk())
{
uint16 id = chunks.getChunkId();
IStorageObject *storageObject = createChunkById(id, chunks.isChunkContainer());
bool cont = chunks.isChunkContainer();
IStorageObject *storageObject = createChunkById(id, cont);
storageObject->setSize(chunks.getChunkSize());
if (storageObject->isContainer()) static_cast<CStorageContainer *>(storageObject)->serial(chunks);
else storageObject->serial(chunks.stream());
Chunks.push_back(TStorageObjectWithId(id, storageObject));
if (chunks.leaveChunk()) // bytes were skipped while reading
throw EStorage();
TStorageObjectContainer::iterator soit = Chunks.end();
--soit;
serialized(soit, cont);
}
}
else
@ -189,6 +193,11 @@ IStorageObject *CStorageContainer::createChunkById(uint16 id, bool container)
}
}
void CStorageContainer::serialized(TStorageObjectContainer::iterator soit, bool container)
{
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

@ -71,7 +71,7 @@ class CStorageContainer : public IStorageObject
public:
// public data
typedef std::pair<uint16, IStorageObject *> TStorageObjectWithId;
typedef std::vector<TStorageObjectWithId> TStorageObjectContainer;
typedef std::list<TStorageObjectWithId> TStorageObjectContainer;
TStorageObjectContainer Chunks;
// inherited
@ -86,7 +86,10 @@ public: // should be protected but that doesn't compile, nice c++!
protected:
// override in subclasses, default to parent if not handled
virtual void serial(CStorageChunks &chunks);
// Create a storage object by id, override to provide custom serialization
virtual IStorageObject *createChunkById(uint16 id, bool container);
// Callback when a storage object has been serialized and put in the chunks list, override to index them
virtual void serialized(TStorageObjectContainer::iterator soit, bool container);
};
// CStorageRaw : serializes raw data, use for unknown data

@ -0,0 +1,47 @@
/**
* \file storage_value.cpp
* \brief CStorageValue
* \date 2012-08-18 15:00GMT
* \author Jan Boon (Kaetemi)
* CStorageValue
*/
/*
* 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
* <http://www.gnu.org/licenses/>.
*/
#include <nel/misc/types_nl.h>
#include "storage_value.h"
// STL includes
// NeL includes
// #include <nel/misc/debug.h>
// Project includes
using namespace std;
// using namespace NLMISC;
namespace PIPELINE {
void dummy_storage_value_cpp() { }
} /* namespace PIPELINE */
/* end of file */

@ -0,0 +1,137 @@
/**
* \file storage_value.h
* \brief CStorageValue
* \date 2012-08-18 15:00GMT
* \author Jan Boon (Kaetemi)
* CStorageValue
*/
/*
* 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
* <http://www.gnu.org/licenses/>.
*/
#ifndef PIPELINE_STORAGE_VALUE_H
#define PIPELINE_STORAGE_VALUE_H
#include <nel/misc/types_nl.h>
// STL includes
// NeL includes
#include <nel/misc/ucstring.h>
// Project includes
#include "storage_object.h"
namespace PIPELINE {
namespace MAX {
template<typename T>
class CStorageValue : public IStorageObject
{
public:
// public data
typedef T TType;
TType Value;
// inherited
virtual std::string getClassName();
virtual void serial(NLMISC::IStream &stream);
virtual void toString(std::ostream &ostream, const std::string &pad = "");
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;
};
template <typename T>
std::string getClassName()
{
return "CStorageValue";
}
template <typename T>
void CStorageValue<T>::serial(NLMISC::IStream &stream)
{
stream.serial(Value);
}
template <>
void CStorageValue<std::string>::serial(NLMISC::IStream &stream)
{
stream.serialBuffer(static_cast<uint8 *>(static_cast<void *>(&Value[0])), Value.size());
}
template <>
void CStorageValue<ucstring>::serial(NLMISC::IStream &stream)
{
stream.serialBuffer(static_cast<uint8 *>(static_cast<void *>(&Value[0])), Value.size() * 2);
}
template <typename T>
void CStorageValue<T>::toString(std::ostream &ostream, const std::string &pad)
{
ostream << "(" << getClassName() << ") { " << Value << " } ";
}
template <typename T>
void CStorageValue<T>::setSize(sint32 size)
{
if (size != sizeof(Value))
nlerror("Size does not match value type");
IStorageObject::setSize(size);
}
template <>
void CStorageValue<std::string>::setSize(sint32 size)
{
Value.resize(size);
}
template <>
void CStorageValue<ucstring>::setSize(sint32 size)
{
Value.resize(size / 2);
}
template <typename T>
bool CStorageValue<T>::getSize(sint32 &size) const
{
size = sizeof(Value);
return true;
}
template <>
bool CStorageValue<std::string>::getSize(sint32 &size) const
{
return Value.size();
}
template <>
bool CStorageValue<ucstring>::getSize(sint32 &size) const
{
return Value.size() * 2;
}
} /* namespace MAX */
} /* namespace PIPELINE */
#endif /* #ifndef PIPELINE_STORAGE_VALUE_H */
/* end of file */
Loading…
Cancel
Save