src/DataBuffer.h

Sun, 02 Feb 2014 17:06:39 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 02 Feb 2014 17:06:39 +0200
changeset 88
5def6ff8b466
child 91
427eb377d53e
permissions
-rw-r--r--

- reformatting

/*
	Copyright 2012-2014 Santeri Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. The name of the author may not be used to endorse or promote products
	   derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef BOTC_DATABUFFER_H
#define BOTC_DATABUFFER_H

#include <stdio.h>
#include <string.h>
#include "Main.h"
#include "StringTable.h"

#define MAX_MARKS 512

// ============================================================================
// data_buffer: A dynamic data buffer.
//
// Notes:
//
// - A mark is a "pointer" to a particular position in the bytecode. The actual
//   permanent position cannot be predicted in any way or form, thus these things
//   are used to "mark" a position like that for future use.
//
// - A reference is another "mark" that references a mark. When the bytecode
//   is written to file, they are changed into their marks' current
//   positions. Marks themselves are never written to files, only refs are
//
class DataBuffer
{
	PROPERTY (private, byte*,					Buffer,			NO_OPS,		STOCK_WRITE)
	PROPERTY (private, int,						AllocatedSize,	NUM_OPS,	STOCK_WRITE)
	PROPERTY (private, byte*,					Position,		NO_OPS,		STOCK_WRITE)
	PROPERTY (private, List<ByteMark*>,			Marks,			LIST_OPS,	STOCK_WRITE)
	PROPERTY (private, List<MarkReference*>,	References,		LIST_OPS,	STOCK_WRITE)

	public:
		DataBuffer (int size = 128);
		~DataBuffer();

		// ====================================================================
		// Merge another data buffer into this one.
		// Note: @other is destroyed in the process!
		void MergeAndDestroy (DataBuffer* other);

		// Clones this databuffer to a new one and returns it.
		DataBuffer* Clone();

		ByteMark*		AddMark (String name);
		MarkReference*	AddReference (ByteMark* mark, bool write_placeholder = true);
		void			CheckSpace (int bytes);
		void			DeleteMark (int marknum);
		void			AdjustMark (ByteMark* mark);
		void			OffsetMark (ByteMark* mark, int offset);
		ByteMark*		FindMarkByName (const String& target);
		void			Dump();
		void			WriteFloat (float a);
		void			WriteStringIndex (const String& a);
		void			WriteString (const String& a);
		void			WriteByte (int8_t data);
		void			WriteWord (int16_t data);
		void			WriteDWord (int32_t data);
		void			CopyBuffer (const DataBuffer* buf);

		inline int GetWrittenSize() const
		{
			return GetPosition() - GetBuffer();
		}
};

#endif // BOTC_DATABUFFER_H

mercurial