Phantom Dusk Theme
A ghostly dark theme with muted purple tones for elegant code.
Path
path.hh
#pragma once
#include <string>
#include <vector>
class Path
{
public:
Path() = default;
virtual ~Path() = default;
virtual std::string to_string() const = 0;
virtual Path& join(const std::string& tail, bool is_file = false);
protected:
std::vector<std::string> path_;
bool final_ = false;
};
path.cc
#include "path.hh"
Path& Path::join(const std::string& tail, bool is_file)
{
if (final_)
return *this;
path_.push_back(tail);
final_ = is_file;
return *this;
}
unix_path.hh
#pragma once
#include "path.hh"
class UnixPath : public Path
{
public:
UnixPath();
std::string to_string() const override;
};
unix_path.cc
#include "unix_path.hh"
#include <sstream>
UnixPath::UnixPath() : Path() {}
std::string UnixPath::to_string() const
{
std::ostringstream oss;
oss << "/";
for (const auto& component : path_)
{
if (component == path_.back() && final_)
oss << component;
else
oss << component << "/";
}
return oss.str();
}
windows_path.hh
#pragma once
#include "path.hh"
class WindowsPath : public Path
{
public:
WindowsPath(char drive);
std::string to_string() const override;
Path& join(const std::string& tail, bool is_file = false) override;
private:
char drive_;
};
windows_path.cc
#include "windows_path.hh"
#include <sstream>
#include <string>
WindowsPath::WindowsPath(char drive) : Path(), drive_(drive) {}
Path& WindowsPath::join(const std::string& tail, bool is_file)
{
if (tail.find_last_of(":\"|?*") != std::string::npos)
return *this;
return Path::join(tail, is_file);
}
std::string WindowsPath::to_string() const
{
std::ostringstream oss;
oss << drive_ << ":\\";
for (const auto& component : path_)
{
if (component == path_.back() && final_)
oss << component;
else
oss << component << "\\";
}
return oss.str();
}
Ref Swap
ref_swap.hh
#pragma once
void ref_swap(int& a, int& b);
void ptr_swap(int* a, int* b);
ref_swap.cc
void ref_swap(int& a, int& b)
{
auto temp = a;
a = b;
b = temp;
}
void ptr_swap(int* a, int* b)
{
if (a == nullptr || b == nullptr)
return;
auto temp = *a;
*a = *b;
*b = temp;
}
Replace
replace.hh
#pragma once
#include <fstream>
#include <iostream>
#include <string>
void replace(const std::string& input_filename,
const std::string& output_filename, const std::string& src_token,
const std::string& dst_token);
replace.cc
#include <fstream>
#include <iostream>
#include <string>
void replace(const std::string& input_filename,
const std::string& output_filename, const std::string& src_token,
const std::string& replaceWord)
{
std::ofstream outFile(output_filename);
std::ifstream readFile(input_filename);
if (!readFile.is_open())
{
std::cerr << "Cannot open input file\n";
return;
}
if (!outFile.is_open())
{
std::cerr << "Cannot write output file\n";
return;
}
std::string line;
while (std::getline(readFile, line))
{
size_t pos = line.find(src_token);
while (pos != std::string::npos)
{
line.replace(pos, src_token.length(), replaceWord);
pos = line.find(src_token, pos + replaceWord.length());
}
outFile << line << "\n";
}
}
Singleton
singleton.hh
#pragma once
#include <string>
template <typename T>
class Singleton
{
public:
static T& instance();
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
protected:
Singleton() = default;
};
#include "singleton.hxx"
class Logger : public Singleton<Logger>
{
friend class Singleton<Logger>;
public:
void open_log_file(const std::string& file);
void write_to_log_file();
void close_log_file();
private:
Logger() = default;
};
singleton.hxx
#pragma once
#include "singleton.hh"
template <typename T>
T& Singleton<T>::instance()
{
static T inst;
return inst;
}
singleton.cc
#include "singleton.hh"
#include <iostream>
void Logger::open_log_file(const std::string& file)
{
std::cout << "LOG: Opening file " << file << "\n";
}
void Logger::write_to_log_file()
{
std::cout << "LOG: Writing into log file ...\n";
}
void Logger::close_log_file()
{
std::cout << "LOG: Closing log file ...\n";
}
Smart Pointer
shared_pointer.hh
#pragma once
#include <cassert>
#include <iostream>
#include <string>
template <typename T>
class SharedPointer
{
public:
using element_type = T;
SharedPointer(element_type* p = nullptr);
~SharedPointer();
SharedPointer(const SharedPointer<element_type>& other);
void reset(element_type* p);
SharedPointer<element_type>& operator=(const SharedPointer<element_type>& other);
element_type& operator*() const;
element_type* operator->() const;
element_type* get() const;
long use_count() const;
template <typename U>
bool operator==(const SharedPointer<U>& rhs) const;
template <typename U>
bool operator!=(const SharedPointer<U>& rhs) const;
bool operator==(const element_type* p) const;
bool operator!=(const element_type* p) const;
SharedPointer<element_type>& operator=(SharedPointer&& other) noexcept;
SharedPointer(SharedPointer&& other);
operator bool() const;
template <typename U>
bool is_a() const;
private:
element_type* data_;
long* count_;
};
#include "shared_pointer.hxx"
shared_pointer.hxx
#pragma once
template <typename T>
SharedPointer<T>::SharedPointer(element_type* p)
: data_(p)
, count_(p ? new long(1) : nullptr)
{}
template <typename T>
SharedPointer<T>::~SharedPointer()
{
if (count_)
{
(*count_)--;
if (*count_ == 0)
{
delete data_;
delete count_;
}
}
}
template <typename T>
SharedPointer<T>::SharedPointer(const SharedPointer<element_type>& other)
: data_(other.data_)
, count_(other.count_)
{
if (count_)
(*count_)++;
}
template <typename T>
SharedPointer<T>& SharedPointer<T>::operator=(
const SharedPointer<element_type>& other)
{
if (this == &other)
return *this;
if (count_)
{
(*count_)--;
if (*count_ == 0)
{
delete data_;
delete count_;
}
}
data_ = other.data_;
count_ = other.count_;
if (count_)
(*count_)++;
return *this;
}
template <typename T>
void SharedPointer<T>::reset(element_type* p)
{
if (data_ == p)
return;
if (count_)
{
(*count_)--;
if (*count_ == 0)
{
delete data_;
delete count_;
}
}
data_ = p;
count_ = p ? new long(1) : nullptr;
}
template <typename T>
SharedPointer<T>::SharedPointer(SharedPointer&& other)
: data_(other.data_)
, count_(other.count_)
{
other.data_ = nullptr;
other.count_ = nullptr;
}
template <typename T>
SharedPointer<T>& SharedPointer<T>::operator=(SharedPointer&& other) noexcept
{
if (this == &other)
return *this;
if (count_)
{
(*count_)--;
if (*count_ == 0)
{
delete data_;
delete count_;
}
}
data_ = other.data_;
count_ = other.count_;
other.data_ = nullptr;
other.count_ = nullptr;
return *this;
}
template <typename T>
typename SharedPointer<T>::element_type& SharedPointer<T>::operator*() const
{
return *data_;
}
template <typename T>
typename SharedPointer<T>::element_type* SharedPointer<T>::operator->() const
{
return data_;
}
template <typename T>
typename SharedPointer<T>::element_type* SharedPointer<T>::get() const
{
return data_;
}
template <typename T>
long SharedPointer<T>::use_count() const
{
return count_ ? *count_ : 0;
}
template <typename T>
template <typename U>
bool SharedPointer<T>::operator==(const SharedPointer<U>& rhs) const
{
return data_ == rhs.get();
}
template <typename T>
template <typename U>
bool SharedPointer<T>::operator!=(const SharedPointer<U>& rhs) const
{
return data_ != rhs.get();
}
template <typename T>
bool SharedPointer<T>::operator==(const element_type* p) const
{
return data_ == p;
}
template <typename T>
bool SharedPointer<T>::operator!=(const element_type* p) const
{
return data_ != p;
}
template <typename T>
SharedPointer<T>::operator bool() const
{
return data_ != nullptr;
}
template <typename T>
template <typename U>
bool SharedPointer<T>::is_a() const
{
return dynamic_cast<U*>(data_) != nullptr;
}
Stdin To File
stdin_to_file.hh
#pragma once
#include <fstream>
#include <iostream>
long int stdin_to_file(const std::string& filename);
stdin_to_file.cc
#include <fstream>
#include <iostream>
#include <string>
long int stdin_to_file(const std::string& filename)
{
std::ofstream file_out;
file_out.open(filename);
std::string token;
auto count = 0;
while (std::cin >> token)
{
file_out << token + "\n";
count++;
}
return count;
}
Vector Implements
point.hh
#pragma once
#include <ostream>
class Point
{
public:
Point(int x, int y) : x_{ x }, y_{ y } {}
Point(const Point& p) { x_ = p.x_; y_ = p.y_; }
Point& operator*=(int scalar);
Point operator*(int scalar) const;
friend std::ostream& operator<<(std::ostream& os, const Point& point);
private:
int x_;
int y_;
};
Point operator*(int scalar, const Point& point);
point.cc
#include "point.hh"
Point& Point::operator*=(int scalar)
{
x_ *= scalar;
y_ *= scalar;
return *this;
}
Point Point::operator*(int scalar) const
{
auto new_point = Point{ *this };
new_point *= scalar;
return new_point;
}
Point operator*(int scalar, const Point& point) { return point * scalar; }
std::ostream& operator<<(std::ostream& os, const Point& point)
{
os << "{ " << point.x_ << ", " << point.y_ << " }";
return os;
}
vector.hh
#pragma once
#include <concepts>
#include <iostream>
#include <vector>
template <typename T, typename U>
concept Mul = requires(T t, U u) {
{ t * u } -> std::same_as<T>;
{ u * t } -> std::same_as<T>;
{ t *= u } -> std::same_as<T&>;
};
template <typename T>
concept Summable = requires(T a, T b) {
{ a + b } -> std::same_as<T>;
{ a += b } -> std::same_as<T&>;
};
template <typename T>
class MyVector
{
public:
MyVector() = default;
MyVector(const std::vector<T>& v) : vec_{ v } {}
template <typename U>
requires Mul<T, U>
MyVector<T> operator*(const U& scalar)
{
std::vector<T> result;
for (const auto& elem : vec_)
result.push_back(elem * scalar);
return MyVector<T>{ result };
}
template <typename U>
requires Mul<T, U>
MyVector<T>& operator*=(const U& scalar)
{
std::vector<T> result;
for (const auto& elem : vec_)
result.push_back(elem * scalar);
vec_ = std::move(result);
return *this;
}
T reduce(T init) const requires Summable<T>
{
for (const auto& elem : vec_)
init = init + elem;
return init;
}
template <typename U>
friend std::ostream& operator<<(std::ostream& os, const MyVector<U>& vec);
private:
std::vector<T> vec_;
};
#include "vector.hxx"
vector.hxx
#pragma once
#include "vector.hh"
template <typename U>
std::ostream& operator<<(std::ostream& os, const MyVector<U>& vec)
{
for (const auto& elem : vec.vec_)
os << elem << " ";
os << std::endl;
return os;
}
War
soldier.hh
#pragma once
#include <string>
class Soldier
{
public:
Soldier() = default;
Soldier(int hp, int dmg);
~Soldier() = default;
void attack(Soldier& s);
void print_state() const;
virtual void scream() const = 0;
int hp;
int dmg;
};
soldier.cc
#include "soldier.hh"
#include <iostream>
Soldier::Soldier(int hp, int dmg)
{
Soldier::dmg = dmg;
Soldier::hp = hp;
}
void Soldier::attack(Soldier& s) { s.hp -= this->dmg; }
void Soldier::print_state() const
{
std::cout << "I have " << this->hp << " health points.\n";
}
knight.hh
#pragma once
#include "soldier.hh"
class Knight : public Soldier
{
public:
Knight();
void scream() const override;
};
knight.cc
#include "knight.hh"
#include <iostream>
Knight::Knight() : Soldier(20, 5) {}
void Knight::scream() const
{
std::cout << "Be quick or be dead!\n";
}
assassin.hh
#pragma once
#include "soldier.hh"
class Assassin : public Soldier
{
public:
Assassin();
void scream() const override;
};
assassin.cc
#include "assassin.hh"
#include <iostream>
Assassin::Assassin() : Soldier(10, 9) {}
void Assassin::scream() const
{
std::cout << "Out of the shadows!\n";
}
regiment.hh
#pragma once
#include <vector>
#include "soldier.hh"
class Regiment
{
public:
Regiment() = default;
void join_by(Regiment& r);
size_t count() const;
void add_soldier(Soldier* s);
void print_state() const;
void scream() const;
private:
std::vector<Soldier*> soldiers;
};
regiment.cc
#include "regiment.hh"
void Regiment::join_by(Regiment& r)
{
for (const auto& soldier : r.soldiers)
soldiers.push_back(soldier);
r.soldiers.clear();
}
size_t Regiment::count() const { return soldiers.size(); }
void Regiment::add_soldier(Soldier* s) { soldiers.push_back(s); }
void Regiment::print_state() const
{
for (const auto& soldier : soldiers)
soldier->print_state();
}
void Regiment::scream() const
{
for (const auto& soldier : soldiers)
soldier->scream();
}