」 feat: finished ex02

This commit is contained in:
2025-04-09 14:06:06 +02:00
parent df7ffd0aeb
commit 2cca13a331
8 changed files with 357 additions and 0 deletions

60
ex01/Makefile Normal file
View File

@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2025/04/09 12:23:41 by adjoly ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
NAME = Serializer
CC = c++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
ifeq ($(VERBOSE),true)
FLAGS += -D VERBOSE
endif
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean all
.PHONY: clean fclean all re

53
ex01/Serializer.cpp Normal file
View File

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Serializer.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 11:08:07 by adjoly #+# #+# */
/* Updated: 2025/04/09 12:23:14 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Serializer.hpp"
#include <string>
void _log(std::string emoji, std::string what, std::string who,
std::string str) {
#ifdef VERBOSE
if (who.empty())
std::cout << "" << emoji << "" << what << ": " << str << std::endl;
else
std::cout << "" << emoji << "" << what << "(" << who << "): " << str
<< std::endl;
#else
(void)emoji, (void)what, (void)who, (void)str;
#endif
}
uintptr_t Serializer::serialize(Data *ptr) {
return reinterpret_cast<uintptr_t>(ptr);
}
Data *Serializer::deserialize(uintptr_t raw) {
return reinterpret_cast<Data *>(raw);
}
Serializer::Serializer(void) {
_log("", "Serializer", "", "default constructor called");
}
Serializer::Serializer(const Serializer &cpy) {
_log("", "Serializer", "", "copy constructor called");
if (this != &cpy)
*this = cpy;
}
Serializer::~Serializer(void) {
_log("", "Serializer", "", "destructor called");
}
Serializer &Serializer::operator=(const Serializer &) {
return *this;
}

32
ex01/Serializer.hpp Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Serializer.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 10:53:16 by adjoly #+# #+# */
/* Updated: 2025/04/09 12:22:12 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <stdint.h>
struct Data {
uintptr_t data;
};
class Serializer {
public:
static uintptr_t serialize(Data *ptr);
static Data *deserialize(uintptr_t raw);
private:
Serializer(void);
~Serializer(void);
Serializer(const Serializer &);
Serializer &operator=(const Serializer &);
};

30
ex01/main.cpp Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 12:16:46 by adjoly #+# #+# */
/* Updated: 2025/04/09 12:21:09 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Serializer.hpp"
#include <iostream>
int main() {
Data data;
data.data = 31;
std::cout << "base : (" << &data<< ") foo = " << data.data
<< std::endl;
uintptr_t serialized = Serializer::serialize(&data);
std::cout << "serilasize : (0x" << std::hex << serialized << std::dec
<< ")\n";
Data *deserialized = Serializer::deserialize(serialized);
std::cout << "deserialize : (" << deserialized
<< ") foo = " << deserialized->data << std::endl;
}