Arctic Noir Theme
A deep dark theme with cool icy accents for focused development.
Directories Infos
directory_info.hh
#pragma once
#include <cstdint>
#include <string>
class DirectoryInfo
{
public:
DirectoryInfo() = default;
DirectoryInfo(const std::string& name, size_t size, uint16_t rights,
const std::string& owner);
const std::string& get_name() const;
const std::string& get_owner() const;
size_t get_size() const;
uint16_t get_rights() const;
bool is_valid() const;
private:
std::string name_;
size_t size_;
uint16_t rights_;
std::string owner_;
bool is_valid_ = false;
};
directory_info.cc
#include "directory_info.hh"
DirectoryInfo::DirectoryInfo(const std::string& name, size_t size,
uint16_t rights, const std::string& owner)
: name_(name), size_(size), rights_(rights), owner_(owner), is_valid_(true)
{}
const std::string& DirectoryInfo::get_name() const { return name_; }
const std::string& DirectoryInfo::get_owner() const { return owner_; }
size_t DirectoryInfo::get_size() const { return size_; }
uint16_t DirectoryInfo::get_rights() const { return rights_; }
bool DirectoryInfo::is_valid() const { return is_valid_; }
read_info.hh
#pragma once
#include <istream>
#include "directory_info.hh"
DirectoryInfo read_info(std::istream& is);
read_info.cc
#include "read_info.hh"
#include <sstream>
#include <string>
DirectoryInfo read_info(std::istream& is)
{
std::string line;
if (!std::getline(is, line))
return DirectoryInfo{};
auto ss = std::istringstream{ line };
std::string name;
size_t size;
uint16_t rights;
std::string owner;
if (!(ss >> name >> size >> std::oct >> rights >> owner))
return DirectoryInfo{};
std::string extra;
if (ss >> extra)
return DirectoryInfo{};
return DirectoryInfo{ name, size, rights, owner };
}
Doubly Linked List
node.hh
#pragma once
#include <memory>
class Node
{
public:
Node(int v);
int get_val() const;
void set_val(int val);
std::shared_ptr<Node> get_next() const;
void set_next(std::shared_ptr<Node> next);
std::shared_ptr<Node> get_prev() const;
void set_prev(std::shared_ptr<Node> prev);
private:
int val_;
std::shared_ptr<Node> next_;
std::weak_ptr<Node> prev_;
};
node.cc
#include "node.hh"
Node::Node(int v) : val_(v) {}
int Node::get_val() const { return val_; }
void Node::set_val(int val) { val_ = val; }
std::shared_ptr<Node> Node::get_next() const { return next_; }
void Node::set_next(std::shared_ptr<Node> next) { next_ = next; }
std::shared_ptr<Node> Node::get_prev() const { return prev_.lock(); }
void Node::set_prev(std::shared_ptr<Node> prev) { prev_ = prev; }
list.hh
#pragma once
#include <optional>
#include <ostream>
#include "node.hh"
class List
{
public:
List();
void push_front(int i);
void push_back(int i);
std::optional<int> pop_front();
std::optional<int> pop_back();
void print(std::ostream& os) const;
size_t length() const;
private:
size_t nb_elts_;
std::shared_ptr<Node> first_;
std::shared_ptr<Node> last_;
};
list.cc
#include "list.hh"
List::List() : nb_elts_(0) {}
void List::push_front(int i)
{
auto node = std::make_shared<Node>(i);
if (first_)
{
node->set_next(first_);
first_->set_prev(node);
}
else
last_ = node;
first_ = node;
nb_elts_++;
}
void List::push_back(int i)
{
auto node = std::make_shared<Node>(i);
if (last_)
{
node->set_prev(last_);
last_->set_next(node);
}
else
first_ = node;
last_ = node;
nb_elts_++;
}
std::optional<int> List::pop_front()
{
if (!first_)
return std::nullopt;
int val = first_->get_val();
first_ = first_->get_next();
if (first_)
first_->set_prev(nullptr);
else
last_ = nullptr;
nb_elts_--;
return val;
}
std::optional<int> List::pop_back()
{
if (!last_)
return std::nullopt;
int val = last_->get_val();
last_ = last_->get_prev();
if (last_)
last_->set_next(nullptr);
else
first_ = nullptr;
nb_elts_--;
return val;
}
void List::print(std::ostream& os) const
{
auto current = first_;
while (current)
{
if (current != first_)
os << ' ';
os << current->get_val();
current = current->get_next();
}
}
size_t List::length() const { return nb_elts_; }
Exception
invalid_argument.hh
#pragma once
#include <exception>
#include <string>
class InvalidArgumentException : public std::exception
{
public:
InvalidArgumentException(const std::string& msg);
virtual const char* what() const noexcept;
private:
std::string msg_;
};
invalid_argument.cc
#include "invalid_argument.hh"
InvalidArgumentException::InvalidArgumentException(const std::string& msg)
: msg_(msg)
{}
const char* InvalidArgumentException::what() const noexcept
{
return msg_.c_str();
}
player.hh
#pragma once
#include <iostream>
#include "invalid_argument.hh"
class Player
{
public:
Player(const std::string& name, int age, int guess);
int get_guess() const;
friend std::ostream& operator<<(std::ostream& os, const Player& b);
bool operator==(const Player& p) const;
bool operator!=(const Player& p) const;
private:
std::string name_;
int age_;
int guess_;
};
std::ostream& operator<<(std::ostream& os, const Player& p);
player.cc
#include "player.hh"
Player::Player(const std::string& name, int age, int guess)
: name_(name), age_(age), guess_(guess)
{
if (name.empty())
throw InvalidArgumentException("Name can't be empty.");
if (age < 0)
throw InvalidArgumentException("Well, it seems you are not born yet.");
if (age > 150)
throw InvalidArgumentException("Sorry gramp, too old to play.");
}
int Player::get_guess() const { return guess_; }
bool Player::operator==(const Player& p) const { return this == &p; }
bool Player::operator!=(const Player& p) const { return this != &p; }
std::ostream& operator<<(std::ostream& os, const Player& p)
{
return os << p.name_ << '(' << p.age_ << ')';
}
game.hh
#pragma once
#include "invalid_argument.hh"
#include "player.hh"
class Game
{
public:
Game(int secret);
void play(const Player& p1, const Player& p2) const;
private:
int secret_;
};
game.cc
#include "game.hh"
#include <iostream>
Game::Game(int secret) : secret_(secret) {}
void Game::play(const Player& p1, const Player& p2) const
{
if (p1 == p2)
throw InvalidArgumentException("Stop playing by yourself!");
auto d1 = std::abs(p1.get_guess() - secret_);
auto d2 = std::abs(p2.get_guess() - secret_);
if (d1 > d2)
std::cout << p1 << " wins!" << std::endl;
else if (d1 < d2)
std::cout << p2 << " wins!" << std::endl;
else
std::cout << "Draw!" << std::endl;
}
Exist Functor
exist.hh
#pragma once
#include <set>
template <class T>
class Exist
{
public:
bool operator()(const T& value);
private:
std::set<T> seen_;
};
#include "exist.hxx"
exist.hxx
#pragma once
#include "exist.hh"
template <class T>
bool Exist<T>::operator()(const T& value)
{
if (seen_.find(value) != seen_.end())
return true;
seen_.insert(value);
return false;
}
For Each
for_each.hh
#pragma once
template <typename Iterator, typename Function>
void my_foreach(Iterator begin, Iterator end, Function f);
#include "for_each.hxx"
for_each.hxx
#pragma once
template <typename Iterator, typename Function>
void my_foreach(Iterator begin, Iterator end, Function f)
{
while (begin != end)
{
f(*begin);
begin++;
}
}
Forward Multiplication
forward_multiplication.hh
#pragma once
#include <functional>
using inner_lambda_type = const std::function<int(int rhs)>;
using outer_lambda_type = const std::function<inner_lambda_type(int lhs)>;
extern outer_lambda_type mult_by;
forward_multiplication.cc
#include "forward_multiplication.hh"
outer_lambda_type mult_by = [](https://github.com/arthurnrj/int l) { return [l](https://github.com/arthurnrj/int r) { return l * r; }; };
Hello World
hello_world.cc
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
Hot Potato
bomb.hh
#pragma once
class Bomb
{
public:
Bomb(int ticks);
void tick();
bool has_exploded() const;
private:
int max_ticks_;
int count_;
};
bomb.cc
#include "bomb.hh"
#include <iostream>
#include <stdexcept>
Bomb::Bomb(int ticks) : max_ticks_(ticks), count_(0)
{
if (ticks <= 0)
throw std::runtime_error("ticks must be strictly positive");
}
void Bomb::tick()
{
if (has_exploded())
throw std::runtime_error("bomb has already exploded");
if (count_ % 2 == 0)
std::cout << "Tic!\n";
else
std::cout << "Tac!\n";
count_++;
}
bool Bomb::has_exploded() const { return count_ >= max_ticks_; }
player.hh
#pragma once
#include <memory>
#include <string>
#include "bomb.hh"
class Player
{
public:
Player(const std::string& name, size_t nb_presses);
void pass_bomb(Player& receiver);
void press_bomb();
bool has_bomb() const;
bool is_dead() const;
void set_bomb(std::unique_ptr<Bomb> bomb);
const std::string& get_name() const;
Player* get_next() const;
void set_next(std::unique_ptr<Player> next);
private:
std::string name_;
std::unique_ptr<Bomb> bomb_;
size_t nb_presses_;
std::unique_ptr<Player> next_;
};
player.cc
#include "player.hh"
#include <iostream>
#include <stdexcept>
Player::Player(const std::string& name, size_t nb_presses)
: name_(name), bomb_(nullptr), nb_presses_(nb_presses), next_(nullptr)
{}
void Player::pass_bomb(Player& receiver)
{
if (!has_bomb())
throw std::runtime_error("player does not have a bomb");
if (receiver.has_bomb())
throw std::runtime_error("receiver already has a bomb");
std::cout << name_ << " passes the bomb to " << receiver.name_ << ".\n";
receiver.bomb_ = std::move(bomb_);
}
void Player::press_bomb()
{
if (!has_bomb())
throw std::runtime_error("player does not have a bomb");
for (size_t i = 0; i < nb_presses_; i++)
{
if (bomb_->has_exploded())
return;
bomb_->tick();
}
}
bool Player::has_bomb() const { return bomb_ != nullptr; }
bool Player::is_dead() const
{
if (bomb_)
return bomb_->has_exploded();
return false;
}
void Player::set_bomb(std::unique_ptr<Bomb> bomb) { bomb_ = std::move(bomb); }
const std::string& Player::get_name() const { return name_; }
Player* Player::get_next() const { return next_.get(); }
void Player::set_next(std::unique_ptr<Player> next) { next_ = std::move(next); }
game.hh
#pragma once
#include <string>
#include "player.hh"
class Game
{
public:
Game() = default;
void add_player(const std::string& name, size_t nb_presses);
void play(int bomb_ticks);
private:
std::unique_ptr<Player> head_;
Player* tail_ = nullptr;
};
game.cc
#include "game.hh"
#include <iostream>
#include <stdexcept>
void Game::add_player(const std::string& name, size_t nb_presses)
{
auto p = std::make_unique<Player>(name, nb_presses);
if (this->head_ == nullptr)
{
this->head_ = std::move(p);
tail_ = head_.get();
}
else
{
auto temp = p.get();
tail_->set_next(std::move(p));
tail_ = temp;
}
}
void Game::play(int bomb_ticks)
{
if (!head_ || !head_->get_next())
throw std::runtime_error("less than 2 players");
auto bomb = std::make_unique<Bomb>(bomb_ticks);
head_->set_bomb(std::move(bomb));
Player* current = head_.get();
while (true)
{
current->press_bomb();
if (current->is_dead())
{
std::cout << current->get_name() << " has exploded.\n";
return;
}
Player* next_player = current->get_next();
if (!next_player)
next_player = head_.get();
current->pass_bomb(*next_player);
current = next_player;
}
}