From 6b480ad1a3e417effeef237b1fa0bc4dc2c474c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sun, 27 Oct 2024 21:19:34 +0100 Subject: [PATCH] Unify BaseClass, Integer, Boolean with generated classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/cpp/static/BaseClass.cpp | 16 ++-- cimgen/languages/cpp/static/BaseClass.hpp | 19 +++-- cimgen/languages/cpp/static/Boolean.cpp | 63 +++++++-------- cimgen/languages/cpp/static/Boolean.hpp | 31 +++----- cimgen/languages/cpp/static/Integer.cpp | 96 ++++++++++++----------- cimgen/languages/cpp/static/Integer.hpp | 43 +++++----- 6 files changed, 127 insertions(+), 141 deletions(-) diff --git a/cimgen/languages/cpp/static/BaseClass.cpp b/cimgen/languages/cpp/static/BaseClass.cpp index 20dc2088..ff3a94a5 100644 --- a/cimgen/languages/cpp/static/BaseClass.cpp +++ b/cimgen/languages/cpp/static/BaseClass.cpp @@ -1,22 +1,20 @@ - #include "BaseClass.hpp" using namespace CIMPP; BaseClass::~BaseClass() {} -void BaseClass::addConstructToMap(std::unordered_map& factory_map) {} - -void BaseClass::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} - -void BaseClass::addClassAssignFnsToMap(std::unordered_map& assign_map) {} - const char BaseClass::debugName[] = "BaseClass"; -const char* BaseClass::debugString() +const char* BaseClass::debugString() const { return BaseClass::debugName; } -CIMPP::BaseClassDefiner BaseClass::declare() { +void BaseClass::addConstructToMap(std::unordered_map& factory_map) {} +void BaseClass::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} +void BaseClass::addClassAssignFnsToMap(std::unordered_map& assign_map) {} + +const BaseClassDefiner BaseClass::declare() +{ return BaseClassDefiner(BaseClass::addConstructToMap, BaseClass::addPrimitiveAssignFnsToMap, BaseClass::addClassAssignFnsToMap, BaseClass::debugName); } diff --git a/cimgen/languages/cpp/static/BaseClass.hpp b/cimgen/languages/cpp/static/BaseClass.hpp index a7cd6129..7af998c6 100644 --- a/cimgen/languages/cpp/static/BaseClass.hpp +++ b/cimgen/languages/cpp/static/BaseClass.hpp @@ -5,18 +5,23 @@ #define CGMES_BUILD #endif +#include #include + #include "BaseClassDefiner.hpp" -class BaseClass { +class BaseClass +{ public: enum cgmesProfile {EQ = 0, SSH = 1, TP = 2, SV = 3, DY = 4, GL = 5, DI = 6}; virtual ~BaseClass(); - static CIMPP::BaseClassDefiner declare(); - static void addConstructToMap(std::unordered_map&); - static void addPrimitiveAssignFnsToMap(std::unordered_map&); - static void addClassAssignFnsToMap(std::unordered_map&); - const static char debugName[]; - virtual const char* debugString(); + + static const char debugName[]; + virtual const char* debugString() const; + + static void addConstructToMap(std::unordered_map& factory_map); + static void addPrimitiveAssignFnsToMap(std::unordered_map& assign_map); + static void addClassAssignFnsToMap(std::unordered_map& assign_map); + static const CIMPP::BaseClassDefiner declare(); }; #endif // BASECLASS_HPP diff --git a/cimgen/languages/cpp/static/Boolean.cpp b/cimgen/languages/cpp/static/Boolean.cpp index 35f7fa9a..1818ab8a 100644 --- a/cimgen/languages/cpp/static/Boolean.cpp +++ b/cimgen/languages/cpp/static/Boolean.cpp @@ -1,21 +1,13 @@ #include "Boolean.hpp" -#include "CIMExceptions.hpp" -using namespace CIMPP; - -Boolean::Boolean(){} - -Boolean::~Boolean(){} +#include +#include -Boolean::Boolean(bool value) - : value(value), initialized(true) {} +#include "../src/CIMExceptions.hpp" -const BaseClassDefiner Boolean::declare() -{ - return BaseClassDefiner(Boolean::addConstructToMap, Boolean::addPrimitiveAssignFnsToMap, Boolean::addClassAssignFnsToMap, Boolean::debugName); -} +using namespace CIMPP; -Boolean& Boolean::operator=(bool &rop) +Boolean& Boolean::operator=(bool rop) { value = rop; initialized = true; @@ -24,56 +16,57 @@ Boolean& Boolean::operator=(bool &rop) Boolean::operator bool() { - if(!initialized) + if (!initialized) { throw new ReadingUninitializedField(); } return value; } -void Boolean::addConstructToMap(std::unordered_map& factory_map) {} - -void Boolean::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} - -void Boolean::addClassAssignFnsToMap(std::unordered_map& assign_map) {} - const char Boolean::debugName[] = "Boolean"; -const char* Boolean::debugString() +const char* Boolean::debugString() const { return Boolean::debugName; } -namespace CIMPP { +namespace CIMPP +{ std::istream& operator>>(std::istream& lop, Boolean& rop) { + rop.initialized = false; + std::string tmp; lop >> tmp; - if(tmp == "true" || tmp == "True" || tmp == "TRUE") + + if (tmp == "true" || tmp == "True" || tmp == "TRUE") { rop.value = true; rop.initialized = true; return lop; } - if(tmp == "false" || tmp == "False" || tmp == "FALSE") + if (tmp == "false" || tmp == "False" || tmp == "FALSE") { rop.value = false; rop.initialized = true; return lop; } - else - { - lop.setstate(std::ios::failbit); - return lop; - } + + lop.setstate(std::ios::failbit); + return lop; } - std::ostream& operator<<(std::ostream& os, Boolean& rop) + std::ostream& operator<<(std::ostream& os, const Boolean& obj) { - if (rop) { - os << "true"; - } - else { - os << "false"; + if (obj.initialized) + { + if (obj.value) + { + os << "true"; + } + else + { + os << "false"; + } } return os; } diff --git a/cimgen/languages/cpp/static/Boolean.hpp b/cimgen/languages/cpp/static/Boolean.hpp index 34e30d18..9dc8d09c 100644 --- a/cimgen/languages/cpp/static/Boolean.hpp +++ b/cimgen/languages/cpp/static/Boolean.hpp @@ -1,38 +1,31 @@ #ifndef BOOLEAN_H #define BOOLEAN_H -#include -#include #include +#include -#include "BaseClass.hpp" - -namespace CIMPP { +namespace CIMPP +{ /** * A type with the value space "true" and "false". */ class Boolean { public: - Boolean(); - virtual ~Boolean(); - static const BaseClassDefiner declare(); + Boolean() : value(false), initialized(false) {} + Boolean(bool value) : value(value), initialized(true) {} - Boolean(bool value); - Boolean& operator=(bool &rop); - friend std::istream& operator>>(std::istream& lop, Boolean& rop); - friend std::ostream& operator<<(std::ostream& os, Boolean& rop); + Boolean& operator=(bool rop); operator bool(); - bool value = false; - bool initialized = false; + bool value; + bool initialized; static const char debugName[]; - virtual const char* debugString(); + const char* debugString() const; - static void addConstructToMap(std::unordered_map& factory_map); - static void addPrimitiveAssignFnsToMap(std::unordered_map&); - static void addClassAssignFnsToMap(std::unordered_map&); + friend std::istream& operator>>(std::istream& lop, Boolean& rop); + friend std::ostream& operator<<(std::ostream& os, const Boolean& obj); }; } -#endif +#endif // BOOLEAN_H diff --git a/cimgen/languages/cpp/static/Integer.cpp b/cimgen/languages/cpp/static/Integer.cpp index 638dd417..2a1cda43 100644 --- a/cimgen/languages/cpp/static/Integer.cpp +++ b/cimgen/languages/cpp/static/Integer.cpp @@ -1,72 +1,74 @@ #include "Integer.hpp" -#include "CIMExceptions.hpp" -using namespace CIMPP; +#include -Integer::Integer(){} +#include "../src/CIMExceptions.hpp" -Integer::~Integer(){} +using namespace CIMPP; -Integer::Integer(long int value) - : value(value), initialized(true) {} +Integer& Integer::operator=(long int rop) +{ + value = rop; + initialized = true; + return *this; +} -const BaseClassDefiner Integer::declare() { - return BaseClassDefiner(Integer::addConstructToMap, Integer::addPrimitiveAssignFnsToMap, Integer::addClassAssignFnsToMap, Integer::debugName); +Integer::operator long int() +{ + if (!initialized) + { + throw new ReadingUninitializedField(); + } + return value; } const char Integer::debugName[] = "Integer"; -const char* Integer::debugString() { +const char* Integer::debugString() const +{ return Integer::debugName; } -void Integer::addConstructToMap(std::unordered_map& factory_map) {} - -void Integer::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} - -void Integer::addClassAssignFnsToMap(std::unordered_map& assign_map) {} - -namespace CIMPP { - Integer& Integer::operator=(long int &rop){ - value = rop; - initialized = true; - return *this; - } - - Integer& Integer::operator-=(const Integer& rhs){ - value -= rhs.value; - return *this; - } - - Integer& Integer::operator*=(const Integer& rhs){ - value *= rhs.value; - return *this; - } +Integer& Integer::operator+=(const Integer& rhs) +{ + value += rhs.value; + return *this; +} - Integer& Integer::operator/=(const Integer& rhs){ - value /= rhs.value; - return *this; - } +Integer& Integer::operator-=(const Integer& rhs) +{ + value -= rhs.value; + return *this; +} - Integer& Integer::operator+=(const Integer& rhs){ - value += rhs.value; - return *this; - } +Integer& Integer::operator*=(const Integer& rhs) +{ + value *= rhs.value; + return *this; +} - Integer::operator long int(){ - if(!initialized) - { - throw new ReadingUninitializedField(); - } - return value; - } +Integer& Integer::operator/=(const Integer& rhs) +{ + value /= rhs.value; + return *this; +} +namespace CIMPP +{ std::istream& operator>>(std::istream& lop, Integer& rop) { std::string tmp; lop >> tmp; - rop.value = stol(tmp); rop.initialized = true; return lop; } + + std::ostream& operator<<(std::ostream& os, const Integer& obj) + { + if (obj.initialized) + { + os << obj.value; + } + return os; + } } diff --git a/cimgen/languages/cpp/static/Integer.hpp b/cimgen/languages/cpp/static/Integer.hpp index a39f6c81..fab9a715 100644 --- a/cimgen/languages/cpp/static/Integer.hpp +++ b/cimgen/languages/cpp/static/Integer.hpp @@ -1,41 +1,36 @@ #ifndef INTEGER_H #define INTEGER_H -#include -#include #include +#include -#include "BaseClass.hpp" - -namespace CIMPP { - +namespace CIMPP +{ /** - * An Integer number. The range is unspecified and not limited. - */ + * An Integer number. The range is unspecified and not limited. + */ class Integer { public: - Integer(); - Integer(long int value); - virtual ~Integer(); - static const BaseClassDefiner declare(); - Integer& operator=(long int &rop); - Integer& operator+=(const Integer& rhs); - Integer& operator-=(const Integer& rhs); - Integer& operator*=(const Integer& rhs); - Integer& operator/=(const Integer& rhs); - friend std::istream& operator>>(std::istream& lop, Integer& rop); + Integer() : value(0), initialized(false) {} + Integer(long int value) : value(value), initialized(true) {} + + Integer& operator=(long int rop); operator long int(); - long int value = 0; - bool initialized = false; + long int value; + bool initialized; static const char debugName[]; - virtual const char* debugString(); + const char* debugString() const; - static void addConstructToMap(std::unordered_map& factory_map); - static void addPrimitiveAssignFnsToMap(std::unordered_map&); - static void addClassAssignFnsToMap(std::unordered_map&); + Integer& operator+=(const Integer& rhs); + Integer& operator-=(const Integer& rhs); + Integer& operator*=(const Integer& rhs); + Integer& operator/=(const Integer& rhs); + + friend std::istream& operator>>(std::istream& lop, Integer& rop); + friend std::ostream& operator<<(std::ostream& os, const Integer& obj); }; } #endif // INTEGER_H