-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathTokenMessage.h
More file actions
204 lines (166 loc) · 5.62 KB
/
Copy pathTokenMessage.h
File metadata and controls
204 lines (166 loc) · 5.62 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//==============================================================================
//
// TokenMessage.h
//
// Diplomacy AI Client - Part of the DAIDE project (www.daide.org.uk).
//
// (C) David Norman 2002 david@ellought.demon.co.uk
// (C) Greg Utas 2019-2025 greg@pentennea.com
//
// This software may be reused for non-commercial purposes without charge,
// and without notifying the authors. Use of any part of this software for
// commercial purposes without permission from the authors is prohibited.
//
#ifndef TOKENMESSAGE_H_INCLUDED
#define TOKENMESSAGE_H_INCLUDED
#include <cstddef>
#include <memory>
#include <string>
#include "DipTypes.h"
namespace Diplomacy
{
class Token;
}
//------------------------------------------------------------------------------
namespace Diplomacy
{
// Provides a wrapper for a sequence of tokens enclosed in parentheses.
//
class TokenMessage
{
public:
// Constructs an empty message.
//
TokenMessage();
// Constructs a message containing a single token.
//
TokenMessage(token_t raw);
TokenMessage(const Token& token);
// Constructs a message from a sequence of tokens (STREAM).
// STREAM must end with TOKEN_END_OF_MESSAGE.
//
explicit TokenMessage(const Token* stream);
// Construct a message from a stream of LENGTH tokens.
//
TokenMessage(const Token* stream, size_t length);
// Copy constructor.
//
TokenMessage(const TokenMessage& that);
// Not subclassed. Invokes clear().
//
~TokenMessage();
// Copy/move operators.
//
TokenMessage& operator=(const TokenMessage& that);
TokenMessage& operator=(TokenMessage&& that);
// Copies the message's tokens into TOKENS, which can hold up to MAX
// tokens. Returns false if the message is empty or contains more
// tokens than TOKENS can hold. Returns true on success, in which
// case the last entry in TOKENS will be TOKEN_END_OF_MESSAGE.
//
bool get_tokens(Token tokens[], size_t max) const;
// Returns true if the message is empty.
//
bool empty() const { return (length_ == 0); }
// Returns the length of the message.
//
size_t size() const { return length_; }
// Returns true if the message is a single token.
//
bool is_single_token() const { return (length_ == 1); }
// Returns the first token.
//
Token front() const;
// Returns the token at [INDEX].
//
Token operator[](size_t index) const;
Token at(size_t index) const;
// Returns true if the message contains any parameters that are enclosed
// in parentheses.
//
bool has_nested_parms() const { return (length_ != parm_count_); }
// Returns the number of parameters. A parameter is a single token or
// a stream of tokens enclosed by parentheses. A parameter can itself
// contain nested parameters.
//
size_t parm_count() const { return parm_count_; }
// Returns the Nth parameter as a message.
//
TokenMessage get_parm(size_t n) const;
// Returns the index into the stream of tokens where the Nth parameter
// starts, omitting its leading parenthesis.
//
size_t parm_start(size_t n) const;
// Returns true if the Nth parameter is a single token.
//
bool parm_is_single_token(size_t n) const;
// Copies the stream of TOKENS into the message. STREAM must end with
// TOKEN_END_OF_MESSAGE. Returns the offset of any error, or NO_ERROR
// on success.
//
size_t set_from(const Token* stream);
// Copies the stream of TOKENS into the message. STREAM is truncated
// at LENGTH. Returns the offset of any error, or NO_ERROR on success.
//
size_t set_from(const Token* stream, size_t length);
// Returns the message as a readable string.
//
std::string to_str() const;
// Sets the message by interpreting TEXT, which is of the form returned
// to_str(). Returns the offset of any error, or NO_ERROR on success.
//
size_t set_from(const std::string& text);
// Sets the message to the string of ASCII tokens in TEXT.
//
void set_as_ascii(const std::string& text);
// Creates a copy of the message, but enclosed in parentheses.
//
TokenMessage enclose() const;
// Modifies the message by enclosing it in parentheses.
//
void enclose_this();
// Clears the message and frees its resources.
//
void clear();
// Logs the message. EXPL will be the log's title.
//
void log(const std::string& expl) const;
// The + operators perform straight concatenation (i.e. append).
// The & operators enclose THAT in parentheses before appending.
//
TokenMessage operator+(const Token& token);
TokenMessage operator+(const TokenMessage& that);
TokenMessage operator&(const Token& token);
TokenMessage operator&(const TokenMessage& that);
// Compares this message to THAT.
//
bool operator==(const TokenMessage& that) const;
bool operator!=(const TokenMessage& that) const;
bool operator<(const TokenMessage& that) const;
private:
// Finds the message's parameters if they have not already been
// found.
//
void find_parms() const;
// For storing the message's contents.
//
typedef std::unique_ptr<Token[]> TokensPtr;
// For storing the starting location of each of the message's
// parameters.
//
typedef std::unique_ptr<size_t[]> ParmBeginsPtr;
// The number of tokens in the message.
//
size_t length_;
// The stream of tokens.
//
TokensPtr message_;
// The number of parameters in the message.
//
size_t parm_count_;
// The index into message_ where each parameter begins.
//
mutable ParmBeginsPtr parm_begins_;
};
}
#endif