From 6169d697a5be59426d034b878bffc848de49491d Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 18 Nov 2020 10:30:31 +0100 Subject: Added cpp06/ex01 and ex02 --- .gitignore | 1 + cpp06/ex01/Makefile | 35 +++++++++++++++ cpp06/ex01/main.cpp | 100 +++++++++++++++++++++++++++++++++++++++++++ cpp06/ex01/serialization.hpp | 32 ++++++++++++++ cpp06/ex02/A.cpp | 18 ++++++++ cpp06/ex02/A.hpp | 7 ++- cpp06/ex02/B.cpp | 18 ++++++++ cpp06/ex02/B.hpp | 7 ++- cpp06/ex02/Base.cpp | 18 ++++++++ cpp06/ex02/Base.hpp | 5 ++- cpp06/ex02/C.cpp | 18 ++++++++ cpp06/ex02/C.hpp | 7 ++- cpp06/ex02/Makefile | 10 ++--- cpp06/ex02/main.cpp | 92 ++++++++++++++++++++++++++++++++++++++- 14 files changed, 357 insertions(+), 11 deletions(-) create mode 100644 cpp06/ex01/Makefile create mode 100644 cpp06/ex01/main.cpp create mode 100644 cpp06/ex01/serialization.hpp create mode 100644 cpp06/ex02/A.cpp create mode 100644 cpp06/ex02/B.cpp create mode 100644 cpp06/ex02/Base.cpp create mode 100644 cpp06/ex02/C.cpp diff --git a/.gitignore b/.gitignore index 5190700..7e84749 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ iter array identify_real_type scalar_conversion +serialization *_shrubbery diff --git a/cpp06/ex01/Makefile b/cpp06/ex01/Makefile new file mode 100644 index 0000000..fff9a9f --- /dev/null +++ b/cpp06/ex01/Makefile @@ -0,0 +1,35 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/11/18 09:31:16 by charles #+# #+# # +# Updated: 2020/11/18 10:17:03 by charles ### ########.fr # +# # +# ############################################################################ # + +NAME = serialization + +CXX = clang++ +CXXFLAGS = -std=c++98 -Wall -Wextra -Werror + +SRC = main.cpp +OBJ = $(SRC:.cpp=.o) + +all: $(NAME) + +$(NAME): $(OBJ) + $(CXX) -o $@ $^ + +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $^ + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) + +re: fclean all diff --git a/cpp06/ex01/main.cpp b/cpp06/ex01/main.cpp new file mode 100644 index 0000000..3786106 --- /dev/null +++ b/cpp06/ex01/main.cpp @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/18 09:32:02 by charles #+# #+# */ +/* Updated: 2020/11/18 10:22:26 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include +#include +#include + +#include "serialization.hpp" + +void generate_chars(char c[8]) +{ + char choices[] = + { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' + }; + for (int i = 0; i < 8; i++) + c[i] = choices[rand() % (sizeof(choices) / sizeof(char))]; +} + +void* serialize(void) +{ + RawData *d = new RawData; + + static const char choices[] = + { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' + }; + size_t choices_len = sizeof(choices) / sizeof(char); + for (int i = 0; i < 8; i++) + { + d->c1[i] = choices[rand() % choices_len]; + d->c2[i] = choices[rand() % choices_len]; + } + d->n = rand() % INT_MAX; + return reinterpret_cast(d); +} + +Data* deserialize(void* raw) +{ + RawData *raw_data = reinterpret_cast(raw); + Data *data = new Data; + + char tmp[9] = {'\0'}; + memcpy(tmp, raw_data->c1, 8 * sizeof(char)); + data->s1 = tmp; + memcpy(tmp, raw_data->c2, 8 * sizeof(char)); + data->s2 = tmp; + data->n = raw_data->n; + return data; +} + +int main() +{ + int seed; + std::ifstream devRandom("/dev/random"); + if (devRandom.is_open()) + { + devRandom.read((char*)&seed, sizeof(int)); + devRandom.close(); + } + else + { + seed = time(NULL); + } + srand(seed); + + for (int i = 0; i < 20; i++) + { + void *raw = serialize(); + Data *data = deserialize(raw); + std::cout << "s1: " << data->s1 + << ", n: " << std::setw(10) << data->n + << ", s2: " << data->s2 + << std::endl; + delete reinterpret_cast(raw); + delete data; + } + return 0; +} diff --git a/cpp06/ex01/serialization.hpp b/cpp06/ex01/serialization.hpp new file mode 100644 index 0000000..36a5434 --- /dev/null +++ b/cpp06/ex01/serialization.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* serialization.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/18 10:22:17 by charles #+# #+# */ +/* Updated: 2020/11/18 10:22:35 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SERIALIZATION_HPP +# define SERIALIZATION_HPP + +# include + +struct RawData +{ + char c1[8]; + int n; + char c2[8]; +}; + +struct Data +{ + std::string s1; + int n; + std::string s2; +}; + +#endif diff --git a/cpp06/ex02/A.cpp b/cpp06/ex02/A.cpp new file mode 100644 index 0000000..caad17c --- /dev/null +++ b/cpp06/ex02/A.cpp @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* A.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/18 09:18:09 by charles #+# #+# */ +/* Updated: 2020/11/18 09:21:08 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "A.hpp" + +A::A() {} +A::A(A const& other) { (void)other; } +A& A::operator=(A const& other) { (void)other; return *this; } +A::~A() {} diff --git a/cpp06/ex02/A.hpp b/cpp06/ex02/A.hpp index 30c68d5..4738d7c 100644 --- a/cpp06/ex02/A.hpp +++ b/cpp06/ex02/A.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 09:56:52 by charles #+# #+# */ -/* Updated: 2020/04/15 10:03:37 by charles ### ########.fr */ +/* Updated: 2020/11/18 09:22:09 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,11 @@ class A : public Base { +public: + A(); + A(A const& other); + A& operator=(A const& other); + virtual ~A(); }; #endif diff --git a/cpp06/ex02/B.cpp b/cpp06/ex02/B.cpp new file mode 100644 index 0000000..133e61d --- /dev/null +++ b/cpp06/ex02/B.cpp @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* B.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/18 09:18:09 by charles #+# #+# */ +/* Updated: 2020/11/18 09:21:15 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "B.hpp" + +B::B() {} +B::B(B const& other) { (void)other; } +B& B::operator=(B const& other) { (void)other; return *this; } +B::~B() {} diff --git a/cpp06/ex02/B.hpp b/cpp06/ex02/B.hpp index aeac82e..c9e4a44 100644 --- a/cpp06/ex02/B.hpp +++ b/cpp06/ex02/B.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 09:57:12 by charles #+# #+# */ -/* Updated: 2020/04/15 10:07:52 by charles ### ########.fr */ +/* Updated: 2020/11/18 09:22:13 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,11 @@ class B : public Base { +public: + B(); + B(B const& other); + B& operator=(B const& other); + virtual ~B(); }; #endif diff --git a/cpp06/ex02/Base.cpp b/cpp06/ex02/Base.cpp new file mode 100644 index 0000000..0a601fd --- /dev/null +++ b/cpp06/ex02/Base.cpp @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/18 09:20:02 by charles #+# #+# */ +/* Updated: 2020/11/18 09:21:22 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Base.hpp" + +Base::Base() {} +Base::Base(Base const& other) { (void)other; } +Base& Base::operator=(Base const& other) { (void)other; return *this; } +Base::~Base() {} diff --git a/cpp06/ex02/Base.hpp b/cpp06/ex02/Base.hpp index c70706a..0ec092b 100644 --- a/cpp06/ex02/Base.hpp +++ b/cpp06/ex02/Base.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 09:56:24 by charles #+# #+# */ -/* Updated: 2020/04/15 09:56:41 by charles ### ########.fr */ +/* Updated: 2020/11/18 09:16:48 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,9 @@ class Base { public: + Base(); + Base(Base const& other); + Base& operator=(Base const& other); virtual ~Base(); }; diff --git a/cpp06/ex02/C.cpp b/cpp06/ex02/C.cpp new file mode 100644 index 0000000..81bb62a --- /dev/null +++ b/cpp06/ex02/C.cpp @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* C.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/18 09:18:09 by charles #+# #+# */ +/* Updated: 2020/11/18 09:21:01 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "C.hpp" + +C::C() {} +C::C(C const& other) { (void)other; } +C& C::operator=(C const& other) { (void)other; return *this; } +C::~C() {} diff --git a/cpp06/ex02/C.hpp b/cpp06/ex02/C.hpp index 537c0c5..ee26bbb 100644 --- a/cpp06/ex02/C.hpp +++ b/cpp06/ex02/C.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 09:57:24 by charles #+# #+# */ -/* Updated: 2020/04/15 10:03:30 by charles ### ########.fr */ +/* Updated: 2020/11/18 09:22:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,11 @@ class C : public Base { +public: + C(); + C(C const& other); + C& operator=(C const& other); + virtual ~C(); }; #endif diff --git a/cpp06/ex02/Makefile b/cpp06/ex02/Makefile index b68a586..8a8ffca 100644 --- a/cpp06/ex02/Makefile +++ b/cpp06/ex02/Makefile @@ -6,7 +6,7 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/04/15 09:59:30 by charles #+# #+# # -# Updated: 2020/04/15 10:00:05 by charles ### ########.fr # +# Updated: 2020/11/18 09:22:44 by charles ### ########.fr # # # # **************************************************************************** # @@ -15,21 +15,21 @@ NAME = identify_real_type CXX = clang++ CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -SRC = main.cpp +SRC = main.cpp A.cpp B.cpp C.cpp Base.cpp OBJ = $(SRC:.cpp=.o) all: $(NAME) $(NAME): $(OBJ) - $(CXX) -o $@ $< + $(CXX) -o $@ $^ %.o: %.cpp $(CXX) $(CXXFLAGS) -c -o $@ $^ clean: - rm -rf $(OBJ) + rm -f $(OBJ) fclean: clean - rm -rf $(NAME) + rm -f $(NAME) re: fclean all diff --git a/cpp06/ex02/main.cpp b/cpp06/ex02/main.cpp index 17c0c3d..86cd78f 100644 --- a/cpp06/ex02/main.cpp +++ b/cpp06/ex02/main.cpp @@ -6,12 +6,14 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 10:00:18 by charles #+# #+# */ -/* Updated: 2020/04/15 10:05:23 by charles ### ########.fr */ +/* Updated: 2020/11/18 09:29:49 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include +#include #include +#include #include "Base.hpp" #include "A.hpp" #include "B.hpp" @@ -28,9 +30,95 @@ Base* generate(void) return NULL; } +void identify_from_pointer(Base *p) +{ + if (dynamic_cast(p) != NULL) + std::cout << "A" << std::endl; + else if (dynamic_cast(p) != NULL) + std::cout << "B" << std::endl; + else if (dynamic_cast(p) != NULL) + std::cout << "C" << std::endl; + else + std::cout << "Couldn't identify from pointer" << std::endl; +} + +void identify_from_reference(Base &p) +{ + try + { + (void)dynamic_cast(p); + std::cout << "A" << std::endl; + return; + } + catch (std::exception &e) + {} + try + { + (void)dynamic_cast(p); + std::cout << "B" << std::endl; + return; + } + catch (std::exception &e) + {} + try + { + (void)dynamic_cast(p); + std::cout << "C" << std::endl; + return; + } + catch (std::exception &e) + {} + + std::cout << "Couldn't identify from reference" << std::endl; +} + int main() { - srand(time(NULL)); + int seed; + std::ifstream devRandom("/dev/random"); + if (devRandom.is_open()) + { + devRandom.read((char*)&seed, sizeof(int)); + devRandom.close(); + } + else + { + seed = time(NULL); + } + srand(seed); + + { + std::cout << "=============== IDENTIFY FROM POINTER ===============" << std::endl; + Base *a = new A(); + Base *b = new B(); + Base *c = new C(); + identify_from_pointer(a); + identify_from_pointer(b); + identify_from_pointer(c); + delete a; + delete b; + delete c; + } + + { + std::cout << "=============== IDENTIFY FROM REF ===============" << std::endl; + A a; + B b; + C c; + identify_from_reference(a); + identify_from_reference(b); + identify_from_reference(c); + } + + std::cout << "=============== GENERATE ===============" << std::endl; + for (int i = 0; i < 10; i++) + { + Base *b = generate(); + std::cout << "---" << std::endl; + identify_from_pointer(b); + identify_from_reference(*b); + delete b; + } return 0; } -- cgit