# HG changeset patch
# User Lasse Schuirmann
# Date 1392217176 -3600
# Wed Feb 12 15:59:36 2014 +0100
# Node ID eb666098a68bd0b3d39ae9efe895b8e992625c9d
# Parent 1363d909c5770799775dc33cb71507c09022dd7e
Added serialization methods and added possibility to serialize comments.
diff -r 1363d909c577 -r eb666098a68b libinterp/corefcn/comment-list.h
--- a/libinterp/corefcn/comment-list.h Tue Feb 11 00:42:43 2014 -0200
+++ b/libinterp/corefcn/comment-list.h Wed Feb 12 15:59:36 2014 +0100
@@ -71,6 +71,9 @@
comment_type type (void) const { return typ; }
~octave_comment_elt (void) { }
+
+ friend std::istream& basic_read (std::istream& is,
+ octave_comment_elt& value);
private:
diff -r 1363d909c577 -r eb666098a68b libinterp/corefcn/toplev.cc
--- a/libinterp/corefcn/toplev.cc Tue Feb 11 00:42:43 2014 -0200
+++ b/libinterp/corefcn/toplev.cc Wed Feb 12 15:59:36 2014 +0100
@@ -81,6 +81,8 @@
#include "variables.h"
#include "version.h"
+#include "basic_serialization.h"
+
#ifndef SHELL_PATH
#define SHELL_PATH "/bin/sh"
#endif
@@ -573,6 +575,7 @@
octave_parser parser (*lxr);
int retval = 0;
+ char tmpnum = 'A';
do
{
try
@@ -590,6 +593,15 @@
{
if (parser.stmt_list)
{
+ std::string tmpstr = "tmp/tmp";
+ tmpstr += tmpnum;
+ tmpnum++;
+ std::cout<accept (*current_evaluator);
octave_quit ();
diff -r 1363d909c577 -r eb666098a68b libinterp/parse-tree/basic_serialization.cc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/libinterp/parse-tree/basic_serialization.cc Wed Feb 12 15:59:36 2014 +0100
@@ -0,0 +1,3 @@
+#include "basic_serialization.h"
+
+
diff -r 1363d909c577 -r eb666098a68b libinterp/parse-tree/basic_serialization.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/libinterp/parse-tree/basic_serialization.h Wed Feb 12 15:59:36 2014 +0100
@@ -0,0 +1,275 @@
+#ifndef _BASIC_SERIALIZATION_H
+#define _BASIC_SERIALIZATION_H
+
+#include
+#include
+
+#include
+#include
+
+#include "base-list.h"
+#include "comment-list.h"
+#include "pt-stmt.h"
+#include "binary_serialization.h"
+
+/**
+ * This file provides binary serialization and deserialiation for some binary,
+ * STL and all serializable classes.
+ */
+
+#define NULL_POINTER static_cast(0)
+#define NON_NULL_POINTER static_cast(1)
+
+// -- PROTOTYPES
+std::ostream& basic_write (std::ostream& os, const std::string value);
+
+std::istream& basic_read (std::istream& is, std::string value);
+
+inline std::ostream& basic_write (std::ostream& os,
+ const octave_comment_elt::comment_type& value);
+
+inline std::istream& basic_read (std::istream& is,
+ octave_comment_elt::comment_type& value);
+
+template
+inline std::ostream& basic_write (std::ostream& os, const Z* value);
+
+template
+inline std::istream& basic_read (std::istream& is, Z* value);
+
+// For octave_base_list
+/**
+ * Serializes the object in binary form to a stream.
+ *
+ * @param os the stream to serialize to.
+ * @return the stream for further operations.
+ */
+template
+inline std::ostream&
+basic_write (std::ostream& os,
+ const octave_base_list value);
+
+/**
+ * Deserializes the object in binary form from a stream.
+ *
+ * @param is the stream.
+ * @return the stream for further operations.
+ */
+template
+inline std::istream& basic_read (std::istream& is, octave_base_list value);
+
+// for octave_comment_elt
+inline std::ostream& basic_write (std::ostream& os, const octave_comment_elt& value);
+
+inline std::istream& basic_read (std::istream& is, octave_comment_elt& value);
+
+/**
+ * basic_write for serializable classes.
+ *
+ * @param os Reference to the stream.
+ * @param value Reference to the value (const).
+ */
+inline std::ostream&
+basic_write (std::ostream& os, const serializable& value)
+{
+ return value.write (os);
+}
+
+/**
+ * basic_read for serializable classes.
+ *
+ * @param is Reference to the stream.
+ * @param value Reference to the value (will be changes)).
+ */
+inline std::istream&
+basic_read (std::istream& is, serializable& value)
+{
+ return value.read (is);
+}
+
+/**
+ * Overload for good old C strings.
+ */
+inline std::ostream&
+basic_write (std::ostream& os, const char* value)
+{
+ // for maximum performance just calculate the length once and omit the
+ // termination character
+ size_t len = strlen (value);
+ basic_write (os, len);
+ return os.write (value, len);
+}
+
+/**
+ * Overload for good old C strings.
+ *
+ * There is no ordinary specialization for this since this would be good for
+ * buffer overflows and all these evil things.
+ */
+inline std::istream&
+basic_read (std::istream& is, char* value, size_t max_length)
+{
+ size_t len;
+ basic_read (is, len);
+ if (len <= max_length)
+ {
+ is.read (value, len);
+ // this was not in the serialized version
+ value[len] = '\0';
+ }
+ else
+ {
+ std::cerr << "The file is probably corrupted." << std::endl;
+ throw -1; // TODO
+ }
+ return is;
+}
+
+/**
+ * std::string overload
+ */
+inline std::ostream&
+basic_write (std::ostream& os, const std::string value)
+{
+ return basic_write (os, value.c_str());
+}
+
+inline std::istream&
+basic_read (std::istream& is, std::string value)
+{
+
+ size_t len;
+ basic_read (is, len);
+ char * tmp = new char[len + 1];
+
+ is.read (tmp, len);
+ // this was not in the serialized version
+ tmp[len] = '\0';
+
+ value = tmp;// convert to std::string
+ return is;
+}
+
+/**
+ * Template for lists.
+ */
+template
+inline std::ostream&
+basic_write (std::ostream& os, const std::list& value)
+{
+ basic_write (os, value.size ());
+ typename std::list::const_iterator it = value.begin(), endit = value.end ();
+ for (; it != endit; ++it)
+ {
+ basic_write (os, *it);
+ }
+ return os;
+}
+
+/**
+ * Template for lists.
+ */
+template
+inline std::istream&
+basic_read (std::istream& is, std::list& value)
+{
+ typename std::list::size_type size;
+ N tmp;
+ basic_read (is, size);
+ while (size > 0)
+ {
+ basic_read (is, tmp);
+ value.push_back (tmp);
+ --size;
+ }
+ return is;
+}
+
+
+
+template
+inline std::ostream&
+basic_write (std::ostream& os, const Z* value)
+{
+ if (value != NULL)
+ {
+ basic_write (os, NON_NULL_POINTER);
+ return basic_write (os, *value);
+ }
+ else
+ {
+ basic_write (os, NULL_POINTER);
+ return os;
+ }
+}
+
+template
+inline std::istream&
+basic_read (std::istream& is, Z* value)
+{
+ uint8_t typ;
+ basic_read (is, typ);
+ if (typ == NULL_POINTER)
+ {
+ return is;
+ }
+ else
+ {
+ if (value != NULL)
+ {
+
+ return basic_read (is, *value);
+ }
+ else
+ {
+ // Null pointer exception
+ throw -1;
+ return is;
+ }
+ }
+}
+
+template
+inline std::ostream&
+basic_write (std::ostream& os,
+ const octave_base_list value)
+{
+ return basic_write (os, value.lst);
+}
+
+template
+inline std::istream&
+basic_read (std::istream& is, octave_base_list value)
+{
+ return basic_read (is, value.lst);
+}
+
+// TODO pack this to a cc file
+inline std::ostream&
+basic_write (std::ostream& os, const octave_comment_elt::comment_type& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, octave_comment_elt::comment_type& value)
+{
+ return binary_read (is, value);
+}
+
+// for octave_comment_elt
+inline std::ostream&
+basic_write (std::ostream& os, const octave_comment_elt& value)
+{
+ basic_write (os, value.type ());
+ return basic_write (os, value.text());
+}
+
+inline std::istream&
+basic_read (std::istream& is, octave_comment_elt& value)
+{
+ basic_read (is, value.typ);
+ return basic_read (is, value.txt);
+}
+
+#endif // _BASIC_SERIALIZATION_H
diff -r 1363d909c577 -r eb666098a68b libinterp/parse-tree/binary_serialization.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/libinterp/parse-tree/binary_serialization.h Wed Feb 12 15:59:36 2014 +0100
@@ -0,0 +1,171 @@
+#ifndef _BINARY_SERIALIZATION_H
+#define _BINARY_SERIALIZATION_H
+
+#include
+
+#include "serializable.h"
+
+/**
+ * Writes a binary type value to a stream.
+ *
+ * @param os Reference to the stream.
+ * @param value Reference to the value (const).
+ * @return The stream for further operations.
+ */
+template
+inline std::ostream&
+binary_write (std::ostream& os, const T& value)
+{
+ return os.write (reinterpret_cast(&value), sizeof (value));
+}
+
+/**
+ * Read a binary type value from stream.
+ *
+ * @param is Reference to the stream.
+ * @param value Reference to the value (will be changed).
+ * @return The stream for further operations.
+ */
+template
+inline std::istream&
+binary_read (std::istream& is, T& value)
+{
+ return is.read (reinterpret_cast(&value), sizeof (value));
+}
+
+
+inline std::ostream&
+basic_write (std::ostream& os, const bool& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::ostream&
+basic_write (std::ostream& os, const uint8_t& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::ostream&
+basic_write (std::ostream& os, const uint16_t& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::ostream&
+basic_write (std::ostream& os, const uint32_t& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::ostream&
+basic_write (std::ostream& os, const uint64_t& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::ostream&
+basic_write (std::ostream& os, const int8_t& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::ostream&
+basic_write (std::ostream& os, const int16_t& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::ostream&
+basic_write (std::ostream& os, const int32_t& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::ostream&
+basic_write (std::ostream& os, const int64_t& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::ostream&
+basic_write (std::ostream& os, const float& value)
+{
+ return binary_write (os, value);
+}
+
+inline std::ostream&
+basic_write (std::ostream& os, const double& value)
+{
+ return binary_write (os, value);
+}
+
+
+
+inline std::istream&
+basic_read (std::istream& is, bool& value)
+{
+ return binary_read (is, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, uint8_t& value)
+{
+ return binary_read (is, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, uint16_t& value)
+{
+ return binary_read (is, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, uint32_t& value)
+{
+ return binary_read (is, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, uint64_t& value)
+{
+ return binary_read (is, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, int8_t& value)
+{
+ return binary_read (is, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, int16_t& value)
+{
+ return binary_read (is, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, int32_t& value)
+{
+ return binary_read (is, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, int64_t& value)
+{
+ return binary_read (is, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, float& value)
+{
+ return binary_read (is, value);
+}
+
+inline std::istream&
+basic_read (std::istream& is, double& value)
+{
+ return binary_read (is, value);
+}
+
+#endif // _BINARY_SERIALIZATION_H
diff -r 1363d909c577 -r eb666098a68b libinterp/parse-tree/oct-parse.in.yy
--- a/libinterp/parse-tree/oct-parse.in.yy Tue Feb 11 00:42:43 2014 -0200
+++ b/libinterp/parse-tree/oct-parse.in.yy Wed Feb 12 15:59:36 2014 +0100
@@ -37,6 +37,7 @@
#include
#include
+#include
#include
#include