wavelength 1.0.0
Loading...
Searching...
No Matches
dataarray.h
Go to the documentation of this file.
1#pragma once
2
3#include <stddef.h>
4#include <vector>
5
6#include "math/color.h"
7#include "math/vector2.h"
8#include "math/vector3.h"
9#include "os/binstream.h"
10#include "os/mem.h"
11#include "os/prnstream.h"
12#include "os/string.h"
13
20class DataArray {
21public:
29
31 union Node {
32 int mInt;
33 const char *mSymbol;
34 float mFloat;
36 };
37
45 explicit DataArray(int nSize);
46
55 DataArray(BinStream &stream, const std::vector<const char *> &files);
56
64
70 static void *operator new(size_t) {
71 return PoolAlloc(sizeof(DataArray), sizeof(DataArray), "DataArray", 0);
72 }
73
79 static void operator delete(void *pBlock) {
80 PoolFree(sizeof(DataArray), pBlock);
81 }
82
92 static DataArray *Concat(const std::vector<DataArray *> &arrays, bool bRelease);
93
102 void Merge(const DataArray *pOther);
103
112 void SetFileLine(const char *pszFile, int nLine);
113
122 void SortByKey();
123
133 void Resize(int nSize);
134
143 Node Value(int nIndex) const;
144
151 void AddRef();
152
158 int Size() const {
159 return mSize;
160 }
161
170 int Type(int nIndex) const;
171
184 bool FindVector(const char *pszName, Vector3 *pValue, bool bFail) const;
185
196 bool FindVector(const char *pszName, Vector2 *pValue, bool bFail) const;
197
204 void Release();
205
215 static DataArray *Read(const char *pszPath, BinStream *pStream);
216
228 static DataArray *ReadText(const char *pszPath, BinStream *pStream);
229
239 DataArray *FindArray(const char *pszTag, bool bFail) const;
240
251 DataArray *FindArray(int nTag) const;
252
263 bool FindString(const char *pszName, String *pValue, bool bFail) const;
264
277 bool FindColor(const char *pszName, Color *pValue, bool bFail) const;
278
289 bool FindSymbol(const char *pszName, const char **ppszValue, bool bFail) const;
290
301 bool FindInt(const char *pszName, int *pnValue, bool bFail) const;
302
313 bool FindFloat(const char *pszName, float *pfValue, bool bFail) const;
314
326 bool FindBool(const char *pszName, int *pnValue, bool bFail) const;
327
336 const char *Sym(int nIndex) const;
337
346 int Int(int nIndex) const;
347
356 float Float(int nIndex) const;
357
366 DataArray *Array(int nIndex) const;
367
381 void SetNode(int nIndex, Node value, int nType);
382
390 void Print(PrnStream &stream) const;
391
404 static void MakeCompiledPath(char *pszOut, const char *pszPath, bool bCompiled);
405
414 static DataArray *New(int nSize);
415
424 static void DefineMacro(const char *pszName, DataArray *pValue);
425
434 static DataArray *FindMacro(const char *pszName);
435
437 const char *GetFile() const {
438 return mFile;
439 }
440
442 short GetLine() const {
443 return mLine;
444 }
445
446private:
451 Node *mNodes;
452 const char *mFile;
453 short mSize;
454 unsigned short mRefs;
455 short mLine;
456 short mSorted;
457
466 void PrintNode(PrnStream &stream, int nIndex) const;
467
475 bool HasArray() const;
476
485 void SetType(int nIndex, int nType);
486
496 static int CompareKeys(const void *pFirst, const void *pSecond);
497};
498
508
518
Stream of raw bytes, the base of the memory, file, and network streams.
Definition binstream.h:22
const char * Sym(int nIndex) const
Report a node as a symbol.
static DataArray * ReadText(const char *pszPath, BinStream *pStream)
Parse script text into an array, never the compiled form Read() prefers on the disc.
bool FindFloat(const char *pszName, float *pfValue, bool bFail) const
Find the floating-point number that follows a tag.
static void MakeCompiledPath(char *pszOut, const char *pszPath, bool bCompiled)
Build the path of the compiled copy of a data file.
int Int(int nIndex) const
Report a node as an integer.
int Size() const
Report the number of nodes.
Definition dataarray.h:158
NodeType
Kinds of node Type() reports, two bits each.
Definition dataarray.h:23
@ kNodeArray
Definition dataarray.h:27
@ kNodeInt
Definition dataarray.h:24
@ kNodeSymbol
Definition dataarray.h:25
@ kNodeFloat
Definition dataarray.h:26
void SetFileLine(const char *pszFile, int nLine)
Record the file and the line the array was read from.
DataArray(BinStream &stream, const std::vector< const char * > &files)
Read an array the compiler wrote, with one reference.
const char * GetFile() const
Definition dataarray.h:437
bool FindSymbol(const char *pszName, const char **ppszValue, bool bFail) const
Find the symbol that follows a tag.
void SortByKey()
Sort the child arrays by the value of their first nodes, and mark the array as sorted.
DataNode Node(int i)
Read a node of any type.
int Type(int nIndex) const
Report the kind of a node.
bool FindBool(const char *pszName, int *pnValue, bool bFail) const
Find the truth value that follows a tag.
float Float(int nIndex) const
Report a node as a float.
DataArray * FindArray(const char *pszTag, bool bFail) const
Find the child array whose first node is a tag.
static DataArray * FindMacro(const char *pszName)
Find the value of a script macro.
bool FindInt(const char *pszName, int *pnValue, bool bFail) const
Find the integer that follows a tag.
void Print(PrnStream &stream) const
Write the array as script text.
static DataArray * Concat(const std::vector< DataArray * > &arrays, bool bRelease)
Join the nodes of several arrays into a new array.
bool FindVector(const char *pszName, Vector3 *pValue, bool bFail) const
Find the three numbers that follow a tag.
bool FindVector(const char *pszName, Vector2 *pValue, bool bFail) const
Find the two numbers that follow a tag.
void Release()
Drop one reference and destroy the array when none remains.
DataArray * FindArray(int nTag) const
Find the child array whose first node is an integer tag.
void AddRef()
Take one more reference.
Node Value(int nIndex) const
Report a node's value without regard to its kind.
void Merge(const DataArray *pOther)
Add the child arrays of another array whose tags this array lacks, and merge the children whose tags ...
bool FindColor(const char *pszName, Color *pValue, bool bFail) const
Find the colour that follows a tag.
bool FindString(const char *pszName, String *pValue, bool bFail) const
Find the symbol that follows a tag and copy it into a string.
void Resize(int nSize)
Change the number of nodes, releasing the child arrays of the nodes removed.
static DataArray * New(int nSize)
Create an array of nodes with one reference.
static void DefineMacro(const char *pszName, DataArray *pValue)
Define a script macro, taking a reference on its value.
DataArray * Array(int nIndex) const
Report a node as an array.
static DataArray * Read(const char *pszPath, BinStream *pStream)
Parse script text into an array.
short GetLine() const
Definition dataarray.h:442
~DataArray()
Release the child arrays and the node storage.
void SetNode(int nIndex, Node value, int nType)
Replace a node with a value of a kind.
DataArray(int nSize)
Construct an array of cleared integer nodes with one reference.
Text output stream.
Definition prnstream.h:16
Growable text buffer that is also a PrnStream.
Definition string.h:19
void DataTerminate()
Release every array the data layer still retains.
void DataSetCompiled()
Make DataArray::Read() use the compiled copy of every data file.
void DataReserveSymbols()
Reserve the symbol table before the first data file is read.
void * PoolAlloc(int nSize, int nBlockSize, const char *pszTag, int nUnused)
Allocate a block from the small-block pool and bill it to a tag.
void PoolFree(int nSize, void *pBlock)
Return a block to the small-block pool.
Linear colour with four float components.
Definition color.h:12
Two-component vector, used for texture coordinates.
Definition vector2.h:12
Three-component vector, padded to a PlayStation 2 quadword.
Definition vector3.h:14
A node's value, read as the member its NodeType selects.
Definition dataarray.h:31
int mInt
Definition dataarray.h:32
DataArray * mArray
Definition dataarray.h:35
float mFloat
Definition dataarray.h:34
const char * mSymbol
Definition dataarray.h:33