From 0c0391e59ffd0d5dab9e50f8f7115111c98bdafa Mon Sep 17 00:00:00 2001 From: binrapt Date: Sat, 17 Oct 2009 07:20:53 +0200 Subject: [PATCH] Oh, boy. I erroneously thought that operator= used the copy constructor by default but this only happens when you are making a declaration on the same line - which is not the case here. Introducing a peculiar construction pattern base class for all the union/pointer structs. --- fridh/construction.hpp | 18 ++++++++++++++++++ fridh/function.hpp | 10 ++++++++++ interpreter/function.cpp | 27 ++++++++++++++++++++++----- shared/construction.cpp | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 fridh/construction.hpp create mode 100644 shared/construction.cpp diff --git a/fridh/construction.hpp b/fridh/construction.hpp new file mode 100644 index 0000000..f8dbe3f --- /dev/null +++ b/fridh/construction.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace fridh +{ + struct construction_pattern + { + virtual void copy(construction_pattern const & other); + virtual void destroy(); + + construction_pattern(); + construction_pattern(construction_pattern const & other); + ~construction_pattern(); + + construction_pattern & operator=(construction_pattern const & other); + }; +} diff --git a/fridh/function.hpp b/fridh/function.hpp index 241fab2..e39cba4 100644 --- a/fridh/function.hpp +++ b/fridh/function.hpp @@ -5,6 +5,8 @@ #include +#include + namespace fridh { namespace executable_unit_type @@ -137,6 +139,11 @@ namespace fridh parse_tree_node(parse_tree_node const & other); ~parse_tree_node(); + void copy(parse_tree_node const & other); + void destroy(); + + parse_tree_node & operator=(parse_tree_node const & other); + parse_tree_node(parse_tree_node_type::type type); parse_tree_node(variable * variable_pointer); parse_tree_node(unary_operator_type::type unary_operator); @@ -235,6 +242,9 @@ namespace fridh executable_unit(); executable_unit(executable_unit const & other); ~executable_unit(); + + private: + executable_unit & operator=(executable_unit const & other); }; struct function diff --git a/interpreter/function.cpp b/interpreter/function.cpp index 3abdac4..096b103 100644 --- a/interpreter/function.cpp +++ b/interpreter/function.cpp @@ -13,7 +13,17 @@ namespace fridh parse_tree_node::parse_tree_node(parse_tree_node const & other) { - std::cout << "parse_tree_node " << (void *)this << " from " << (void *)&other << std::endl; + copy(other); + } + + parse_tree_node::~parse_tree_node() + { + destroy(); + } + + void parse_tree_node::copy(parse_tree_node const & other) + { + std::cout << "parse_tree_node copy " << (void *)this << " from " << (void *)&other << std::endl; //std::cout << (int)other.type << std::endl; @@ -39,13 +49,11 @@ namespace fridh #undef COPY_MEMBER //std::cout << "Result: " << to_string() << std::endl; - } - parse_tree_node::~parse_tree_node() + void parse_tree_node::destroy() { - - std::cout << "~parse_tree_node " << (void *)this << std::endl; + std::cout << "parse_tree_node destroy " << (void *)this << std::endl; #define DELETE_MEMBER(type, member) \ case parse_tree_node_type::type: \ @@ -62,10 +70,19 @@ namespace fridh DELETE_MEMBER(array, array_pointer) } + type = parse_tree_node_type::uninitialised; + #undef DELETE_MEMBER } + parse_tree_node & parse_tree_node::operator=(parse_tree_node const & other) + { + destroy(); + copy(other); + return *this; + } + parse_tree_node::parse_tree_node(parse_tree_node_type::type type): type(type) { diff --git a/shared/construction.cpp b/shared/construction.cpp new file mode 100644 index 0000000..cf2966e --- /dev/null +++ b/shared/construction.cpp @@ -0,0 +1,35 @@ +#include + +namespace fridh +{ + void construction_pattern::copy(construction_pattern const & other) + { + throw ail::exception("Construction pattern copy requested"); + } + + void construction_pattern::destroy() + { + throw ail::exception("Construction pattern destruction requested"); + } + + construction_pattern::construction_pattern() + { + } + + construction_pattern::construction_pattern(construction_pattern const & other) + { + copy(other); + } + + construction_pattern::~construction_pattern() + { + destroy(); + } + + construction_pattern & construction_pattern::operator=(construction_pattern const & other) + { + destroy(); + copy(other); + return *this; + } +} -- 2.11.4.GIT