File: insert_operation.hpp

package info (click to toggle)
obby 0.4.6-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,800 kB
  • ctags: 1,512
  • sloc: cpp: 11,181; sh: 9,627; makefile: 235; perl: 29; sed: 16
file content (325 lines) | stat: -rw-r--r-- 9,647 bytes parent folder | download | duplicates (4)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* libobby - Network text editing library
 * Copyright (C) 2005, 2006 0x539 dev group
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef _OBBY_INSERT_OPERATION_HPP_
#define _OBBY_INSERT_OPERATION_HPP_

#include "text.hpp"
#include "operation.hpp"

namespace obby
{

/** insert_operation inserts an amount of text at a specified position in
 * the document.
 *
 * This is the base class for both reversed and non-reversed insert
 * operations that implements common functionality.
 */
template<typename Document, typename String>
class basic_insert_operation: public operation<Document>
{
public:
	typedef operation<Document> operation_type;
	typedef basic_insert_operation<Document, String>
		base_insert_operation_type;

	typedef typename operation_type::document_type document_type;
	typedef String string_type;

	basic_insert_operation(position pos,
	                       const String& text);

	/** Creates a copy of this operation.
	 */
	virtual operation_type* clone() const;

	/** Creates the reverse operation of this one.
	 * @param doc Document to receive additional information from.
	 */
	virtual operation_type* reverse(const document_type& doc) const;

	/** Transforms <em>base_op</em> against this operation.
	 */
	virtual operation_type* transform(const operation_type& base_op) const;

	/** Includes the effect of the given insertion into this operation.
	 */
	virtual operation_type* transform_insert(position pos,
	                                         const std::string& text) const;

	/** Includes the effect of the given deletion into this operation.
	 */
	virtual operation_type* transform_delete(position pos,
	                                         position len) const;
protected:
	position m_pos;
	string_type m_text;

	// Has to be implemented by subclasses since basic_insert_operation
	// is abstract
	virtual base_insert_operation_type*
	construct(position pos,
	          const string_type& text) const = 0;
};

/** Operation that insert text at a position in the document.
 */
template<typename Document>
class insert_operation: public basic_insert_operation<Document, std::string>
{
public:
	typedef operation<Document> operation_type;
	typedef typename basic_insert_operation<Document, std::string>::
		base_insert_operation_type base_insert_operation_type;

	typedef typename operation_type::document_type document_type;
	typedef typename basic_insert_operation<Document, std::string>::
		string_type string_type;

	insert_operation(position pos,
	                 const string_type& text);

	insert_operation(const net6::packet& pack,
	                 unsigned int& index);

	virtual void apply(document_type& doc,
	                   const user* author) const;

	virtual void append_packet(net6::packet& pack) const;
protected:
	virtual base_insert_operation_type*
	construct(position pos,
	          const string_type& text) const;
};

/** @brief Operation that is the result of a reverse() call to delete_operation.
 *
 * A delete operation may have deleted text of multiple users, so this class
 * also stores the authors of the text to insert.
 *
 * TODO: Rename in reversed_insert_operation
 */
template<typename Document>
class reversible_insert_operation: public basic_insert_operation<Document, text>
{
public:
	typedef operation<Document> operation_type;
	typedef typename basic_insert_operation<Document, text>::
		base_insert_operation_type base_insert_operation_type;

	typedef typename operation_type::document_type document_type;
	typedef typename basic_insert_operation<Document, text>::string_type
		string_type;

	reversible_insert_operation(position pos,
	                            const string_type& text);

	reversible_insert_operation(const net6::packet& pack,
	                            unsigned int& index,
	                            const user_table& user_table);

	virtual void apply(document_type& doc,
	                   const user* author) const;

