diff --git a/d06/ex01/Makefile b/d06/ex01/Makefile new file mode 100644 index 0000000..b97ae84 --- /dev/null +++ b/d06/ex01/Makefile @@ -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 + diff --git a/d06/ex01/headers/Data.hpp b/d06/ex01/headers/Data.hpp new file mode 100644 index 0000000..ec9cc8d --- /dev/null +++ b/d06/ex01/headers/Data.hpp @@ -0,0 +1,9 @@ +#ifndef DATA_HPP +# define DATA_HPP + +struct Data { +public: + std::string str; +}; + +#endif diff --git a/d06/ex01/serialize b/d06/ex01/serialize new file mode 100755 index 0000000..06dc451 Binary files /dev/null and b/d06/ex01/serialize differ diff --git a/d06/ex01/srcs/main.cpp b/d06/ex01/srcs/main.cpp new file mode 100644 index 0000000..5b20135 --- /dev/null +++ b/d06/ex01/srcs/main.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include "Data.hpp" + +/* + * template < typename T > + * void printBits(T num) + * { + * unsigned long int *p = reinterpret_cast(&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(ptr) ); +} +Data* deserialize(uintptr_t raw) { + return ( reinterpret_cast(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; +} +