ex00 and ex01

This commit is contained in:
2026-03-01 00:28:26 +01:00
commit 8a78b30d08
8 changed files with 484 additions and 0 deletions

43
ex01/Makefile Normal file
View File

@@ -0,0 +1,43 @@
NAME = serialization
SRC = main.cpp \
Serializer/Serializer.cpp
OBJ = $(SRC:.cpp=.o)
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
GREEN = \033[0;32m
RED = \033[0;31m
RESET = \033[0m
TOTAL := $(words $(SRC))
COUNT = 0
all: $(NAME)
$(NAME): $(OBJ)
@rm -f .build_start
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
@printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)"
%.o: %.cpp
@if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi
@$(eval COUNT = $(shell echo $$(($(COUNT)+1))))
@PERCENT=$$(($(COUNT)*100/$(TOTAL))); \
BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \
SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \
printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL)
@$(CC) $(CFLAGS) -c $< -o $@
clean:
@printf "$(RED)Eliminando...\n$(RESET)"
@rm -f $(OBJ)
fclean: clean
@rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

View File

@@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Serializer.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/28 23:48:07 by aortigos #+# #+# */
/* Updated: 2026/02/28 23:48:07 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Serializer.hpp"
//////////////////
// Constructors //
//////////////////
Serializer::Serializer()
{
// std::cout << "Serializer default constructor called" << std::endl;
}
Serializer::Serializer(const Serializer &other)
{
*this = other;
// std::cout << "Serializer copy constructor called" << std::endl;
}
Serializer& Serializer::operator=(const Serializer &other)
{
if (this != &other)
{
// Copy attributes here
}
// std::cout << "Serializer copy assignment operator called" << std::endl;
return (*this);
}
Serializer::~Serializer()
{
// std::cout << "Serializer destructor called" << std::endl;
}
uintptr_t Serializer::serialize(Data *ptr)
{
return (reinterpret_cast<uintptr_t>(ptr));
}
Data *Serializer::deserialize(uintptr_t raw)
{
return (reinterpret_cast<Data*>(raw));
}

View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Serializer.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/28 23:48:07 by aortigos #+# #+# */
/* Updated: 2026/02/28 23:48:07 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SERIALIZER_HPP
# define SERIALIZER_HPP
# include <iostream>
# include <stdint.h>
struct Data
{
int age;
std::string name;
};
class Serializer
{
private:
Serializer();
Serializer(const Serializer &other);
Serializer& operator=(const Serializer &other);
~Serializer();
public:
static uintptr_t serialize(Data *ptr);
static Data *deserialize(uintptr_t raw);
};
#endif

34
ex01/main.cpp Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/28 23:46:53 by aortigos #+# #+# */
/* Updated: 2026/03/01 00:25:41 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Serializer/Serializer.hpp"
int main()
{
Data *original = new Data();
original->age = 42;
original->name = "Hadi";
std::cout << "Puntero original: " << original << std::endl;
uintptr_t serialized = Serializer::serialize(original);
std::cout << "Serializado (número): " << serialized << std::endl;
Data *recovered = Serializer::deserialize(serialized);
std::cout << "Puntero recovered: " << recovered << std::endl;
std::cout << "Son iguales: " << (original == recovered) << std::endl;
std::cout << "Age recovered: " << recovered->age << std::endl;
std::cout << "Name recovered: " << recovered->name << std::endl;
delete original;
}