	virtual void append_packet(net6::packet& pack) const;
protected:
	virtual base_insert_operation_type*
	construct(position pos,
	          const string_type& text) const;
};

template<typename Document, typename String>
basic_insert_operation<Document, String>::
	basic_insert_operation(position pos,
                               const string_type& text):
	operation<Document>(), m_pos(pos), m_text(text)
{
}

template<typename Document, typename String>
typename basic_insert_operation<Document, String>::operation_type*
basic_insert_operation<Document, String>::clone() const
{
	return construct(m_pos, m_text);
}

template<typename Document, typename String>
typename basic_insert_operation<Document, String>::operation_type*
basic_insert_operation<Document, String>::
	reverse(const document_type& doc) const
{
	return new delete_operation<Document>(m_pos, m_text.length() );
}

template<typename Document, typename String>
typename basic_insert_operation<Document, String>::operation_type*
basic_insert_operation<Document, String>::
	transform(const operation_type& base_op) const
{
	return base_op.transform_insert(m_pos, m_text);
}

template<typename Document, typename String>
typename basic_insert_operation<Document, String>::operation_type*
basic_insert_operation<Document, String>::
	transform_insert(position pos,
                         const std::string& text) const
{
	if(m_pos < pos)
	{
		// Case 1
		return clone();
	}
	else if(m_pos == pos)
	{
		// Special case
		if(m_text < text)
		{
			return clone();
		}
		else
		{
			return construct(m_pos + text.length(), m_text);
		}
	}
	else
	{
		// Case 2
		return construct(
			m_pos + text.length(),
			m_text
		);
	}
}

template<typename Document, typename String>
typename basic_insert_operation<Document, String>::operation_type*
basic_insert_operation<Document, String>::transform_delete(position pos,
                                                           position len) const
{
	if(m_pos <= pos)
	{
		// Case 3
		return clone();
	}
	else if(m_pos > pos + len)
	{
		// Case 4
		return construct(m_pos - len, m_text);
	}
	else
	{
		// Case 5
		return construct(pos, m_text);
	}
}

template<typename Document>
insert_operation<Document>::insert_operation(position pos,
                                             const string_type& text):
	basic_insert_operation<Document, std::string>(pos, text)
{
}

template<typename Document>
insert_operation<Document>::insert_operation(const net6::packet& pack,
                                             unsigned int& index):
	basic_insert_operation<Document, std::string>(
		pack.get_param(index + 0).net6::parameter::as<int>(),
		pack.get_param(index + 1).net6::parameter::as<std::string>()
	)
{
	index += 2;
}

template<typename Document>
void insert_operation<Document>::apply(document_type& doc,
                                       const user* author) const
{
	doc.insert(
		basic_insert_operation<Document, std::string>::m_pos,
		basic_insert_operation<Document, std::string>::m_text,
		author
	);
}

template<typename Document>
void insert_operation<Document>::append_packet(net6::packet& pack) const
{
	pack << "ins" << basic_insert_operation<Document, std::string>::m_pos
	     << basic_insert_operation<Document, std::string>::m_text;
}

template<typename Document>
typename insert_operation<Document>::base_insert_operation_type*
insert_operation<Document>::construct(position pos,
                                      const string_type& text) const
{
	return new insert_operation<Document>(pos, text);
}

template<typename Document>
reversible_insert_operation<Document>::
	reversible_insert_operation(position pos,
	                            const string_type& text):
	basic_insert_operation<Document, obby::text>(pos, text)
{
}

template<typename Document>
reversible_insert_operation<Document>::
	reversible_insert_operation(const net6::packet& pack,
	                            unsigned int& index,
	                            const user_table& user_table):
	basic_insert_operation<Document, text>(
		pack.get_param(index ++).net6::parameter::as<int>(),
		text(pack, index, user_table)
	)
{
}

template<typename Document>
void reversible_insert_operation<Document>::apply(document_type& doc,
                                                  const user* author) const
{
	doc.insert(
		basic_insert_operation<Document, text>::m_pos,
		basic_insert_operation<Document, text>::m_text
	);
}

template<typename Document>
void reversible_insert_operation<Document>::
	append_packet(net6::packet& pack) const
{
	pack << "revins" << basic_insert_operation<Document, text>::m_pos;
	basic_insert_operation<Document, text>::m_text.append_packet(pack);
}

template<typename Document>
typename reversible_insert_operation<Document>::base_insert_operation_type*
reversible_insert_operation<Document>::construct(position pos,
                                                 const string_type& text) const
{
	return new reversible_insert_operation<Document>(pos, text);
}

} // namespace obby

#endif // _OBBY_INSERT_OPERATION_HPP_