summaryrefslogtreecommitdiff
path: root/src/yapf/fixedsizearray.hpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-01-02 19:19:48 +0000
committerrubidium <rubidium@openttd.org>2007-01-02 19:19:48 +0000
commit66bbf336c6af7353ef0aeed58002c46543b30635 (patch)
treead4a63860df2626b22f77e7dac712e958bea54cb /src/yapf/fixedsizearray.hpp
parentccc0a3f4dbf58c005b22341ac8874252924690cd (diff)
downloadopenttd-66bbf336c6af7353ef0aeed58002c46543b30635.tar.xz
(svn r7759) -Merge: makefile rewrite. This merge features:
- A proper ./configure, so everything needs to be configured only once, not for every make. - Usage of makedepend when available. This greatly reduces the time needed for generating the dependencies. - A generator for all project files. There is a single file with sources, which is used to generate Makefiles and the project files for MSVC. - Proper support for OSX universal binaries. - Object files for non-MSVC compiles are also placed in separate directories, making is faster to switch between debug and release compiles and it does not touch the directory with the source files. - Functionality to make a bundle of all needed files for for example a nightly or distribution of a binary with all needed GRFs and language files. Note: as this merge moves almost all files, it is recommended to make a backup of your working copy before updating your working copy.
Diffstat (limited to 'src/yapf/fixedsizearray.hpp')
-rw-r--r--src/yapf/fixedsizearray.hpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/yapf/fixedsizearray.hpp b/src/yapf/fixedsizearray.hpp
new file mode 100644
index 000000000..48b177f3c
--- /dev/null
+++ b/src/yapf/fixedsizearray.hpp
@@ -0,0 +1,99 @@
+/* $Id$ */
+
+#ifndef FIXEDSIZEARRAY_HPP
+#define FIXEDSIZEARRAY_HPP
+
+
+/** fixed size array
+ * Upon construction it preallocates fixed size block of memory
+ * for all items, but doesn't construct them. Item's construction
+ * is delayed. */
+template <class Titem_, int Tcapacity_>
+struct CFixedSizeArrayT {
+ /** the only member of fixed size array is pointer to the block
+ * of C array of items. Header can be found on the offset -sizeof(CHdr). */
+ Titem_ *m_items;
+
+ /** header for fixed size array */
+ struct CHdr
+ {
+ int m_num_items; ///< number of items in the array
+ int m_ref_cnt; ///< block reference counter (used by copy constructor and by destructor)
+ };
+
+ // make types and constants visible from outside
+ typedef Titem_ Titem; // type of array item
+
+ static const int Tcapacity = Tcapacity_; // the array capacity (maximum size)
+ static const int TitemSize = sizeof(Titem_); // size of item
+ static const int ThdrSize = sizeof(CHdr); // size of header
+
+ /** Default constructor. Preallocate space for items and header, then initialize header. */
+ CFixedSizeArrayT()
+ {
+ // allocate block for header + items (don't construct items)
+ m_items = (Titem*)(((int8*)malloc(ThdrSize + Tcapacity * sizeof(Titem))) + ThdrSize);
+ SizeRef() = 0; // initial number of items
+ RefCnt() = 1; // initial reference counter
+ }
+
+ /** Copy constructor. Preallocate space for items and header, then initialize header. */
+ CFixedSizeArrayT(const CFixedSizeArrayT<Titem_, Tcapacity_>& src)
+ {
+ // share block (header + items) with the source array
+ m_items = src.m_items;
+ RefCnt()++; // now we share block with the source
+ }
+
+ /** destroy remaining items and free the memory block */
+ ~CFixedSizeArrayT()
+ {
+ // release one reference to the shared block
+ if ((--RefCnt()) > 0) return; // and return if there is still some owner
+
+ Clear();
+ // free the memory block occupied by items
+ free(((int8*)m_items) - ThdrSize);
+ m_items = NULL;
+ }
+
+ /** Clear (destroy) all items */
+ FORCEINLINE void Clear()
+ {
+ // walk through all allocated items backward and destroy them
+ for (Titem* pItem = &m_items[Size() - 1]; pItem >= m_items; pItem--) {
+ pItem->~Titem_();
+ }
+ // number of items become zero
+ SizeRef() = 0;
+ }
+
+protected:
+ /** return reference to the array header (non-const) */
+ FORCEINLINE CHdr& Hdr() { return *(CHdr*)(((int8*)m_items) - ThdrSize); }
+ /** return reference to the array header (const) */
+ FORCEINLINE const CHdr& Hdr() const { return *(CHdr*)(((int8*)m_items) - ThdrSize); }
+ /** return reference to the block reference counter */
+ FORCEINLINE int& RefCnt() { return Hdr().m_ref_cnt; }
+ /** return reference to number of used items */
+ FORCEINLINE int& SizeRef() { return Hdr().m_num_items; }
+public:
+ /** return number of used items */
+ FORCEINLINE int Size() const { return Hdr().m_num_items; }
+ /** return true if array is full */
+ FORCEINLINE bool IsFull() const { return Size() >= Tcapacity; };
+ /** return true if array is empty */
+ FORCEINLINE bool IsEmpty() const { return Size() <= 0; };
+ /** index validation */
+ FORCEINLINE void CheckIdx(int idx) const { assert(idx >= 0); assert(idx < Size()); }
+ /** add (allocate), but don't construct item */
+ FORCEINLINE Titem& AddNC() { assert(!IsFull()); return m_items[SizeRef()++]; }
+ /** add and construct item using default constructor */
+ FORCEINLINE Titem& Add() { Titem& item = AddNC(); new(&item)Titem; return item; }
+ /** return item by index (non-const version) */
+ FORCEINLINE Titem& operator [] (int idx) { CheckIdx(idx); return m_items[idx]; }
+ /** return item by index (const version) */
+ FORCEINLINE const Titem& operator [] (int idx) const { CheckIdx(idx); return m_items[idx]; }
+};
+
+#endif /* FIXEDSIZEARRAY_HPP */