Skip to content

Instantly share code, notes, and snippets.

View dirvine's full-sized avatar

David Irvine dirvine

View GitHub Profile
@dirvine
dirvine / Atomic
Last active August 29, 2015 14:05
monitor<T>
template<class T>
class Atomic {
private:
mutable T t;
mutable std::mutex m;
public:
Atomic( T t_ ) : t( t_ ) { }
template<typename F>
auto operator()( F f ) const -> decltype(f(t))
#==================================================================================================#
# #
# Copyright 2012 MaidSafe.net limited #
# #
# This MaidSafe Software is licensed to you under (1) the MaidSafe.net Commercial License, #
# version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which #
# licence you accepted on initial access to the Software (the "Licences"). #
# #
# By contributing code to the MaidSafe Software, or to this project generally, you agree to be #
# bound by the terms of the MaidSafe Contributor Agreement, version 1.0, found in the root #
@dirvine
dirvine / c++11_regular.h
Last active August 29, 2015 14:07
c++11_regular
// from Eric Niebler's talk from C++Now 2014 https://www.youtube.com/watch?v=zgOF4NrQllo
class RegularCxx11 {
RegularCxx11();
RegularCxx11(RegularCxx11 const &);
RegularCxx11(RegularCxx11 &&) noexcept;
~RegularCxx11();
RegularCxx11 & operator=(RegularCxx11 const &);
RegularCxx11 & operator=(RegularCxx11 &&) noexcept;
friend bool operator==(RegularCxx11 const &, RegularCxx11 const &);
friend bool operator!=(RegularCxx11 const &, RegularCxx11 const &);
@dirvine
dirvine / lrucache.h
Last active August 29, 2015 14:10
LRUcache / filter scratchpad
/*
A last recently used cache that has a capacity and time to live setting. Passing a void ValueType
allows this object to be used as a firewall / filter type device that can hold and check
for keys already seen. Users can set the capacity, time_to_live or both allowing a cache that will
not hold data too long or stay full if it not being accessed frequently. This should
allow the cache to not hold stale information at the cost of a check every time we add that looks
at the timestamp of the last entry in the list and compares this to the maps timestamp.
Research links
http://en.wikipedia.org/wiki/Cache_algorithms
@dirvine
dirvine / async_result.h
Created December 11, 2014 14:44
aync_result example
#include "maidsafe/common/test.h"
#include <iostream>
#include "asio/io_service.hpp"
#include "asio.hpp"
// Note: the handler signatures here must contain
// boost::system::error_code as first argument (not std::error_code)
// for asio to understand it is to indicate errors.
template <typename CompletionToken>
@dirvine
dirvine / crtp.cc
Last active August 29, 2015 14:14
example inherit/crtp forwarding ctr for derived from base
#include <iostream>
#include <string>
using namespace std;
template <typename Child>
struct Routing {
Routing() = delete;
explicit Routing(std::string s) : t(s) {}
void Get() { static_cast<Child*>(this)->FacadeGet(); }
@dirvine
dirvine / lib.rs
Last active August 29, 2015 14:16
dynamic dispatch - rust
/* Copyright 2014 MaidSafe.net limited
This MaidSafe Software is licensed to you under (1) the MaidSafe.net Commercial License,
version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
licence you accepted on initial access to the Software (the "Licences").
By contributing code to the MaidSafe Software, or to this project generally, you agree to be
bound by the terms of the MaidSafe Contributor Agreement, version 1.0, found in the root
directory of this project at LICENSE, COPYING and CONTRIBUTOR respectively and also
available at: http://www.maidsafe.net/licenses
Unless required by applicable law or agreed to in writing, the MaidSafe Software distributed
under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
@dirvine
dirvine / rust_examples.md
Last active August 29, 2015 14:17
playpen urls
use std::net::{IpAddr, SocketAddr, UdpSocket};
fn main() {
let local_addr = SocketAddr::new(IpAddr::new_v4(0, 0, 0, 0), 3671 );
let mut socket = match UdpSocket::bind(&local_addr) {
Ok(s) => s,
Err(e) => panic!("couldn't bind socket: {}", e),
};
match socket.join_multicast(&IpAddr::new_v4(224, 0, 23, 12)) {
Err(why) => println!("! {}", why),
@dirvine
dirvine / endian.cc
Created January 10, 2013 01:01
Endian calculation (unrestricted union)
union Endian {
char c;
int i;
Endian(void) : i(1) { };
bool big(void) { return !!c; };
} endian;
cerr << (endian.big() ? "big" : "little");