make keys accessible by rows/cols
[evolve-layout.git] / C / Layout.cpp
blobdff4824222db813c49488b59ed4ba954ae9c5300
1 #pragma once
3 #include <string>
4 #include <vector>
5 #include <iterator>
7 #include <glibmm/ustring.h>
8 class Layout {
9 typedef Glib::ustring string;
10 typedef std::vector<string::value_type> vector;
11 public:
12 typedef vector::iterator iterator;
13 typedef vector::reverse_iterator reverse_iterator;
15 Layout(const std::string &s = "") {
16 this->layout.reserve(12+11+10); // 33 keys
18 this->layout.clear();
19 this->add(s);
21 virtual ~Layout() {}
22 //Layout(const Layout &l) {}
23 //Layout &operator=(const Layout &l) {}
25 void add(const string &s) {
26 copy(s.begin(), s.end(), std::back_inserter(this->layout));
28 void add(const std::string &s) {
29 this->add(string(s));
31 void addLine(const std::string &s) {
32 this->add(s);
33 this->layout.push_back('\n');
36 void add(const string::value_type c) {
37 this->layout.push_back(c);
40 // TODO write a back_inserter
41 const std::string toString() const {
42 std::string s;
44 vector::const_iterator it = this->layout.begin();
45 vector::const_iterator end = this->layout.end();
46 vector::const_iterator block_end;
48 for(block_end = it+5; it != block_end && it != end; ++it) {
49 s.append(string(1, *it)); }
50 s.append(" ");
52 for(block_end = it+7; it != block_end && it != end; ++it) {
53 s.append(string(1, *it)); }
54 s.append("\n");
56 for(block_end = it+5; it != block_end && it != end; ++it) {
57 s.append(string(1, *it)); }
58 s.append(" ");
60 for(block_end = it+6; it != block_end && it != end; ++it) {
61 s.append(string(1, *it)); }
62 s.append("\n");
64 for(block_end = it+5; it != block_end && it != end; ++it) {
65 s.append(string(1, *it)); }
66 s.append(" ");
68 for(block_end = it+5; it != block_end && it != end; ++it) {
69 s.append(string(1, *it)); }
71 return s;
73 const string toUString() const {
74 string s;
75 for(vector::const_iterator it = this->layout.begin();
76 it != this->layout.end(); ++it) {
77 s.append(string(1, *it));
79 return s;
82 // random access
83 string::reference operator[](const int i) {
84 return this->layout[i];
86 // matrix access
87 string::reference operator()(const int row, const int col) {
88 switch(row) {
89 case 1:
90 return this->layout[12+col];
91 break;
92 case 2:
93 return this->layout[12+11+col];
94 break;
95 case 0:
96 default:
97 return this->layout[col];
101 // return an iterator on the vector
102 // iterates gunichars, need to be converted before sending to stream
103 iterator begin() {
104 return this->layout.begin();
106 iterator end() {
107 return this->layout.end();
109 reverse_iterator rbegin() {
110 return this->layout.rbegin();
112 reverse_iterator rend() {
113 return this->layout.rend();
115 private:
116 // a vector storing keys to allow random (write) access
117 // only stores a single char per position
118 vector layout;