Skip to content

Instantly share code, notes, and snippets.

@bolry
bolry / CMakeLists.txt
Created April 15, 2018 08:32
Example of using copy and "generator" with CMake-files
cmake_minimum_required(VERSION 3.11)
project(LearningCMake)
set(Hmm "${CMAKE_CURRENT_BINARY_DIR}/hmm.cpp")
set(Foo "${CMAKE_CURRENT_BINARY_DIR}/foo.cpp")
set(Bar "${CMAKE_CURRENT_BINARY_DIR}/bar.cpp")
add_executable(LearningCMake ${Hmm} "${Foo}" "${Bar}")
add_custom_command(OUTPUT ${Hmm}
@bolry
bolry / print_enum.h
Created February 4, 2018 14:33
Print enum i C++11 and later
template<typename OStream, typename Enum>
OStream& print_enum(OStream& os, Enum const e)
{
static_assert(std::is_enum<Enum>::value, "not a enumerator");
return os << static_cast<typename std::underlying_type<Enum>::type>(e);
}
@bolry
bolry / __cxa_demangle_example.cpp
Created January 7, 2018 11:24
Example use of __cxa_demangle with no memory leaks
// Original example from https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html
#include <cmath>
#include <cstdlib>
#include <array>
#include <exception>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <cxxabi.h>
@bolry
bolry / check_compiler_typedefs.c
Created December 30, 2017 11:08
Let compiler check you typedefs
typedef struct {
char int8_err [-1 + 2 * (sizeof(int8_t) == 1)];
char uint8_err [-1 + 2 * (sizeof(uint8_t) == 1)];
char int16_err [-1 + 2 * (sizeof(int16_t) == 2)];
char uint16_err[-1 + 2 * (sizeof(uint16_t) == 2)];
char int32_err [-1 + 2 * (sizeof(int32_t) == 4)];
char uint32_err[-1 + 2 * (sizeof(uint32_t) == 4)];
} throwaway_t;
@bolry
bolry / cmakefile.txt
Created September 10, 2017 10:19
CMake stuff
Example of settings to CMake I use
Overrides RelWithDebugInfo from '-O2' to '-Og'
Also sets TYPE to 'Debug'
And example of setting the Eclipse executable path.
alias ncmake='CXX=g++ CC=gcc cmake -DCMAKE_C_FLAGS_RELWITHDEBINFO="-Og -g -DNDEBUG" -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-Og -g -DNDEBUG" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_ECLIPSE_MAKE_ARGUMENTS=-v -DCMAKE_ECLIPSE_EXECUTABLE=/home/bo/eclipse/cpp-mars/eclipse/eclipse -DCMAKE_ECLIPSE_VERSION:STRING="4.5 (Mars)" -G "Eclipse CDT4 - Ninja"'
@bolry
bolry / capturestream.cpp
Created May 22, 2017 12:11
Temporary Capture stream
class CaptureStream {
private:
std::basic_ios<char>& m_stream;
std::streambuf* m_orig_streambuf;
std::stringstream m_buffer;
CaptureStream(CaptureStream const&) = delete;
CaptureStream& operator=(CaptureStream const&) = delete;
public:
CaptureStream(std::basic_ios<char>& stream) :
@bolry
bolry / kkloepper_fav.cpp
Last active June 14, 2022 18:21
C++11 Stopwatch
class Stopwatch {
using clock = std::chrono::high_resolution_clock;
bool is_running() const { return stop_time_ == clock::time_point::min(); }
clock::time_point end_time() const { return is_running() ? clock::now() : stop_time_; }
clock::time_point begin_time_{clock::now()}, stop_time_{clock::time_point::min()};
public:
void stop() { if (is_running()) stop_time_ = clock::now(); }
clock::duration elapsed() const { return end_time() - begin_time_; }
};
@bolry
bolry / hsutter_fav3lines.cpp
Created January 8, 2017 22:17
Meyers singelton, does destruction!
widget& instance() {
static widget w;
return w;
}
@bolry
bolry / hsutter_fav.cpp
Last active January 8, 2017 22:14
A complete reference-counted object cache
shared_ptr<widget> get_widget(int id) {
static map<int, weak_ptr<widget>> cache;
static mutex m;
lock_guard<mutex> hold(m);
auto sp = cache[id].lock();
if (!sp) cache[id] = sp = load_widget(id);
return sp;
}
@bolry
bolry / main.cpp
Last active November 3, 2016 20:53
C++ main implementation
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <new>
#define CATCH_UNHANDLED 0
#define TOOL_NAME "<your tool name here>"
char const KToolName[] = TOOL_NAME;