# 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 #include @@ -3901,7 +3902,7 @@ parser.lexer.fcn_file_full_name = full_file; int status = parser.run (); - + fcn_ptr = parser.primary_fcn_ptr; if (status == 0) diff -r 1363d909c577 -r eb666098a68b libinterp/parse-tree/pt-stmt.cc --- a/libinterp/parse-tree/pt-stmt.cc Tue Feb 11 00:42:43 2014 -0200 +++ b/libinterp/parse-tree/pt-stmt.cc Wed Feb 12 15:59:36 2014 +0100 @@ -178,6 +178,23 @@ tw.visit_statement (*this); } +std::ostream& tree_statement::write (std::ostream& os) const +{ + tree::write (os); + basic_write (os, cmd); + basic_write (os, expr); + return basic_write (os, comm); +} + +std::istream& tree_statement::read (std::istream& is) +{ + tree::read (is); + basic_read (is, cmd); + basic_read (is, expr); + return basic_read (is, comm); +} + + int tree_statement_list::set_breakpoint (int line) { @@ -290,3 +307,21 @@ { tw.visit_statement_list (*this); } + +std::ostream& tree_statement_list::write (std::ostream& os) const +{ + basic_write (os, + (dynamic_cast* const>(this))); + basic_write (os, function_body); + basic_write (os, anon_function_body); + return basic_write (os, script_body); +} + +std::istream& tree_statement_list::read (std::istream& is) +{ + basic_read (is, + (dynamic_cast*>(this))); + basic_read (is, function_body); + basic_read (is, anon_function_body); + return basic_read (is, script_body); +} diff -r 1363d909c577 -r eb666098a68b libinterp/parse-tree/pt-stmt.h --- a/libinterp/parse-tree/pt-stmt.h Tue Feb 11 00:42:43 2014 -0200 +++ b/libinterp/parse-tree/pt-stmt.h Wed Feb 12 15:59:36 2014 +0100 @@ -33,8 +33,10 @@ #include #include "base-list.h" +#include "basic_serialization.h" #include "comment-list.h" #include "debug.h" +#include "serializable.h" #include "symtab.h" #include "pt.h" @@ -102,6 +104,22 @@ symbol_table::context_id context) const; void accept (tree_walker& tw); + + /** + * Serializes the object in binary form to a stream. + * + * @param os the stream to serialize to. + * @return the stream for further operations. + */ + virtual std::ostream& write (std::ostream& os) const; + + /** + * Deserializes the object in binary form from a stream. + * + * @param is the stream. + * @return the stream for further operations. + */ + virtual std::istream& read (std::istream& is); private: @@ -174,6 +192,22 @@ symbol_table::context_id context) const; void accept (tree_walker& tw); + + /** + * Serializes the object in binary form to a stream. + * + * @param os the stream to serialize to. + * @return the stream for further operations. + */ + virtual std::ostream& write (std::ostream& os) const; + + /** + * Deserializes the object in binary form from a stream. + * + * @param is the stream. + * @return the stream for further operations. + */ + virtual std::istream& read (std::istream& is); private: diff -r 1363d909c577 -r eb666098a68b libinterp/parse-tree/pt.cc --- a/libinterp/parse-tree/pt.cc Tue Feb 11 00:42:43 2014 -0200 +++ b/libinterp/parse-tree/pt.cc Wed Feb 12 15:59:36 2014 +0100 @@ -32,6 +32,8 @@ #include "pt.h" #include "pt-pr-code.h" +#include "basic_serialization.h" + // Hide the details of the string buffer so that we are less likely to // create a memory leak. @@ -48,3 +50,19 @@ return retval; } + +std::ostream& tree::write (std::ostream& os) const +{ + basic_write (os, line_num); + basic_write (os, column_num); + basic_write (os, bp); + return os; +} + +std::istream& tree::read (std::istream& is) +{ + basic_read (is, line_num); + basic_read (is, column_num); + basic_read (is, bp); + return is; +} diff -r 1363d909c577 -r eb666098a68b libinterp/parse-tree/pt.h --- a/libinterp/parse-tree/pt.h Tue Feb 11 00:42:43 2014 -0200 +++ b/libinterp/parse-tree/pt.h Wed Feb 12 15:59:36 2014 +0100 @@ -27,13 +27,15 @@ #include +#include "serializable.h" + class octave_function; class tree_walker; // Base class for the parse tree. class -tree +tree : public serializable { public: @@ -65,6 +67,22 @@ std::string str_print_code (void); virtual void accept (tree_walker& tw) = 0; + + /** + * Serializes the object in binary form to a stream. + * + * @param os the stream to serialize to. + * @return the stream for further operations. + */ + virtual std::ostream& write (std::ostream& os) const; + + /** + * Deserializes the object in binary form from a stream. + * + * @param is the stream. + * @return the stream for further operations. + */ + virtual std::istream& read (std::istream& is); private: diff -r 1363d909c577 -r eb666098a68b libinterp/parse-tree/serializable.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libinterp/parse-tree/serializable.h Wed Feb 12 15:59:36 2014 +0100 @@ -0,0 +1,29 @@ +#ifndef _SERIALIZABLE_H +#define _SERIALIZABLE_H + +#include + +class +serializable +{ +public: + virtual ~serializable () { }; + + /** + * Serializes the object to a binary stream. + * + * @param os The stream to write into. + * @return The stream as usual. + */ + virtual std::ostream& write (std::ostream& os) const = 0; + + /** + * Loads a serialized object from binary stream. + * + * @param is The stream to write into. + * @return The stream as usual. + */ + virtual std::istream& read (std::istream& is) = 0; +}; + +#endif // _SERIALIZABLE_H diff -r 1363d909c577 -r eb666098a68b liboctave/util/base-list.h --- a/liboctave/util/base-list.h Tue Feb 11 00:42:43 2014 -0200 +++ b/liboctave/util/base-list.h Wed Feb 12 15:59:36 2014 +0100 @@ -23,10 +23,14 @@ #if !defined (octave_base_list_h) #define octave_base_list_h 1 +class tree_statement; + #include #include +#include + template class octave_base_list @@ -120,6 +124,14 @@ } ~octave_base_list (void) { } + + template + friend std::ostream& basic_write (std::ostream& os, + const octave_base_list value); + + template + friend std::istream& basic_read (std::istream& is, + octave_base_list value); private: