Skip to content

Instantly share code, notes, and snippets.

@sbuller
Created March 1, 2012 20:35
Show Gist options
  • Save sbuller/1953060 to your computer and use it in GitHub Desktop.
Save sbuller/1953060 to your computer and use it in GitHub Desktop.
Type based view
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Draw {
public:
Draw(ostream &out):out(out){}
template <class T>
friend Draw& operator<< (Draw &out, T t);
private:
ostream &out;
};
template <class T>
Draw& operator<< (Draw &out, T t)
{
out.out <<t;
return out;
}
#pragma once
struct Input {};
struct Filter {};
struct Quantizer {};
struct Bandwidth {};
struct SampleRate {};
struct OversampleFilter {};
struct IIRFilter {};
struct InputMode {};
struct NotchDelay {};
struct BandwidthMode {};
struct Heartbeat {};
struct Volume {};
class Model {
public:
Input input;
Filter filter;
Quantizer quantizer;
Bandwidth bandwidth;
SampleRate sampleRate;
OversampleFilter oversampleFilter;
Volume volume;
IIRFilter iirFilter;
InputMode inputMode;
NotchDelay notchDelay;
BandwidthMode bandwidthMode;
Heartbeat heartbeat;
};
#include "model.h"
#include "draw.h"
template <int l, class T>
struct Line {
Line(T t):t(t) {}
T t;
};
Draw& operator<<(Draw &out, Line<0, Volume> l)
{
return out <<"V1";
}
Draw& operator<<(Draw &out, Line<1, Volume> l)
{
return out <<"V2";
}
Draw& operator<<(Draw &out, Line<2, Volume> l)
{
return out <<"V3";
}
Draw& operator<<(Draw &out, Line<0, Heartbeat> l)
{
return out <<"!";
}
Draw& operator<<(Draw &out, Line<1, Heartbeat> l)
{
return out <<"@";
}
Draw& operator<<(Draw &out, Line<2, Heartbeat> l)
{
return out <<"#";
}
Draw& operator<<(Draw &out, Line<3, Heartbeat> l)
{
return out <<"$";
}
Draw& operator<<(Draw &out, Input i)
{
return out <<"PC-BST";
}
Draw& operator<<(Draw &out, Filter f)
{
return out <<"SHR";
}
Draw& operator<<(Draw &out, Quantizer q)
{
return out <<"9bP";
}
Draw& operator<<(Draw &out, Bandwidth b)
{
return out <<"BST";
}
Draw& operator<<(Draw &out, SampleRate s)
{
return out <<"44099";
}
Draw& operator<<(Draw &out, OversampleFilter o)
{
return out <<"^";
}
Draw& operator<<(Draw &out, IIRFilter i)
{
return out <<"PCM";
}
Draw& operator<<(Draw &out, NotchDelay n)
{
return out <<"/64";
}
Draw& operator<<(Draw &out, BandwidthMode b)
{
return out <<"NOR";
}
Draw& operator<<(Draw &out, InputMode i)
{
return out <<"i2S";
}
Draw& operator<<(Draw &out, Model m)
{
out <<Line<0, Heartbeat>(m.heartbeat)
<<m.input
<<" " <<m.inputMode
<<" " <<m.oversampleFilter
<<m.sampleRate
<<"\n";
out <<Line<1, Heartbeat>(m.heartbeat)
<<"Fi." <<m.filter
<<" " <<m.iirFilter
<<" " <<Line<0,Volume>(m.volume)
<<"\n";
out <<Line<2,Heartbeat>(m.heartbeat)
<<"PL." <<m.bandwidth
<<" " <<m.bandwidthMode
<<" " <<Line<1,Volume>(m.volume)
<<"\n";
out <<Line<3,Heartbeat>(m.heartbeat)
<<"Qz." <<m.quantizer
<<" " <<m.notchDelay
<<" " <<Line<2,Volume>(m.volume)
<<"\n";
return out;
}
int main()
{
Draw out(cout);
Model m;
out <<m;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment