Skip to content

Instantly share code, notes, and snippets.

@jbandela
jbandela / set_iterators.cpp
Last active August 29, 2015 14:07
set_iterators
#include <boost/iterator/iterator_facade.hpp>
#include <boost/range/iterator_range.hpp>
#include <iterator>
#include <utility>
template<class Iter1, class Iter2,class Comp>
struct set_intersection_iterator:public boost::iterator_facade<set_intersection_iterator<Iter1,Iter2,Comp>,const typename std::iterator_traits<Iter1>::value_type,std::forward_iterator_tag>
{
Iter1 begin1_;
Iter1 end1_;
#include <tuple>
#include <utility>
#include <cstdint>
#include <stdexcept>
using std::size_t;
template<typename F, typename Tuple, size_t... I>
auto
apply_(F&& f, Tuple&& args, std::integer_sequence<size_t,I...>)
@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 / 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{
@jbandela
jbandela / factorial.cpp
Created December 15, 2015 16:27
Precomputed factorial
#include <numeric>
template<class T>
constexpr T cfact(unsigned f) {
return f?cfact<T>(f - 1)*f : 1;
}
template<class T>
constexpr T factorial_max_helper(T value_so_far, unsigned f) {
// 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;
};