comparison oldclasslib/include/util.h @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, a System for Syntax Directed Programming
3 *
4 * Utility Class Definitions
5 * For copying, clearing, protecting and duplicating arrays.
6 *
7 * These class definitions are complete within this file.
8 * There is no corresponding CPP module.
9 *
10 * Copyright 1993 Parsifal Software. All Rights Reserved.
11 *
12 * This software is provided 'as-is', without any express or implied
13 * warranty. In no event will the authors be held liable for any damages
14 * arising from the use of this software.
15 *
16 * Permission is granted to anyone to use this software for any purpose,
17 * including commercial applications, and to alter it and redistribute it
18 * freely, subject to the following restrictions:
19 *
20 * 1. The origin of this software must not be misrepresented; you must not
21 * claim that you wrote the original software. If you use this software
22 * in a product, an acknowledgment in the product documentation would be
23 * appreciated but is not required.
24 * 2. Altered source versions must be plainly marked as such, and must not be
25 * misrepresented as being the original software.
26 * 3. This notice may not be removed or altered from any source distribution.
27 */
28
29 #ifndef UTIL_H
30 #define UTIL_H
31
32 #include <string.h>
33
34 // General purpose copy procedure. Works with pointers of any type.
35
36 template<class T>
37 void copy(T *dest, const T *src, unsigned n) {
38 memmove(dest, src, n*sizeof(T));
39 }
40
41 template<class T>
42 void copy(T *dest, const T *src) {
43 memmove(dest, src, sizeof(T));
44 }
45
46 // Clear an array to zero
47
48 template <class T>
49 T *clear(T *dest, unsigned n) {
50 memset(dest, 0, n*sizeof(T));
51 return dest;
52 }
53
54 template <class T>
55 T *clear(T *dest) {
56 memset(dest, 0, sizeof(T));
57 return dest;
58 }
59
60 // Make a duplicate of any array
61
62 template <class T>
63 T *memdup(const T *src, unsigned n) {
64 T *result = new T[n];
65 memmove(result, src, n*sizeof(T));
66 return result;
67 }
68
69 // General purpose class for saving and restoring data arrays
70
71 template <class T>
72 class protect {
73 private:
74 T *save_value;
75 unsigned n;
76 T *original_pointer;
77
78 public:
79 protect(T *p, unsigned k) {
80 save_value = new T[n = k];
81 copy(save_value, p, n);
82 original_pointer = p;
83 }
84 protect(T *p) {
85 save_value = new T[n = 1];
86 copy(save_value, p, n);
87 original_pointer = p;
88 }
89 ~protect() {
90 copy(original_pointer, save_value, n);
91 delete [] save_value;
92 }
93 };
94
95 #endif