Skip to content

Instantly share code, notes, and snippets.

@beached
beached / move_capture.cpp
Last active August 29, 2015 14:12
A wrapper to allow moving of values into a C++ 11 lambda
template<typename T>
class MoveCapture {
mutable T m_value;
public:
MoveCapture( ) = delete;
MoveCapture( T && val ) : m_value( std::move( val ) ) { }
MoveCapture( MoveCapture const & other ) : m_value( std::move( other.m_value ) ) { }
MoveCapture& operator=(MoveCapture const & rhs) {
@beached
beached / bare_asio_server.cpp
Last active June 16, 2024 14:31
A barebones async server with Boost ASIO
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <cstdint>
#include <iostream>
#include <list>
#include <memory>
struct Connection {
boost::asio::ip::tcp::socket socket;
boost::asio::streambuf read_buffer;
@beached
beached / rounding_fun.cpp
Last active August 29, 2015 14:12
Some useful little functions to round, ceil, and floor to the nearest N. A little rough, but they work
template<class T, class U>
T round_by( T const & value, U const & rnd_by ) {
static_assert(std::is_arithmetic<T>::value, "First template parameter must be an arithmetic type");
static_assert(std::is_floating_point<U>::value, "Second template parameter must be a floating point type");
const auto rnd = round( static_cast<U>(value) / rnd_by );
const auto ret = rnd*rnd_by;
return static_cast<T>(ret);
}
template<class T, class U>
@beached
beached / mixed_traits.cpp
Last active August 29, 2015 14:13
Succinct way of mixing several type traits to create new one
#include <type_traits>
template<typename T>
using is_numeric = std::integral_contstant<bool, std::is_integral<T>::value || std::is_floating_point<T>::value>;
template<typename T> using is_numeric_t = typename is_numeric<T>::type;
#1 GND -> 10 #2 TVS -> 3.3
#3 DBGCLK -> 18 #4 DBGDAT -> 17
#5 CSn -> 09 #6 SCLK -> 08
#7 RESETn -> 05 #8 MOSI -> 07
#9 3.3v -> 01 #10 MISO -> 06
# number is the cc debugger pin number
the -> number is the XRF pin number
Hole on odd side
@beached
beached / punycode.js
Created August 14, 2016 04:41 — forked from bnoordhuis/punycode.js
javascript punycode encoder and decoder
/**
* Copyright (C) 2011 by Ben Noordhuis <info@bnoordhuis.nl>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
char[] message = "Secret message!".toCharArray();
char[] userPassword = input("Enter your password: ");
//16 bytes twice PBKDF2 minimum salt recommendation
byte[] salt = SecureRandom.getBytes(16);
//Why did I use SHA512? see note at the bottom
//produce 64 bytes of secretmaterial from user password
byte[] secretMaterial = PBKDF2(userPassword, salt, 5000, "SHA512", 64);
// The MIT License (MIT)
//
// Copyright (c) 2018 Darrell Wright
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files( the "Software" ), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
using System;
using System.IO;
namespace CSVExport {
public class CSVExport {
private static string CsvEscape( object value ) {
if(value is null) {
return string.Empty;
}
string str = value.ToString( );
return @"""" + str.Replace( @"""", @"""""" ) + @"""";
@beached
beached / C++ normal operators.md
Last active April 15, 2024 02:33
A list of the normal signatures of C++ operators that allow overloading

C++ Operator Signatures

This is a list of C++ operators that can be overloaded and their normal signatures(a.k.a what an int would do). The order is the preffered order to use them(The first one listed is often preffered)

Arithmetic

operator+ addition

  • free function -> T operator+( T const & lhs, T const & rhs )
  • member function -> T operator+( T const & rhs ) const

operator+ unary plus

  • member function -> T operator+( ) const