Skip to content

Instantly share code, notes, and snippets.

View martinmoene's full-sized avatar

Martin Moene martinmoene

View GitHub Profile
@martinmoene
martinmoene / gist:9410391
Created March 7, 2014 12:13
std::between() an idea by Niels Dekker
// std::between() an idea by Niels Dekker
#include <functional>
namespace std {
template<class T>
bool between(T const& val, T const& lo, T const& hi)
{
return between( val, lo, hi, std::less<T>() );
@martinmoene
martinmoene / app-state-machine.cpp
Last active November 5, 2022 09:26
C++17 example of user interface with state machine based on std::variant.
// C++17 example of user interface with state machine based on std::variant.
// The state machine engine is based on code by Mateusz Pusz,
// https://github.com/mpusz/fsm-variant (MIT-license)
// CppCon 2016. Ben Deane: Using Types Effectively,
// https://www.youtube.com/watch?v=ojZbFIQSdl8
//------------------------------------------------------------------------
// State machine engine:
@martinmoene
martinmoene / Understand-C++
Last active October 16, 2022 20:21
Myth 1: “To understand C++, you must first learn C” -- performance aspect
// https://isocpp.org/blog/2014/12/myths-1
//
// Bjarne Stroustrup.
//
// 2. Myth 1: “To understand C++, you must first learn C”
//
// No. Learning basic programming using C++ is far easier than with C.
//
// C is almost a subset of C++, but it is not the best subset to learn first
// because C lacks the notational support, the type safety, and the easier-to-use
@martinmoene
martinmoene / value-semantics-sean-parent.cpp
Created August 18, 2015 15:07
Code from talk: Inheritance Is The Base Class of Evil by Sean Parent at Going Native 2013
// Sean Parent. Inheritance Is The Base Class of Evil. Going Native 2013
// Video: https://www.youtube.com/watch?v=bIhUE5uUFOA
// Code : https://github.com/sean-parent/sean-parent.github.io/wiki/Papers-and-Presentations
/*
Copyright 2013 Adobe Systems Incorporated
Distributed under the MIT License (see license at
http://stlab.adobe.com/licenses.html)
This file is intended as example code and is not production quality.
/*
* ./format-base.h
*
* In the public domain
*
* Author: Martin Moene
*/
#ifndef FORMAT_BASE_H_INCLUDED
#define FORMAT_BASE_H_INCLUDED
@martinmoene
martinmoene / program.config.hpp
Last active December 14, 2020 06:09
Tweak-header, A New Approach to Build-Time Library Configuration, by Colby Pike aka vector-of-bool. Oct 4, 2020
// Tweak-header, A New Approach to Build-Time Library Configuration,
// by Colby Pike aka vector-of-bool. Oct 4, 2020
// https://vector-of-bool.github.io/2020/10/04/lib-configuration.html
#include <cstdlib>
#include <string>
namespace program { namespace config {
namespace defaults
@martinmoene
martinmoene / main.cpp
Last active June 7, 2019 08:16
Converting endianness (C++11) - inspired by https://github.com/MayaPosch/ByteBauble
#include "nonstd/endian.hpp"
#include <iostream>
#define print_to_big_endian(x) \
std::cout << #x << ": " << std::hex << to_num(x) << " to be: " << to_num(nonstd::to_big_endian(x)) << "\n"
#define print_to_little_endian(x) \
std::cout << #x << ": " << std::hex << to_num(x) << " to le: " << to_num(nonstd::to_little_endian(x)) << "\n"
#define print_to_native_endian(x) \
@martinmoene
martinmoene / Jinja2Cpp-ParseError.cpp
Last active April 29, 2019 11:42
Jinja2Cpp: ParseError and assignment/swap -- Note: nonstd::expected uses swap() in operator=( expected const & other ).
//
// If you write
// ParseError(ParseError&& other) noexcept(true) = default;
// the type won't be treated as nothrow move-constructable.
// But if you write this ctor manually - everything is ok.
#include <initializer_list>
#include <utility>
#include <vector>
@martinmoene
martinmoene / cmd.txt
Last active October 10, 2018 13:20
Compare number of lines in different Subversion revisions of files
# file1.cpp has code removed unintentionally:
> svn-diff-size.py --threshold -60 --histogram --revision 4710:4839 Folder/*.cpp
Folder/file1.cpp: (4317 -> 4136): -181
Folder/file2.cpp: (2323 -> 2145): -178
Folder/file2.cpp: (1031 -> 886): -145
Histogram delta line-count:
-200: 0
-180: 2 **
@martinmoene
martinmoene / non-library-option-example.h
Last active May 21, 2018 22:43
C++ option handling without dedicated library
using text = std::string;
using texts = std::vector<text>;
inline auto split_option( text arg ) -> std::tuple<text, text>
{
auto pos = arg.rfind( '=' );
return pos == text::npos
? std::make_tuple( arg, "" )
: std::make_tuple( arg.substr( 0, pos ), arg.substr( pos + 1 ) );