Browse Source

Initial Commit

- added Integer boxing class
- added Class class (like Object class in Java)
- added IllegalOperationException exception
- added initial cmake project file
Patrick 3 years ago
commit
6bc7c51351

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+.idea

+ 1 - 0
source/.gitignore

@@ -0,0 +1 @@
+cmake-build-debug

+ 15 - 0
source/CMakeLists.txt

@@ -0,0 +1,15 @@
+cmake_minimum_required(VERSION 3.17)
+set(PROJECT_NAME ls_std)
+project(${PROJECT_NAME})
+
+set(CMAKE_CXX_STANDARD 11)
+
+set(SOURCE_FILES
+        ${CMAKE_CURRENT_SOURCE_DIR}/boxing/Integer.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/base/Class.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/base/Class.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/boxing/Integer.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/boxing/IBoxing.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/exception/IllegalOperationException.hpp)
+
+add_library(${PROJECT_NAME} ${SOURCE_FILES})

+ 19 - 0
source/base/Class.cpp

@@ -0,0 +1,19 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-07
+ * Changed:         2020-08-07
+ *
+ * */
+
+#include "Class.hpp"
+
+ls_std::Class::Class(std::string _name):
+name(std::move(_name))
+{}
+
+std::string ls_std::Class::getClassName()
+{
+  return this->name;
+}

+ 30 - 0
source/base/Class.hpp

@@ -0,0 +1,30 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-07
+ * Changed:         2020-08-07
+ *
+ * */
+
+#ifndef CLASS_HPP
+#define CLASS_HPP
+
+#include <string>
+
+namespace ls_std {
+  class Class {
+    public:
+
+      explicit Class(std::string _name);
+      ~Class() = default;
+
+      std::string getClassName();
+
+    private:
+
+      std::string name {};
+  };
+}
+
+#endif

+ 26 - 0
source/boxing/IBoxing.hpp

@@ -0,0 +1,26 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-07
+ * Changed:         2020-08-07
+ *
+ * */
+
+#ifndef I_BOXING_HPP
+#define I_BOXING_HPP
+
+#include <string>
+
+namespace ls_std {
+  class IBoxing {
+    public:
+
+      IBoxing() = default;
+      ~IBoxing() = default;
+
+      virtual void parse(std::string parseText) = 0;
+  };
+}
+
+#endif

+ 228 - 0
source/boxing/Integer.cpp

@@ -0,0 +1,228 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-07
+ * Changed:         2020-08-07
+ *
+ * */
+
+#include "Integer.hpp"
+#include "../exception/IllegalOperationException.hpp"
+
+ls_std::Integer::Integer(int _value) : Class("Integer"),
+value(_value)
+{}
+
+ls_std::Integer::Integer() : Class("Integer")
+{}
+
+ls_std::Integer& ls_std::Integer::operator=(int _value)
+{
+  this->value = _value;
+  return *this;
+}
+
+int ls_std::Integer::operator-() const
+{
+  return -this->value;
+}
+
+int ls_std::Integer::operator+(const Integer &_integer) const
+{
+  return this->value + _integer.value;
+}
+
+int ls_std::Integer::operator+(int _value) const
+{
+  return this->value + _value;
+}
+
+int ls_std::Integer::operator*(const Integer &_integer) const
+{
+  return this->value * _integer.value;
+}
+
+int ls_std::Integer::operator*(int _value) const
+{
+  return this->value * _value;
+}
+
+int ls_std::Integer::operator-(const Integer &_integer) const
+{
+  return this->value - _integer.value;
+}
+
+int ls_std::Integer::operator-(int _value) const
+{
+  return this->value - _value;
+}
+
+int ls_std::Integer::operator/(const Integer &_integer) const
+{
+  if(_integer.value == 0) {
+    throw ls_std::IllegalOperationException {};
+  }
+
+  return this->value / _integer.value;
+}
+
+int ls_std::Integer::operator/(int _value) const
+{
+  if(_value == 0) {
+    throw ls_std::IllegalOperationException {};
+  }
+
+  return this->value / _value;
+}
+
+ls_std::Integer & ls_std::Integer::operator+=(const Integer &_integer)
+{
+  this->value += _integer.value;
+  return *this;
+}
+
+ls_std::Integer & ls_std::Integer::operator+=(int _value)
+{
+  this->value += _value;
+  return *this;
+}
+
+ls_std::Integer & ls_std::Integer::operator-=(const Integer &_integer)
+{
+  this->value -= _integer.value;
+  return *this;
+}
+
+ls_std::Integer & ls_std::Integer::operator-=(int _value)
+{
+  this->value -= _value;
+  return *this;
+}
+
+ls_std::Integer & ls_std::Integer::operator*=(const Integer &_integer)
+{
+  this->value *= _integer.value;
+  return *this;
+}
+
+ls_std::Integer & ls_std::Integer::operator*=(int _value)
+{
+  this->value *= _value;
+  return *this;
+}
+
+ls_std::Integer & ls_std::Integer::operator/=(const Integer &_integer)
+{
+  if(_integer.value == 0) {
+    throw ls_std::IllegalOperationException {};
+  }
+
+  this->value /= _integer.value;
+  return *this;
+}
+
+ls_std::Integer & ls_std::Integer::operator/=(int _value)
+{
+  if(_value == 0) {
+    throw ls_std::IllegalOperationException {};
+  }
+
+  this->value /= _value;
+  return *this;
+}
+
+bool ls_std::Integer::operator==(const Integer &_integer) const
+{
+  return this->value == _integer.value;
+}
+
+bool ls_std::Integer::operator==(int _value) const
+{
+  return this->value == _value;
+}
+
+bool ls_std::Integer::operator!=(const Integer &_integer) const
+{
+  return this->value == _integer.value;
+}
+
+bool ls_std::Integer::operator!=(int _value) const
+{
+  return this->value == _value;
+}
+
+bool ls_std::Integer::operator>(const Integer &_integer) const
+{
+  return this->value > _integer.value;
+}
+
+bool ls_std::Integer::operator>(int _value) const
+{
+  return this->value > _value;
+}
+
+bool ls_std::Integer::operator>=(const Integer &_integer) const
+{
+  return this->value >= _integer.value;
+}
+
+bool ls_std::Integer::operator>=(int _value) const
+{
+  return this->value >= _value;
+}
+
+bool ls_std::Integer::operator<(const Integer &_integer) const
+{
+  return this->value < _integer.value;
+}
+
+bool ls_std::Integer::operator<(int _value) const
+{
+  return this->value < _value;
+}
+
+bool ls_std::Integer::operator<=(const Integer &_integer) const
+{
+  return this->value <= _integer.value;
+}
+
+bool ls_std::Integer::operator<=(int _value) const
+{
+  return this->value <= _value;
+}
+
+bool ls_std::Integer::operator&&(const Integer &_integer) const
+{
+  return this->value && _integer.value;
+}
+
+bool ls_std::Integer::operator&&(int _value) const
+{
+  return this->value && _value;
+}
+
+bool ls_std::Integer::operator||(const Integer &_integer) const
+{
+  return this->value || _integer.value;
+}
+
+bool ls_std::Integer::operator||(int _value) const
+{
+  return this->value || _value;
+}
+
+void ls_std::Integer::operator++()
+{
+  this->value += 1;
+}
+
+void ls_std::Integer::operator--()
+{
+  this->value -= 1;
+}
+
+void ls_std::Integer::parse(std::string parseText)
+{
+  this->value = std::stoi(parseText);
+}

