2 * \file storage_array.h
4 * \date 2012-08-21 11:33GMT
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_ARRAY_H
29 #define PIPELINE_STORAGE_ARRAY_H
30 #include <nel/misc/types_nl.h>
36 #include <nel/misc/ucstring.h>
37 #include <nel/misc/string_common.h>
40 #include "storage_object.h"
45 ////////////////////////////////////////////////////////////////////////
46 ////////////////////////////////////////////////////////////////////////
47 ////////////////////////////////////////////////////////////////////////
50 * \brief CStorageArray
51 * \date 2012-08-21 11:33GMT
52 * \author Jan Boon (Kaetemi)
53 * WARNING: sizeof(TType) should match the serialized size,
54 * otherwise you must specialize the getSize and setSize functions!
57 class CStorageArray
: public IStorageObject
62 typedef std::vector
<TType
> TTypeArray
;
66 virtual std::string
className() const;
67 virtual void serial(NLMISC::IStream
&stream
);
68 virtual void toString(std::ostream
&ostream
, const std::string
&pad
= "") const;
70 public: // should be protected but that doesn't compile, nice c++!
71 // Sets size when reading
72 virtual void setSize(sint32 size
);
73 // Gets the size when writing, return false if unknown
74 virtual bool getSize(sint32
&size
) const;
75 }; /* class CStorageArray */
78 std::string CStorageArray
<T
>::className() const
80 return "StorageArray";
84 void CStorageArray
<T
>::serial(NLMISC::IStream
&stream
)
86 for (typename
TTypeArray::iterator it
= Value
.begin(), end
= Value
.end(); it
!= end
; ++it
)
93 void CStorageArray
<T
>::toString(std::ostream
&ostream
, const std::string
&pad
) const
95 ostream
<< "(" << className() << ") [" << Value
.size() << "] { "; // << s << " } ";
97 for (typename
TTypeArray::const_iterator it
= Value
.begin(), end
= Value
.end(); it
!= end
; ++it
)
99 std::string s
= NLMISC::toString(*it
);
100 //ostream << "\n" << pad << i << ": " << s;
101 ostream
<< "{ " << s
<< " } ";
107 template <typename T
>
108 void CStorageArray
<T
>::setSize(sint32 size
)
110 if (size
% (sizeof(TType
)) != 0)
111 nlerror("Size %i is not a multiple of value type size %i", size
, sizeof(TType
));
112 Value
.resize(size
/ sizeof(TType
));
115 template <typename T
>
116 bool CStorageArray
<T
>::getSize(sint32
&size
) const
118 size
= Value
.size() * sizeof(TType
);
122 ////////////////////////////////////////////////////////////////////////
123 ////////////////////////////////////////////////////////////////////////
124 ////////////////////////////////////////////////////////////////////////
126 template <typename T
>
127 class CStorageArraySizePre
: public CStorageArray
<T
>
130 virtual std::string
className() const;
131 virtual void serial(NLMISC::IStream
&stream
);
132 virtual void setSize(sint32 size
);
133 virtual bool getSize(sint32
&size
) const;
136 template <typename T
>
137 std::string CStorageArraySizePre
<T
>::className() const
139 return "StorageArraySizePre";
142 template <typename T
>
143 void CStorageArraySizePre
<T
>::serial(NLMISC::IStream
&stream
)
145 uint32 size
= this->Value
.size();
147 nlassert(this->Value
.size() == size
);
148 CStorageArray
<T
>::serial(stream
);
151 template <typename T
>
152 void CStorageArraySizePre
<T
>::setSize(sint32 size
)
154 CStorageArray
<T
>::setSize(size
- sizeof(uint32
));
157 template <typename T
>
158 bool CStorageArraySizePre
<T
>::getSize(sint32
&size
) const
160 size
= CStorageArray
<T
>::getSize(size
) + sizeof(uint32
);
164 ////////////////////////////////////////////////////////////////////////
165 ////////////////////////////////////////////////////////////////////////
166 ////////////////////////////////////////////////////////////////////////
168 /// Same as CStorageArraySizePre but with no sizeof checks.
169 /// Use when serializing variable sizes in type T.
170 template <typename T
>
171 class CStorageArrayDynSize
: public CStorageArray
<T
>
174 virtual std::string
className() const;
175 virtual void serial(NLMISC::IStream
&stream
);
176 virtual void setSize(sint32 size
);
177 virtual bool getSize(sint32
&size
) const;
180 template <typename T
>
181 std::string CStorageArrayDynSize
<T
>::className() const
183 return "StorageArrayDynSize";
186 template <typename T
>
187 void CStorageArrayDynSize
<T
>::serial(NLMISC::IStream
&stream
)
189 uint32 size
= this->Value
.size();
191 this->Value
.resize(size
);
192 CStorageArray
<T
>::serial(stream
);
195 template <typename T
>
196 void CStorageArrayDynSize
<T
>::setSize(sint32 size
)
198 // Nothing to do here!
201 template <typename T
>
202 bool CStorageArrayDynSize
<T
>::getSize(sint32
&size
) const
204 // Nothing to do here!
208 ////////////////////////////////////////////////////////////////////////
209 ////////////////////////////////////////////////////////////////////////
210 ////////////////////////////////////////////////////////////////////////
212 } /* namespace MAX */
213 } /* namespace PIPELINE */
215 #endif /* #ifndef PIPELINE_STORAGE_ARRAY_H */