From 46b7119fab32b05923dbf9cda7cd6ce62f572846 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 9 Jan 2017 14:01:51 -0500 Subject: Initial commit --- generator/field.cpp | 193 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 generator/field.cpp (limited to 'generator/field.cpp') diff --git a/generator/field.cpp b/generator/field.cpp new file mode 100644 index 0000000..7e7453d --- /dev/null +++ b/generator/field.cpp @@ -0,0 +1,193 @@ +#include "field.h" +#include +#include + +namespace cadence { + namespace generator { + + field::field(const field& other) + { + type_ = other.type_; + name_ = other.name_; + + switch (type_) + { + case type::integer: + { + integer_ = other.integer_; + + break; + } + + case type::string: + { + new(&string_) std::string(other.string_); + + break; + } + + case type::invalid: + { + break; + } + } + } + + field::field(field&& other) : field() + { + swap(*this, other); + } + + field& field::operator=(field other) + { + swap(*this, other); + + return *this; + } + + void swap(field& first, field& second) + { + using type = field::type; + + type tempType = first.type_; + std::string tempName = std::move(first.name_); + int tempInteger; + std::string tempString; + + switch (first.type_) + { + case type::integer: + { + tempInteger = first.integer_; + + break; + } + + case type::string: + { + tempString = std::move(tempString); + + break; + } + + case type::invalid: + { + break; + } + } + + first.~field(); + + first.type_ = second.type_; + first.name_ = std::move(second.name_); + + switch (second.type_) + { + case type::integer: + { + first.integer_ = second.integer_; + + break; + } + + case type::string: + { + new(&first.string_) std::string(std::move(second.string_)); + + break; + } + + case type::invalid: + { + break; + } + } + + second.~field(); + + second.type_ = tempType; + second.name_ = std::move(tempName); + + switch (tempType) + { + case type::integer: + { + second.integer_ = tempInteger; + + break; + } + + case type::string: + { + new(&second.string_) std::string(std::move(tempString)); + + break; + } + + case type::invalid: + { + break; + } + } + } + + field::~field() + { + switch (type_) + { + case type::string: + { + using string_type = std::string; + string_.~string_type(); + + break; + } + + case type::integer: + case type::invalid: + { + break; + } + } + } + + field::field( + std::string name, + int arg) : + type_(type::integer), + name_(name), + integer_(arg) + { + } + + int field::getInteger() const + { + if (type_ != type::integer) + { + throw std::domain_error("field::getInteger called on non-integer field"); + } + + return integer_; + } + + field::field( + std::string name, + std::string arg) : + type_(type::string), + name_(name) + { + new(&string_) std::string(arg); + } + + std::string field::getString() const + { + if (type_ != type::string) + { + throw std::domain_error("field::getString called on non-string field"); + } + + return string_; + } + + }; +}; -- cgit 1.4.1