+ 102 - 0
source/boxing/Integer.hpp

@@ -0,0 +1,102 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-07
+ * Changed:         2020-08-07
+ *
+ * */
+
+#ifndef INTEGER_HPP
+#define INTEGER_HPP
+
+#include "../base/Class.hpp"
+#include "IBoxing.hpp"
+
+namespace ls_std {
+  class Integer : public Class, IBoxing {
+    public:
+
+      explicit Integer(int _value);
+      Integer();
+      ~Integer() = default;
+
+      // assignment operators
+
+      Integer& operator=(int _value);
+
+      // arithmetic operators
+
+      int operator-() const;
+      int operator+(const Integer& _integer) const;
+      int operator+(int _value) const;
+      int operator*(const Integer& _integer) const;
+      int operator*(int _value) const;
+      int operator-(const Integer& _integer) const;
+      int operator-(int _value) const;
+      int operator/(const Integer& _integer) const;
+      int operator/(int _value) const;
+
+      // compound operators
+
+      Integer& operator+=(const Integer& _integer);
+      Integer& operator+=(int _value);
+      Integer& operator-=(const Integer& _integer);
+      Integer& operator-=(int _value);
+      Integer& operator*=(const Integer& _integer);
+      Integer& operator*=(int _value);
+      Integer& operator/=(const Integer& _integer);
+      Integer& operator/=(int _value);
+
+      // comparison operators
+
+      bool operator==(const Integer& _integer) const;
+      bool operator==(int _value) const;
+      bool operator!=(const Integer& _integer) const;
+      bool operator!=(int _value) const;
+      bool operator>(const Integer& _integer) const;
+      bool operator>(int _value) const;
+      bool operator>=(const Integer& _integer) const;
+      bool operator>=(int _value) const;
+      bool operator<(const Integer& _integer) const;
+      bool operator<(int _value) const;
+      bool operator<=(const Integer& _integer) const;
+      bool operator<=(int _value) const;
+
+      // stream operators
+
+      friend std::ostream& operator<<(std::ostream& outputStream, const Integer& _integer) {
+        outputStream << _integer.value;
+        return outputStream;
+      }
+
+      friend std::istream& operator>>(std::istream& inputStream, Integer& _integer) {
+        inputStream >> _integer.value;
+        return inputStream;
+      }
+
+      // logical operators
+
+      friend bool operator!(const Integer& _integer) {
+        return !_integer.value;
+      }
+
+      bool operator&&(const Integer& _integer) const;
+      bool operator&&(int _value) const;
+      bool operator||(const Integer& _integer) const;
+      bool operator||(int _value) const;
+
+      // increment / decrement operator
+
+      void operator++();
+      void operator--();
+
+      // implementation
+
+      void parse(std::string parseText) override;
+
+      int value {};
+  };
+}
+
+#endif

+ 25 - 0
source/exception/IllegalOperationException.hpp

@@ -0,0 +1,25 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-07
+ * Changed:         2020-08-07
+ *
+ * */
+
+#ifndef ILLEGAL_OPERATION_EXCEPTION_HPP
+#define ILLEGAL_OPERATION_EXCEPTION_HPP
+
+#include <exception>
+
+namespace ls_std {
+  class IllegalOperationException : public std::exception {
+    public:
+
+      const char *what() const noexcept override {
+        return "operation is not valid...";
+      }
+  };
+}
+
+#endif