src/DataBuffer.h

changeset 88
5def6ff8b466
child 91
427eb377d53e
equal deleted inserted replaced
87:8f65914e7046 88:5def6ff8b466
1 /*
2 Copyright 2012-2014 Santeri Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef BOTC_DATABUFFER_H
30 #define BOTC_DATABUFFER_H
31
32 #include <stdio.h>
33 #include <string.h>
34 #include "Main.h"
35 #include "StringTable.h"
36
37 #define MAX_MARKS 512
38
39 // ============================================================================
40 // data_buffer: A dynamic data buffer.
41 //
42 // Notes:
43 //
44 // - A mark is a "pointer" to a particular position in the bytecode. The actual
45 // permanent position cannot be predicted in any way or form, thus these things
46 // are used to "mark" a position like that for future use.
47 //
48 // - A reference is another "mark" that references a mark. When the bytecode
49 // is written to file, they are changed into their marks' current
50 // positions. Marks themselves are never written to files, only refs are
51 //
52 class DataBuffer
53 {
54 PROPERTY (private, byte*, Buffer, NO_OPS, STOCK_WRITE)
55 PROPERTY (private, int, AllocatedSize, NUM_OPS, STOCK_WRITE)
56 PROPERTY (private, byte*, Position, NO_OPS, STOCK_WRITE)
57 PROPERTY (private, List<ByteMark*>, Marks, LIST_OPS, STOCK_WRITE)
58 PROPERTY (private, List<MarkReference*>, References, LIST_OPS, STOCK_WRITE)
59
60 public:
61 DataBuffer (int size = 128);
62 ~DataBuffer();
63
64 // ====================================================================
65 // Merge another data buffer into this one.
66 // Note: @other is destroyed in the process!
67 void MergeAndDestroy (DataBuffer* other);
68
69 // Clones this databuffer to a new one and returns it.
70 DataBuffer* Clone();
71
72 ByteMark* AddMark (String name);
73 MarkReference* AddReference (ByteMark* mark, bool write_placeholder = true);
74 void CheckSpace (int bytes);
75 void DeleteMark (int marknum);
76 void AdjustMark (ByteMark* mark);
77 void OffsetMark (ByteMark* mark, int offset);
78 ByteMark* FindMarkByName (const String& target);
79 void Dump();
80 void WriteFloat (float a);
81 void WriteStringIndex (const String& a);
82 void WriteString (const String& a);
83 void WriteByte (int8_t data);
84 void WriteWord (int16_t data);
85 void WriteDWord (int32_t data);
86 void CopyBuffer (const DataBuffer* buf);
87
88 inline int GetWrittenSize() const
89 {
90 return GetPosition() - GetBuffer();
91 }
92 };
93
94 #endif // BOTC_DATABUFFER_H

mercurial