Implement ex01
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 22 Jan 2025 12:35:36 +0000 (13:35 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 22 Jan 2025 12:35:36 +0000 (13:35 +0100)
ex01/Data.cpp [new file with mode: 0644]
ex01/Data.h [new file with mode: 0644]
ex01/Makefile [new file with mode: 0644]
ex01/Serializer.cpp [new file with mode: 0644]
ex01/Serializer.h [new file with mode: 0644]
ex01/main.cpp [new file with mode: 0644]

diff --git a/ex01/Data.cpp b/ex01/Data.cpp
new file mode 100644 (file)
index 0000000..ae59b9e
--- /dev/null
@@ -0,0 +1,45 @@
+#include "Data.h"
+#include <iostream>
+
+Data::Data()
+       : m_i1(0)
+       , m_i2(0)
+{
+}
+
+Data::Data(int first, int second)
+       : m_i1(first)
+       , m_i2(second)
+{
+}
+
+Data::Data(const Data &other)
+{
+       *this = other;
+}
+
+Data::~Data()
+{
+}
+
+Data   &Data::operator=(const Data &other)
+{
+       if (this == &other)
+               return (*this);
+       m_i1 = other.m_i1;
+       m_i2 = other.m_i2;
+       return (*this);
+}
+
+void   Data::printAddress() const
+{
+       std::cout << this << '\n';
+}
+
+void   Data::printData() const
+{
+       std::cout
+               << "m_i1 = " << m_i1 << '\n'
+               << "m_i2 = " << m_i2 << '\n'
+               ;
+}
diff --git a/ex01/Data.h b/ex01/Data.h
new file mode 100644 (file)
index 0000000..1ddf816
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef DATA_H
+# define DATA_H
+
+class Data
+{
+       private:
+               int     m_i1;
+               int     m_i2;
+
+       public:
+               Data();
+               Data(int first, int second);
+               Data(const Data &other);
+               ~Data();
+
+               Data    &operator=(const Data &other);
+
+               void    printAddress() const;
+               void    printData() const;
+};
+
+#endif // DATA_H
diff --git a/ex01/Makefile b/ex01/Makefile
new file mode 100644 (file)
index 0000000..ac846bb
--- /dev/null
@@ -0,0 +1,54 @@
+CC := c++
+CFLAGS = -std=c++98 -Wall -Wextra -Werror -Wpedantic
+
+ifneq ("$(wildcard .debug)","")
+       CFLAGS += -g
+endif
+
+RM := rm -f
+
+INCDIR := inc
+INCDIR += $(addsuffix /inc, $(SUBPROJECTS));
+ifneq ($(INCLUDE),)
+       INCLUDE := $(addprefix -I, $(INCDIR))
+endif
+
+SRCDIR := .
+
+SOURCES :=     main.cpp                        \
+                       Data.cpp                        \
+                       Serializer.cpp          \
+
+SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
+
+OBJECTS := $(SOURCES:.cpp=.o)
+
+NAME := serialize
+
+all : $(NAME)
+
+debug : .debug
+       $(MAKE) all
+
+nodebug :
+       $(RM) .debug
+       $(MAKE) re
+
+.% :
+       $(MAKE) fclean
+       touch $@
+
+$(NAME) : $(OBJECTS)
+       $(CC) $(CFLAGS) -o $@ $^ $(LINKS)
+
+%.o : %.cpp
+       $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE)
+
+clean :
+       $(RM) $(OBJECTS)
+
+fclean : clean
+       $(RM) $(NAME)
+
+re : fclean
+       $(MAKE) all
diff --git a/ex01/Serializer.cpp b/ex01/Serializer.cpp
new file mode 100644 (file)
index 0000000..d58b83b
--- /dev/null
@@ -0,0 +1,31 @@
+#include "Serializer.h"
+
+Serializer::Serializer()
+{
+}
+
+Serializer::Serializer(const Serializer &other)
+{
+       *this = other;
+}
+
+Serializer::~Serializer()
+{
+}
+
+Serializer     &Serializer::operator=(const Serializer &other)
+{
+       if (this == &other)
+               return (*this);
+       return (*this);
+}
+
+uintptr_t      Serializer::serialize(Data *ptr)
+{
+       return (reinterpret_cast<uintptr_t>(ptr));
+}
+
+Data   *Serializer::deserialize(uintptr_t raw)
+{
+       return (reinterpret_cast<Data*>(raw));
+}
diff --git a/ex01/Serializer.h b/ex01/Serializer.h
new file mode 100644 (file)
index 0000000..e51acfb
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef SERIALIZER_H
+# define SERIALIZER_H
+
+# include "Data.h"
+# include <stdint.h>
+
+class Serializer
+{
+       private:
+               Serializer();
+               Serializer(const Serializer &other);
+               ~Serializer();
+
+               Serializer      &operator=(const Serializer &other);
+
+       public:
+               static uintptr_t        serialize(Data *ptr);
+               static Data                     *deserialize(uintptr_t raw);
+};
+
+#endif // SERIALIZER_H
diff --git a/ex01/main.cpp b/ex01/main.cpp
new file mode 100644 (file)
index 0000000..35ec160
--- /dev/null
@@ -0,0 +1,26 @@
+#include "Data.h"
+#include "Serializer.h"
+#include <iostream>
+#include <stdint.h>
+
+int    main()
+{
+       Data            d1(1, 3);
+       Data            d2(-2, 5);
+       Data            *d1_ptr;
+       uintptr_t       d1_raw;
+
+       std::cout << "With objects:\n\n";
+       d1.printAddress();
+       d1.printData();
+       d2.printAddress();
+       d2.printData();
+       std::cout << "\nRaw value of d1: ";
+       d1_raw = Serializer::serialize(&d1);
+       std::cout << std::hex << d1_raw << "\n\n";
+       std::cout << "Deserialized d1:\n\n";
+       d1_ptr = Serializer::deserialize(d1_raw);
+       d1_ptr->printAddress();
+       d1_ptr->printData();
+       return (0);
+}