diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/oldclasslib/include/util.h	Sat Dec 22 17:52:45 2007 -0500
@@ -0,0 +1,95 @@
+/*
+ * AnaGram, a System for Syntax Directed Programming
+ *
+ * Utility Class Definitions
+ * For copying, clearing, protecting and duplicating arrays.
+ *
+ * These class definitions are complete within this file.
+ * There is no corresponding CPP module.
+ *
+ * Copyright 1993 Parsifal Software. All Rights Reserved.
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
+#ifndef UTIL_H
+#define UTIL_H
+
+#include <string.h>
+
+// General purpose copy procedure. Works with pointers of any type.
+
+template<class T>
+void copy(T *dest, const T *src, unsigned n) {
+  memmove(dest, src, n*sizeof(T));
+}
+
+template<class T>
+void copy(T *dest, const T *src) {
+  memmove(dest, src, sizeof(T));
+}
+
+// Clear an array to zero
+
+template <class T>
+T *clear(T *dest, unsigned n) {
+	memset(dest, 0, n*sizeof(T));
+	return dest;
+}
+
+template <class T>
+T *clear(T *dest) {
+  memset(dest, 0, sizeof(T));
+  return dest;
+}
+
+// Make a duplicate of any array
+
+template <class T>
+T *memdup(const T *src, unsigned n) {
+  T *result = new T[n];
+  memmove(result, src, n*sizeof(T));
+  return result;
+}
+
+// General purpose class for saving and restoring data arrays
+
+template <class T>
+class protect {
+private:
+  T *save_value;
+  unsigned n;
+  T *original_pointer;
+
+public:
+  protect(T *p, unsigned k) {
+    save_value = new T[n = k];
+    copy(save_value, p, n);
+    original_pointer = p;
+  }
+  protect(T *p) {
+    save_value = new T[n = 1];
+    copy(save_value, p, n);
+    original_pointer = p;
+  }
+  ~protect() {
+    copy(original_pointer, save_value, n);
+    delete [] save_value;
+  }
+};
+
+#endif