aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/ArrayWithLength.h
blob: ab491aa804d50555619196a6cbcfb9feb09b5410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#pragma once

#include <assert.h>

//Note - this is meant to be a really simple wrapper round a pointer just to be able to add a length value to arrays.
// As such, it shouldn't delete its data in a destructor as shallow copies will be made of this and we don't want to
// free the data just because one of those has gone out of scope
template <class T> class arrayWithLength
{
public:
	T *data;
	unsigned int length;
	arrayWithLength() { data = nullptr; length = 0; }
	arrayWithLength(unsigned int elements, bool bClearArray=true) { assert(elements!=0); data = new T[elements];  if(bClearArray){ memset( data,0,sizeof(T)*elements); }  this->length = elements; }

	// 4J Stu Added this ctor so I static init arrays in the Item derivation tree
	arrayWithLength( T data[], unsigned int elements) { this->data = data; this->length = elements; }

	//~arrayWithLength() { delete[] data; }

	void resize( unsigned int elements )
	{
		assert( elements > length );
		T *temp = new T[elements];
		memset( temp,0,sizeof(T)*elements);

		if( data != nullptr )
		{
			std::copy( data, data+length, temp );

			delete[] data;
		}
		data = temp;
		length = elements;
	}

	T& operator[](unsigned int i) { return data[i]; }
	T  operator[](unsigned int i) const { return data[i]; }
};

// TODO 4J Stu - This looks right, but is it?
template <class T> class array2DWithLength
{
	typedef arrayWithLength< T >* _parrayWithLength;
public:
	_parrayWithLength *data;
	unsigned int length;
	array2DWithLength() { data = nullptr; length = 0; }
	array2DWithLength(unsigned int dimA, unsigned int dimB)
	{
		data = new _parrayWithLength[dimA];
		this->length = dimA;

		for( unsigned int i = 0; i < length; i++ )
			data[i] = new arrayWithLength<T>(dimB);
	}

	_parrayWithLength& operator[](unsigned int i) { return data[i]; }
	_parrayWithLength  operator[](unsigned int i) const { return data[i]; }
};


class Biome;
class LevelChunk;
class Node;
class Item;
class Tile;
class Stat;
class MobCategory;
class File;
class Vertex;
class _Polygon;		// Renaming as have conflict with Windows Polygon fn
class ServerLevel;
class MultiPlayerLevel;
class Level;
class LevelRenderer;
class WeighedRandomItem;
class WeighedTreasure;
class Layer;
//class Cube;
class ModelPart;
class Enchantment;
class ClipChunk;

typedef arrayWithLength<double> doubleArray;
typedef array2DWithLength<double> coords2DArray;
typedef arrayWithLength<byte> byteArray;
typedef arrayWithLength<char> charArray;
typedef arrayWithLength<short> shortArray;
typedef arrayWithLength<int> intArray;
typedef arrayWithLength<float> floatArray;
typedef arrayWithLength<Biome *> BiomeArray;
typedef arrayWithLength<LevelChunk *> LevelChunkArray;
typedef array2DWithLength<LevelChunk *> LevelChunk2DArray;
typedef arrayWithLength<Node *> NodeArray;
typedef arrayWithLength<Item *> ItemArray;
typedef arrayWithLength<Tile *> TileArray;
typedef arrayWithLength<Stat *> StatArray;
typedef arrayWithLength<MobCategory *> MobCategoryArray;
typedef arrayWithLength<File *> FileArray;
typedef arrayWithLength<Vertex *> VertexArray;
typedef arrayWithLength<_Polygon *> PolygonArray;
typedef arrayWithLength<ServerLevel *> ServerLevelArray;
typedef arrayWithLength<MultiPlayerLevel *> MultiPlayerLevelArray;
typedef arrayWithLength<Level *> LevelArray;
typedef arrayWithLength<LevelRenderer *> LevelRendererArray;
typedef arrayWithLength<WeighedRandomItem *> WeighedRandomItemArray;
typedef arrayWithLength<WeighedTreasure *> WeighedTreasureArray;
typedef arrayWithLength< shared_ptr<Layer> > LayerArray;
//typedef arrayWithLength<Cube *> CubeArray;
typedef arrayWithLength<ModelPart *> ModelPartArray;
typedef arrayWithLength<Enchantment *> EnchantmentArray;
typedef arrayWithLength<ClipChunk> ClipChunkArray;

#include "ItemInstance.h"
typedef arrayWithLength<shared_ptr<ItemInstance> > ItemInstanceArray;