Skip to content

Instantly share code, notes, and snippets.

@alexeiz
alexeiz / interfaceptr.cpp
Last active December 21, 2015 21:39
interface_ptr
#include "interfaceptr.hpp"
#include <cassert>
struct Interface1 {};
struct Interface2 {};
struct Interface3 {};
struct Class1 : Interface1, Interface2 {};
struct Class2 : Interface2, Interface3 {};
@alexeiz
alexeiz / scope_exit.cpp
Last active December 21, 2015 01:09
Simple "scope exit" macro for C++03, which supports arbitrary actions on scope exit.
#include <iostream>
using namespace std;
#define JOIN(a, b) JOIN_(a, b)
#define JOIN_(a, b) a ## b
#define ARG_COUNT(...) ARG_COUNT_IMP(__VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#define ARG_COUNT_IMP( _1,_2,_3,_4,_5,_6,_7,_8,_9, N, ...) N
@alexeiz
alexeiz / check_badweakptr.cpp
Created July 25, 2013 03:21
Determine the existence of a class (bad_weak_ptr) in a namespace (std).
#include <memory>
#include <exception>
#include <iostream>
namespace inject
{
struct bad_weak_ptr : std::exception
{
virtual char const * what() const _GLIBCXX_USE_NOEXCEPT
{
@alexeiz
alexeiz / null_stream.cpp
Created May 5, 2013 04:37
Redirect cout to a null sink using boost::iostreams.
#include <boost/iostreams/stream_buffer.hpp>
#include <boost/iostreams/device/null.hpp>
#include <iostream>
namespace io = boost::iostreams;
int main()
{
io::stream_buffer<io::null_sink> null_buf{io::null_sink()};
std::cout.rdbuf(&null_buf);
@alexeiz
alexeiz / gcharts.html
Created March 20, 2013 06:00
An example of using Google Chart Tools from CoffeeScript. This is a self-contained html page that displays a table and a column chart. It uses coffeescript code to prepare and display the data. The code is translated into js on the fly in the browser.
<html>
<head>
<title>CoffeeScript charts</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.load("visualization", "1", {packages:["table"]});
</script>
@alexeiz
alexeiz / htop-solarized-patch.diff
Created January 28, 2013 17:19
Patch for the default color scheme for htop that works well with the solarized terminal color theme.
diff -u -r a/CRT.c b/CRT.c
--- a/CRT.c 2012-10-19 14:59:28.000000000 -0400
+++ b/CRT.c 2013-01-28 12:08:36.812929000 -0500
@@ -562,7 +562,7 @@
CRT_colors[LED_COLOR] = ColorPair(Green,Black);
CRT_colors[TASKS_RUNNING] = A_BOLD | ColorPair(Green,Black);
CRT_colors[PROCESS] = A_NORMAL;
- CRT_colors[PROCESS_SHADOW] = A_BOLD | ColorPair(Black,Black);
+ CRT_colors[PROCESS_SHADOW] = A_BOLD | ColorPair(Green,Black);
CRT_colors[PROCESS_TAG] = A_BOLD | ColorPair(Yellow,Black);
@alexeiz
alexeiz / iscallable.cpp
Created December 18, 2012 16:15
Test if a type is a callable, i.e. objects of that type can be called as functions. Also shows how to do type tests with decltype instead of sizeof.
#include <type_traits>
template<typename F, typename ...Ts>
class is_callable
{
template<typename G, typename ...Us>
static
decltype(
typename std::add_pointer<decltype(std::declval<G>()(std::declval<Us>()...))>::type{},
std::true_type{})
@alexeiz
alexeiz / overloadset.cpp
Created November 5, 2012 07:16
Lambda overload set
#include <iostream>
template <typename ...Funcs>
struct overload_set;
template <typename Head, typename ...Tail>
struct overload_set<Head, Tail...> : Head, overload_set<Tail...>
{
overload_set(Head head, Tail... tail)
: Head{head}
@alexeiz
alexeiz / output.txt
Created October 26, 2012 21:53
Analysis of passing function parameters by-value vs by-reference in C++
testing: by value
* lvalue
lvalue: cctor
lvalue: mctor
* xvalue
xvalue: mctor
xvalue: mctor
* rvalue
rvalue: mctor
* done
@alexeiz
alexeiz / thr-singleton.cpp
Created August 16, 2012 16:39
Thread-safe one-time initialization in C++11
// C++11 has thread-safe function local statics, but this code is just
// to illustrate how one would do a thread-safe one-time initialization
// using atomic variables.
#include <atomic>
#include <thread>
#include <cassert>
#include <vector>
using namespace std;