d06 ex01 ok

This commit is contained in:
hugogogo
2022-03-06 20:24:43 +01:00
parent 6c16b9c1d6
commit c705276319
4 changed files with 125 additions and 0 deletions

75
d06/ex01/Makefile Normal file
View File

@@ -0,0 +1,75 @@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . name = value \ . += append to a variable #
# VARIABLES . value . != set result of command #
# . name is case sensitive . ?= set if not already set #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = serialize
#TYPE = c
TYPE = cpp
ifeq "$(TYPE)" "c"
CC = c
EXT = c
else ifeq "$(TYPE)" "cpp"
CC = clang++
EXT = cpp
endif
CFLAGS = -Wall -Wextra -Werror $(INCLUDES)
ifeq "$(TYPE)" "cpp"
CFLAGS += -std=c++98
endif
VPATH = $(D_SRCS)
LIBS =
INCLUDES = -I$(D_HEADERS)
D_SRCS = srcs
SRCS = main.cpp
D_HEADERS = headers
HEADERS = Data.hpp
D_OBJS = builds
OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o)
ifeq "$(D_OBJS)" "."
RM_OBJS = rm -f $(OBJS)
else
RM_OBJS = rm -rf $(D_OBJS)
endif
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . target: prerequisites . $@ : target #
# RULES . recipe . $< : 1st prerequisite #
# . recipe . $^ : all prerequisites #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
all: $(NAME)
$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS)
$(CC) $(CFLAGS) -c $< -o $@
$(D_OBJS):
mkdir $@
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
clean:
$(RM_OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY : all clean fclean re

View File

@@ -0,0 +1,9 @@
#ifndef DATA_HPP
# define DATA_HPP
struct Data {
public:
std::string str;
};
#endif

BIN
d06/ex01/serialize Executable file

Binary file not shown.

41
d06/ex01/srcs/main.cpp Normal file
View File

@@ -0,0 +1,41 @@
#include <iostream>
#include <string>
#include <stdint.h>
#include "Data.hpp"
/*
* template < typename T >
* void printBits(T num)
* {
* unsigned long int *p = reinterpret_cast<unsigned long int *>(&num);
*
* for (unsigned long int mask = 1LU << (sizeof(T) *8 -1); mask; mask >>= 1)
* std::cout << ((*p & mask) != 0);
* }
*/
uintptr_t serialize(Data* ptr) {
return ( reinterpret_cast<uintptr_t>(ptr) );
}
Data* deserialize(uintptr_t raw) {
return ( reinterpret_cast<Data*>(raw) );
}
int main() {
Data data;
Data * data_ptr = & data;
uintptr_t raw;
data.str = "42";
raw = serialize(data_ptr);
// printBits(raw);
std::cout << " " << raw << "\n";
data_ptr = deserialize(raw);
// printBits(data_ptr);
std::cout << " " << data_ptr << "\n";
std::cout << data_ptr->str << "\n";
return 0;
}