2 * \file storage_object.h
3 * \brief CStorageObject
4 * \date 2012-08-18 09:02GMT
5 * \author Jan Boon (Kaetemi)
10 * Copyright (C) 2012 by authors
12 * This file is part of RYZOM CORE PIPELINE.
13 * RYZOM CORE PIPELINE is free software: you can redistribute it
14 * and/or modify it under the terms of the GNU Affero General Public
15 * License as published by the Free Software Foundation, either
16 * version 3 of the License, or (at your option) any later version.
18 * RYZOM CORE PIPELINE is distributed in the hope that it will be
19 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
20 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Affero General Public License for more details.
23 * You should have received a copy of the GNU Affero General Public
24 * License along with RYZOM CORE PIPELINE. If not, see
25 * <http://www.gnu.org/licenses/>.
28 #ifndef PIPELINE_STORAGE_OBJECT_H
29 #define PIPELINE_STORAGE_OBJECT_H
30 #include <nel/misc/types_nl.h>
36 #include <nel/misc/stream.h>
45 struct EStorage
: public NLMISC::Exception
47 EStorage() : NLMISC::Exception("PIPELINE::MAX::EStorage") { }
48 EStorage(const char *msg
) : NLMISC::Exception(msg
) { }
49 virtual ~EStorage() throw() { }
52 struct EStorageParse
: public EStorage
54 EStorageParse() : EStorage("PIPELINE::MAX::EStorageParse") { }
55 EStorageParse(const char *msg
) : EStorage(msg
) { }
56 virtual ~EStorageParse() throw() { }
61 PARSE_INTERNAL
= 0x00000001, // Directly parse basic class formats
62 PARSE_BUILTIN
= 0x00000002, // Parse all builtin classes
63 // PARSE_NELDATA = 0x00000004, // Parse all structures related to nel specific data (nel material, node properties, etcetera)
64 // PARSE_NEL3D = 0x00000008, // Parse classes to initialize their nel3d equivalent classes
67 // IStorageObject : exposes serial(CStorageStream &stream) and dump(const std::string &pad)
68 class IStorageObject
: public NLMISC::IStreamable
72 virtual ~IStorageObject();
74 virtual std::string
getClassName() { return className(); } // inherited from NLMISC::IClassable through NLMISC::IStreamable
75 virtual std::string
className() const = 0; // renamed for constness
76 // virtual void serial(NLMISC::IStream &stream); // inherited from NLMISC::IStreamable
77 std::string
toString();
78 virtual void toString(std::ostream
&ostream
, const std::string
&pad
= "") const = 0;
80 public: // should be protected but that doesn't compile, nice c++!
81 // Sets size when reading
82 virtual void setSize(sint32 size
);
83 // Gets the size when writing, return false if unknown
84 virtual bool getSize(sint32
&size
) const;
85 // Only true when inherting from CStorageContainer
86 virtual bool isContainer() const;
89 // CStorageContainer : serializes a container chunk
90 class CStorageContainer
: public IStorageObject
94 typedef std::pair
<uint16
, IStorageObject
*> TStorageObjectWithId
;
95 typedef std::list
<TStorageObjectWithId
> TStorageObjectContainer
;
96 typedef TStorageObjectContainer::iterator TStorageObjectIterator
;
97 typedef TStorageObjectContainer::const_iterator TStorageObjectConstIt
;
101 TStorageObjectContainer m_Chunks
;
102 bool m_ChunksOwnsPointers
;
106 virtual ~CStorageContainer();
109 virtual std::string
className() const;
110 virtual void serial(NLMISC::IStream
&stream
); // only used to wrap a container inside another stream
111 virtual void toString(std::ostream
&ostream
, const std::string
&pad
= "") const;
114 void serial(NLMISC::IStream
&stream
, uint size
); // without wrapping, known size
117 // Parse this class with given version and parse level filter
118 virtual void parse(uint16 version
, uint filter
= 0);
119 // Clean up built data or duplicate unparsed source data, call after serializing build and after parse
120 virtual void clean();
121 // Build the storage structure needed to store the parsed data back
122 virtual void build(uint16 version
, uint filter
= 0);
123 // Give ownership of the chunks back to the m_Chunks, must call build first, call instead of clean, reduces the parse level back to 0
124 virtual void disown();
128 inline const TStorageObjectContainer
&chunks() const { return m_Chunks
; }
129 IStorageObject
*findStorageObject(uint16 id
, uint nb
= 0) const; // find storage object with given id, nb count in case there are more
130 IStorageObject
*findLastStorageObject(uint16 id
) const;
132 public: // should be protected but that doesn't compile, nice c++!
134 virtual bool isContainer() const;
137 // override in subclasses, default to parent if not handled
138 virtual void serial(CStorageChunks
&chunks
);
139 // Create a storage object by id, override to provide custom serialization
140 virtual IStorageObject
*createChunkById(uint16 id
, bool container
);
144 // CStorageRaw : serializes raw data, use for unknown data
145 class CStorageRaw
: public IStorageObject
149 typedef std::vector
<uint8
> TType
;
154 virtual ~CStorageRaw();
157 virtual std::string
className() const;
158 virtual void serial(NLMISC::IStream
&stream
);
159 virtual void toString(std::ostream
&ostream
, const std::string
&pad
= "") const;
161 public: // should be protected but that doesn't compile, nice c++!
162 // Sets size when reading
163 virtual void setSize(sint32 size
);
164 // Gets the size when writing, return false if unknown
165 virtual bool getSize(sint32
&size
) const;
169 } /* namespace MAX */
170 } /* namespace PIPELINE */
172 #endif /* #ifndef PIPELINE_STORAGE_OBJECT_H */