Skip to content

Instantly share code, notes, and snippets.

@jbandela
jbandela / DigestMatcher.cpp
Created November 28, 2012 21:29
Select a template class based on a string
// For motivation see stackoverflow
// http://stackoverflow.com/questions/13612832/template-for-referencing-classes-themselves-rather-than-a-class-that-uses-templa
#include <iostream>
#include <string>
#include <typeinfo>
#include <cstring>
@jbandela
jbandela / capturemymove1.cpp
Created December 4, 2012 21:29
Capture by Move Lambda - C++11
#include <utility>
#include <iostream>
#include <memory>
#include <type_traits>
#include <functional>
template<class T>
using moved_value = std::reference_wrapper<T>;
template<class T,class F>
@jbandela
jbandela / jrbcococ.hpp
Created December 8, 2012 02:26
Some C++ sugar for cocos2d-x
#ifndef JRB_COCOS_H
#include <stdexcept>
struct InitFailed:public std::exception{
InitFailed():std::exception("init Failed"){}
};
template<class T, class... Parms>
cocos2d::CCScene* createScene(Parms&&... p){
@jbandela
jbandela / property.hpp
Created December 11, 2012 16:50
Properties
#include <iostream>
using namespace std;
template<class Class, class Type, const Type& (Class::*Getter)(),void (Class::*Setter)(const Type&)>
class property{
Class* p_;
Class* calc_p(const property& other){
auto diff = reinterpret_cast<const char*>(&other) - reinterpret_cast<const char*>(other.p_);
return reinterpret_cast<Class*>(reinterpret_cast< char*>(this) - diff);
@jbandela
jbandela / cross_compiler_interface1.cpp
Created December 11, 2012 18:22
Version 1 Cross Compiler interfaces
#include <functional>
#include <iostream>
#include <assert.h>
#include <cstddef>
typedef void(*ptr_fun_void_t)();
struct vtable{
// Copyright John R. Bandela 2012.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstddef>
#include <string>
// On Windows use stdcall
#define CROSS_CALL_CALLING_CONVENTION __stdcall
#include <iostream>
#include <string>
// Reusable
template<int i, class T>
struct optional_type{
enum{ pos = i};
typedef T value_type;
// Adapted from http://stackoverflow.com/questions/12707030/sequence-range-unroll-using-variadic-templates
template<unsigned...> struct indices{};
template<unsigned N, unsigned... Indices>
struct indices_gen : indices_gen<N-1, N-1, Indices...>{};
template<unsigned... Indices>
struct indices_gen<1, Indices...>{
typedef indices<0, Indices...> type;
};
#include <iostream>
/* This will let macros expand before concating them */
#define CROSS_COMPILER_INTERFACE_PRIMITIVE_CAT(x, y) x ## y
#define CROSS_COMPILER_INTERFACE_CAT(x, y) CROSS_COMPILER_INTERFACE_PRIMITIVE_CAT(x, y)
// Adapted from http://stackoverflow.com/questions/11920577/casting-all-parameters-passed-in-macro-using-va-args
// With adaptations for MSVC from BOOST_PP
/* This counts the number of args */
@jbandela
jbandela / slist.hpp
Last active February 3, 2024 00:03
Atomic singly linked list
#include <atomic>
#include <memory>
#include <iterator>
#include <utility>
#include <assert.h>
//
// Lock free singly-linked using shared_ptr (if atomic_* for shared_ptr is lock_free)
// You can get the head, and push_front
// You can replace the head