Skip to content

Instantly share code, notes, and snippets.

View marcobergamin's full-sized avatar

Marco Bergamin marcobergamin

View GitHub Profile
@marcobergamin
marcobergamin / bugprone-assert-side-effect-example.cpp
Created May 7, 2022 11:06
An example of the clang-tidy check: bugprone-assert-side-effect
#include <cstdlib>
#ifndef NDEBUG
#define my_assert(x) { x ? static_cast<void>(x) : std::abort(); }
#else
#define my_assert(x) static_cast<void>(x)
#endif
class Foo {
public:
@marcobergamin
marcobergamin / gtest_freertos_main.cpp
Last active November 30, 2023 05:29
GTest main for FreeRTOS
#include "FreeRTOS.h"
extern "C" {
#include "task.h"
}
#include <atomic>
#include <cstdint>
#include <gtest/gtest.h>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <utility>
template <typename IN_TYPE, typename OUT_TYPE>
std::function<OUT_TYPE(IN_TYPE)> memoize(OUT_TYPE(*f)(IN_TYPE))
{
std::unordered_map<IN_TYPE, OUT_TYPE> storage;
return [f, storage](IN_TYPE x) mutable -> OUT_TYPE
@marcobergamin
marcobergamin / circularbuffer.h
Last active August 3, 2019 23:26
Circular buffer with random access const_iterator
#include <iterator>
template<typename T, typename SIZE_TYPE = size_t>
class CircularBuffer
{
public:
CircularBuffer(SIZE_TYPE capacity)
: _capacityPlusOne{ capacity + 1 }
, _buffer{ new T[_capacityPlusOne] }
, _head{ 0 }
@marcobergamin
marcobergamin / gist:69e92857a47e4e2984da19459793ab56
Last active July 2, 2019 22:32
std::enable_shared_from_this + factory method
#include <iostream>
#include <memory>
using namespace std;
class Widget : public std::enable_shared_from_this<Widget>
{
public:
template<typename... Ts>
static std::shared_ptr<Widget> Create(Ts&&... params)