File: RegisterArray.h

package info (click to toggle)
jazz2-native 3.5.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 16,836 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (127 lines) | stat: -rw-r--r-- 4,209 bytes parent folder | download | duplicates (3)
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
117
118
119
120
121
122
123
124
125
126
127
#pragma once

#if defined(WITH_ANGELSCRIPT) || defined(DOXYGEN_GENERATING_OUTPUT)

#include <angelscript.h>

namespace Jazz2::Scripting
{
	struct SArrayBuffer;
	struct SArrayCache;

	/** @brief **AngelScript** array */
	class CScriptArray
	{
	public:
		// Factory functions
		static CScriptArray* Create(asITypeInfo* ot);
		static CScriptArray* Create(asITypeInfo* ot, std::uint32_t length);
		static CScriptArray* Create(asITypeInfo* ot, std::uint32_t length, void* defaultValue);
		static CScriptArray* Create(asITypeInfo* ot, void* listBuffer);

		// Memory management
		void AddRef() const;
		void Release() const;

		// Type information
		asITypeInfo* GetArrayObjectType() const;
		std::int32_t GetArrayTypeId() const;
		std::int32_t GetElementTypeId() const;

		// Get the current size
		std::uint32_t GetSize() const;

		// Returns true if the array is empty
		bool IsEmpty() const;

		// Pre-allocates memory for elements
		void Reserve(std::uint32_t maxElements);

		// Resize the array
		void Resize(std::uint32_t numElements);

		// Get a pointer to an element. Returns 0 if out of bounds
		void* At(std::uint32_t index);
		const void* At(std::uint32_t index) const;

		// Set value of an element. 
		// The value arg should be a pointer to the value that will be copied to the element.
		// Remember, if the array holds handles the value parameter should be the 
		// address of the handle. The refCount of the object will also be incremented
		void SetValue(std::uint32_t index, void* value);

		// Copy the contents of one array to another (only if the types are the same)
		CScriptArray& operator=(const CScriptArray&);

		// Compare two arrays
		bool operator==(const CScriptArray&) const;

		// Array manipulation
		void InsertAt(std::uint32_t index, void* value);
		void InsertAt(std::uint32_t index, const CScriptArray& arr);
		void InsertLast(void* value);
		void RemoveAt(std::uint32_t index);
		void RemoveLast();
		void RemoveRange(std::uint32_t start, std::uint32_t count);
		void SortAsc();
		void SortDesc();
		void SortAsc(std::uint32_t startAt, std::uint32_t count);
		void SortDesc(std::uint32_t startAt, std::uint32_t count);
		void Sort(std::uint32_t startAt, std::uint32_t count, bool asc);
		void Sort(asIScriptFunction* less, std::uint32_t startAt, std::uint32_t count);
		void Reverse();
		int Find(void* value) const;
		int Find(std::uint32_t startAt, void* value) const;
		int FindByRef(void* ref) const;
		int FindByRef(std::uint32_t startAt, void* ref) const;

		// Return the address of internal buffer for direct manipulation of elements
		void* GetBuffer();

		// GC methods
		std::int32_t GetRefCount();
		void SetFlag();
		bool GetFlag();
		void EnumReferences(asIScriptEngine* engine);
		void ReleaseAllHandles(asIScriptEngine* engine);

	protected:
#ifndef DOXYGEN_GENERATING_OUTPUT
		mutable std::int32_t refCount;
		mutable bool gcFlag;
		asITypeInfo* objType;
		SArrayBuffer* buffer;
		std::int32_t elementSize;
		std::int32_t subTypeId;
#endif

		// Constructors
		CScriptArray(asITypeInfo* ot, void* initBuf); // Called from script when initialized with list
		CScriptArray(std::uint32_t length, asITypeInfo* ot);
		CScriptArray(std::uint32_t length, void* defVal, asITypeInfo* ot);
		CScriptArray(const CScriptArray& other);
		virtual ~CScriptArray();

		bool  Less(const void* a, const void* b, bool asc);
		void* GetArrayItemPointer(std::int32_t index);
		void* GetDataPointer(void* buffer);
		void  Copy(void* dst, void* src);
		void  Swap(void* a, void* b);
		void  Precache();
		bool  CheckMaxSize(std::uint32_t numElements);
		void  Resize(std::int32_t delta, std::uint32_t at);
		void  CreateBuffer(SArrayBuffer** buf, std::uint32_t numElements);
		void  DeleteBuffer(SArrayBuffer* buf);
		void  CopyBuffer(SArrayBuffer* dst, SArrayBuffer* src);
		void  Construct(SArrayBuffer* buf, std::uint32_t start, std::uint32_t end);
		void  Destruct(SArrayBuffer* buf, std::uint32_t start, std::uint32_t end);
		bool  Equals(const void* a, const void* b, asIScriptContext* ctx, SArrayCache* cache) const;
	};

	/** @relatesalso CScriptArray
		@brief Registers `array` type to **AngelScript** engine
	*/
	void RegisterArray(asIScriptEngine* engine);
}

#endif