Skip to content

Instantly share code, notes, and snippets.

View oliora's full-sized avatar

Andrey Upadyshev oliora

View GitHub Profile
@oliora
oliora / safe_advance.h
Last active September 26, 2019 02:38
Implementation of safe std::advance, std::next and std::prev similar to proposed N4317 paper "New Safer Functions to Advance Iterators"
// N4317 "New Safer Functions to Advance Iterators" paper can be found at
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4317.pdf
namespace detail {
template<class InputIter>
inline void do_advance(InputIter& it, InputIter end, typename std::iterator_traits<InputIter>::difference_type n, std::input_iterator_tag)
{
assert(n >= 0);
for (; (n > 0) && (it != end); --n)
++it;
@oliora
oliora / extra_formatters.py
Last active February 5, 2019 16:49
Extra LLVM formatters
"""
Extra LLDB Formatters
Contains formatters for:
- `llvm::SmallVector` - https://llvm.org/doxygen/classllvm_1_1SmallVector.html
- `boost::container::flat_map` - https://www.boost.org/doc/libs/1_69_0/doc/html/boost/container/flat_map.html
- `boost::optional` - https://www.boost.org/doc/libs/1_69_0/libs/optional/doc/html/index.html
- `mpark::variant` - https://github.com/mpark/variant
Load into LLDB with `command script import /path/to/extra-formatters.py`
@oliora
oliora / make_handle.h
Last active May 18, 2018 06:26
make_handle
// version 1.0
template<class T, class D>
inline auto make_handle(T* p, D&& d) noexcept(std::is_nothrow_constructible<typename std::decay<D>::type, D&&>::value)
-> std::unique_ptr<T, typename std::decay<D>::type>
{
return {p, std::forward<D>(d)};
}
auto h1 = make_handle(fopen("test", "r"), fclose);
auto h2 = make_handle(dlopen("bla"), dlclose);
@oliora
oliora / narrow.h
Last active May 17, 2018 16:40
Function fallback
std::string wstr_to_utf8(boost::wstring_view);
namespace detail {
template<class T, class = void>
struct narrow_impl
{
template<class U>
static U&& invoke(U&& s) noexcept
{
static_assert(std::is_same<T, U&&>::value, "error");
@oliora
oliora / pretty-json.sh
Last active August 1, 2018 10:05
one-liner to prettify json
alias pretty-json="python -c 'import json; import sys; json.dump(json.load(open(sys.argv[1], \"r\") if len(sys.argv) > 1 else sys.stdin), sys.stdout, indent=2)'"
@oliora
oliora / forward_ref_ctor_assignment.cpp
Created November 16, 2017 09:43
It's a bit tricky to get constructor and assignment operator accepting forward reference (singular or variadic) right
#include <type_traits>
#include <utility>
struct Bar {};
// Note about `is_convertible<T&&, Foo>`. `T&&` is essential there!
struct Foo
{
template<class T, class = typename std::enable_if<!std::is_convertible<T&&, Foo>::value>::type>
Foo(T&& t) {}
@oliora
oliora / delegating_deleter.h
Last active May 31, 2017 22:59
Delegating deleter
/////////////////////////////////////////////
// parser.h
#include <memory>
template<class T>
struct delegating_delete {
inline void operator() (T *p) const noexcept { do_delete(p); }
};
namespace parser {
@oliora
oliora / constexpr_assert.h
Last active May 24, 2022 03:01
constexpr_assert
// A compilation of the following posts:
// http://stackoverflow.com/questions/18648069/g-doesnt-compile-constexpr-function-with-assert-in-it
// http://ericniebler.com/2014/09/27/assert-and-constexpr-in-cxx11/
#include <cassert>
#include <utility>
template<class Assert>
inline void constexpr_assert_failed(Assert&& a) noexcept { std::forward<Assert>(a)(); }
// When evaluated at compile time emits a compilation error if condition is not true.
@oliora
oliora / perf_timer.h
Created September 26, 2016 08:14
Performance timer in pure C++11
#include <chrono>
#include <type_traits>
// Floating seconds duration
using fseconds = std::chrono::duration<double>;
class PerfTimer
{
public:
using clock = std::conditional<
@oliora
oliora / map_insert_test.cpp
Last active September 26, 2016 08:16
Test of performance bug with some std::map and std::unordered_map implementations
// Test of performance bug with some std::map and std::unordered_map implementations
// see http://www.forwardscattering.org/post/37 for details.
//
// Written by Andrey Upadyshev https://github.com/oliora/ based on article's test code
#include <unordered_map>
#include <map>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <type_traits>