Skip to content

Instantly share code, notes, and snippets.

@krdln
krdln / sstream.cc
Last active December 26, 2015 23:49
stringstream
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
string ss2s(ostream const& os) {
return static_cast<stringstream const&>(os).str();
}
int main() {
use std::rt::io::Writer;
use std::rt::io::stdio::with_task_stdout;
struct MagicWriter();
impl Writer for MagicWriter {
fn write(&mut self, buf: &[u8]) {
do with_task_stdout |o| { o.write(buf); }
}
fn flush(&mut self) {
do with_task_stdout |o| { o.flush(); }
@krdln
krdln / stdout.rs
Created October 29, 2013 23:55
stdout() and print
use std::rt::io::{stdout,Writer};
fn main() {
print("Hello");
stdout().write(" World".as_bytes());
println("!");
}
@krdln
krdln / k6.cc
Last active December 25, 2015 17:29
Blinking K6: example of AVR C++IO library.
#define F_CPU 12000000UL
#include <krdln/port.h>
int main() {
Output<A::Pin<0>> led;
Output<A::Pin<2>, false> err; // false: "on" level is 0V
Button<C::Pin<3>> but; // automagic pullup, yay!
int const T = 300;
@krdln
krdln / suffixes
Created October 16, 2013 17:42
Example of custom literal suffixes in c++11
#include <cstdio>
struct _Ms { int x; };
struct _Us { int x; };
constexpr _Ms operator "" ms(long long unsigned x) { return {(int)x}; }
constexpr _Us operator"" us(long long unsigned x) { return {(int)x}; }
void fun(_Ms x) { printf("%d milisekund\n", x.x); }
void fun(_Us x) { printf("%d mikrosekund\n", x.x); }