Skip to content

Instantly share code, notes, and snippets.

@yohhoy
yohhoy / endian.hpp
Last active October 28, 2021 10:16
endian conversion
// requires C++23 or later
#include <bit>
#include <concepts>
template <std::integral T>
constexpr T hton(T value) noexcept
{
if constexpr (std::endian::native == std::endian::little) {
return std::byteswap(value);
} else {
@yohhoy
yohhoy / spdlog-templ.cpp
Last active July 15, 2021 07:06
template code for spdlog library
#include <memory>
#include <vector>
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
int main(int argc, char* argv[])
{
@yohhoy
yohhoy / ticket_fair_mutex.cpp
Last active July 4, 2021 13:19
ticket-based fair mutex implementation
#include <condition_variable>
#include <mutex>
#include <thread>
class fair_mutex {
std::mutex mtx_;
std::condition_variable cv_;
unsigned int next_, curr_;
public:
@yohhoy
yohhoy / mock-string.cpp
Last active July 2, 2021 02:12
Mock class template
#include <initializer_list>
namespace mock {
template <class T>
struct allocator {};
template <class T>
struct char_traits {};
// C++20 https://timsong-cpp.github.io/cppwp/n4861/basic.string
template<class charT, class traits = char_traits<charT>,
#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <ranges>
int main() {
std::string_view str = "apple,banana,cinnamon";
for (auto rev_item: str | std::views::reverse | std::views::split(',')) {
static_assert( std::ranges::forward_range<decltype(rev_item)>);
prog.cc: In function 'int main()':
prog.cc:6:19: error: no match for 'operator|' (operand types are 'std::forward_list<int, std::allocator<int> >' and 'const std::ranges::views::_Reverse')
6 | for (int _: lst | std::views::reverse) {}
| ~~~ ^ ~~~~~~~~~~~~~~~~~~~
| | |
| | const std::ranges::views::_Reverse
| std::forward_list<int, std::allocator<int> >
In file included from prog.cc:2:
/opt/wandbox/gcc-head/include/c++/12.0.0/ranges:818:7: note: candidate: 'template<class _Lhs, class _Rhs> requires (derived_from<_Lhs, std::ranges::views::__adaptor::_RangeAdaptorClosure>) && (derived_from<_Rhs, std::ranges::views::__adaptor::_RangeAdaptorClosure>) constexpr auto std::ranges::views::__adaptor::operator|(_Lhs, _Rhs)'
818 | operator|(_Lhs __lhs, _Rhs __rhs)
@yohhoy
yohhoy / kill_ptree.cs
Created October 17, 2014 07:12
kill process tree(process group) on WIndows/C#
void KillProcessTree(System.Diagnostics.Process process)
{
string taskkill = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "taskkill.exe");
using (var procKiller = new System.Diagnostics.Process()) {
procKiller.StartInfo.FileName = taskkill;
procKiller.StartInfo.Arguments = string.Format("/PID {0} /T /F", process.Id);
procKiller.StartInfo.CreateNoWindow = true;
procKiller.StartInfo.UseShellExecute = false;
procKiller.Start();
procKiller.WaitForExit(1000); // wait 1sec
@yohhoy
yohhoy / utf8_util.hpp
Last active September 15, 2020 04:58
UTF-8 util
bool starts_with_utf8bom(std::string_view s)
{
constexpr char utf8_bom[3] = { 0xEF, 0xBB, 0xBF };
return s.length() >= 3 && std::equal(utf8_bom, utf8_bom + 3, s.begin());
}
std::string& erase_utf8bom(std::string& text)
{
if (starts_with_utf8bom(text)) {
text.erase(0, 3);
#include <type_traits>
int main() {
int x = 42;
static_assert(std::is_same_v<decltype( x ), int>);
static_assert(std::is_same_v<decltype((x)), int&>);
int (y) = 42; // redundant parentheses
static_assert(std::is_same_v<decltype( y ), int>);
static_assert(std::is_same_v<decltype((y)), int&>);
@yohhoy
yohhoy / po3ooronize.py
Last active May 11, 2020 08:43
C++ source text converter for PO3OOrO.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
C++ source text converter for PO3OOrO.
Copyright (C) 2016 yohhoy.
PO3OORO: Enhancing the C++ Basic Character Set with Standard Character Mappings
https://isocpp.org/files/papers/PO3OOrO.pdf
Special thanks to http://emojipedia.org/