「✨」 feat: finished ex02
This commit is contained in:
60
ex01/Makefile
Normal file
60
ex01/Makefile
Normal 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
53
ex01/Serializer.cpp
Normal 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
32
ex01/Serializer.hpp
Normal 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
30
ex01/main.cpp
Normal 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;
|
||||
}
|
81
ex02/Base.cpp
Normal file
81
ex02/Base.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Base.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/09 13:23:31 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/09 14:05:54 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Base.hpp"
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Base *generate(void) {
|
||||
std::srand(time(0));
|
||||
|
||||
switch (std::rand() % 3) {
|
||||
case 0:
|
||||
std::cout << "just made an A class !" << std::endl;
|
||||
return new A;
|
||||
case 1:
|
||||
std::cout << "just made an B class !" << std::endl;
|
||||
return new B;
|
||||
default:
|
||||
std::cout << "just made an C class !" << std::endl;
|
||||
return new C;
|
||||
};
|
||||
}
|
||||
|
||||
void identify(Base *p) {
|
||||
if (dynamic_cast<A *>(p))
|
||||
std::cout << "omg it is an A class !" << std::endl;
|
||||
else if (dynamic_cast<B *>(p))
|
||||
std::cout << "omg it is an B class !" << std::endl;
|
||||
else if (dynamic_cast<C *>(p))
|
||||
std::cout << "omg it is an C class !" << std::endl;
|
||||
else
|
||||
std::cout << "this is none of those class :(" << std::endl;
|
||||
}
|
||||
|
||||
void identify(Base &p) {
|
||||
try {
|
||||
(void)dynamic_cast<A &>(p);
|
||||
std::cout << "omg it is an A class !" << std::endl;
|
||||
return;
|
||||
} catch (...) {
|
||||
}
|
||||
try {
|
||||
(void)dynamic_cast<B &>(p);
|
||||
std::cout << "omg it is an B class !" << std::endl;
|
||||
return;
|
||||
} catch (...) {
|
||||
}
|
||||
try {
|
||||
(void)dynamic_cast<C &>(p);
|
||||
std::cout << "omg it is an C class !" << std::endl;
|
||||
return;
|
||||
} catch (...) {
|
||||
}
|
||||
std::cout << "this is none of those class :(" << std::endl;
|
||||
}
|
||||
|
||||
Base::~Base(void) {
|
||||
_log("➖", "Base", "", "destructor called");
|
||||
}
|
29
ex02/Base.hpp
Normal file
29
ex02/Base.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Base.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/09 13:19:32 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/09 14:02:02 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
class Base {
|
||||
public:
|
||||
virtual ~Base(void);
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
class A : public Base {};
|
||||
class B : public Base {};
|
||||
class C : public Base {};
|
||||
|
||||
Base *generate();
|
||||
void identify(Base *p);
|
||||
void identify(Base &p);
|
60
ex02/Makefile
Normal file
60
ex02/Makefile
Normal 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 13:58:25 by adjoly ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SHELL = bash
|
||||
|
||||
NAME = Base
|
||||
|
||||
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
|
12
ex02/main.cpp
Normal file
12
ex02/main.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
#include "Base.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
Base *base = generate();
|
||||
std::cout << "pointer idendifer : ";
|
||||
identify(base);
|
||||
std::cout << "ref idendifer : ";
|
||||
identify(*base);
|
||||
delete base;
|
||||
}
|
Reference in New Issue
Block a user