From 31ea9c794d605fb5eb2295d26d5f6a4e19327ee0 Mon Sep 17 00:00:00 2001 From: adjoly Date: Mon, 14 Apr 2025 18:53:03 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20finished?= =?UTF-8?q?=20ex02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-format | 5 +++ .envrc | 1 + .gitignore | 1 + ex02/Array.cpp | 0 ex02/Array.hpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ ex02/Makefile | 60 +++++++++++++++++++++++++++++++++ ex02/main.cpp | 28 ++++++++++++++++ flake.lock | 79 +++++++++++++++++++++++++++++++++++++++++++ flake.nix | 36 ++++++++++++++++++++ 9 files changed, 301 insertions(+) create mode 100644 .clang-format create mode 100644 .envrc create mode 100644 ex02/Array.cpp create mode 100644 ex02/Array.hpp create mode 100644 ex02/Makefile create mode 100644 ex02/main.cpp create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..7a2deec --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +UseTab: Always +IndentWidth: 4 +TabWidth: 4 +AlignConsecutiveDeclarations: true +ConstructorInitializerIndentWidth: 4 diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index 34e908a..8de7f41 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ compile_commands.json whatever iter +array diff --git a/ex02/Array.cpp b/ex02/Array.cpp new file mode 100644 index 0000000..e69de29 diff --git a/ex02/Array.hpp b/ex02/Array.hpp new file mode 100644 index 0000000..a613515 --- /dev/null +++ b/ex02/Array.hpp @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Array.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/09 15:36:40 by adjoly #+# #+# */ +/* Updated: 2025/04/14 18:52:43 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +#include + +static 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 +} + +template class Array { + public: + Array(void) : _arr(new T[0]), _size(0) { + _log("➕", "Array", "", "default constructor called"); + } + Array(unsigned int n) : _arr(new T[n]), _size(n) { + _log("➕", "Array", "", "size constructor called"); + } + Array(const Array &cpy) { + _log("➕", "Array", "", "copy constructor called"); + *this = cpy; + } + ~Array(void) { + _log("➖", "Array", "", "destructor called"); + delete[] _arr; + } + + Array &operator=(const Array &cpy) { + _log("➕", "Array", "", "assignement constructor called"); + if (this != &cpy) { + _size = cpy._size; + _arr = new T[_size]; + if (_arr) { + for (std::size_t i = 0; i < _size; i++) { + _arr[i] = cpy._arr[i]; + } + } + } + return (*this); + } + + T &operator[](const size_t &where) { + if (where >= _size) + throw outOfBoundException(); + + return _arr[where]; + } + + T *getArray(void) const { return _arr; } + std::size_t getSize(void) const { return _size; } + + class outOfBoundException : public std::exception { + virtual const char *what() const throw() { return ("out of bound"); } + }; + + private: + T *_arr; + std::size_t _size; + + T *_clone(void) {} +}; + +template +std::ostream &operator<<(std::ostream &os, const Array &arr) { + for (std::size_t i = 0; i < arr.getSize(); i++) { + os << arr.getArray()[i]; + } + return os; +} diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..9edbb2e --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: adjoly +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/10/25 16:09:27 by adjoly #+# #+# # +# Updated: 2025/04/09 15:36:26 by adjoly ### ########.fr # +# # +# **************************************************************************** # + +SHELL = bash + +NAME = array + +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 diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..0b56935 --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/14 18:52:19 by adjoly #+# #+# */ +/* Updated: 2025/04/14 18:52:20 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Array.hpp" +#include + +int main() { + Array str(7); + + str[0] = 10; + str[1] = 9; + str[2] = 8; + str[3] = 7; + str[4] = 6; + str[5] = 5; + str[6] = 4; + + std::cout << str << std::endl; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..c206742 --- /dev/null +++ b/flake.lock @@ -0,0 +1,79 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1733312601, + "narHash": "sha256-4pDvzqnegAfRkPwO3wmwBhVi/Sye1mzps0zHWYnP88c=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "205b12d8b7cd4802fbcb8e8ef6a0f1408781a4f9", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1734649271, + "narHash": "sha256-4EVBRhOjMDuGtMaofAIqzJbg4Ql7Ai0PSeuVZTHjyKQ=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d70bd19e0a38ad4790d3913bf08fcbfc9eeca507", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1733096140, + "narHash": "sha256-1qRH7uAUsyQI7R1Uwl4T+XvdNv778H0Nb5njNrqvylY=", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/5487e69da40cbd611ab2cadee0b4637225f7cfae.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/5487e69da40cbd611ab2cadee0b4637225f7cfae.tar.gz" + } + }, + "pogit": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1733518808, + "narHash": "sha256-tKqXoNTG1PGOnHjb6UvkSpKOZFDXDmZt1p0mw5Cic58=", + "owner": "y-syo", + "repo": "pogit", + "rev": "c3cb3fa9aefcf9e065ee27f2daa62a3826d48169", + "type": "github" + }, + "original": { + "owner": "y-syo", + "repo": "pogit", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "pogit": "pogit" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..68e9f43 --- /dev/null +++ b/flake.nix @@ -0,0 +1,36 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + pogit = { + url = "github:y-syo/pogit"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = inputs@{ nixpkgs, ... }: + let + supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f { + pkgs = import nixpkgs { inherit system; }; + }); + in { + devShells = forEachSupportedSystem ({ pkgs }: { + default = pkgs.mkShell.override + {} + { + hardeningDisable = [ "all" ]; + packages = with pkgs; [ + git + gdb + gcc11 + clang_12 + valgrind + compiledb + norminette + inputs.pogit.packages.${pkgs.system}.default + ]; + }; + }); + }; +} +