Hey guys,
With this defintion:
sss.hpp
#include <fstream>
#include <string>
#include <map>
namespace sss {
using namespace std;
class node {
public:
multimap<string, string> values;
multimap<string, node> children;
string &value(const string name, int index = 0);
node &child(const string name, int index = 0);
//...
};
typedef pair<string, string> value;
typedef pair<string, node> child;
typedef multimap<string, string>::iterator valueiterator;
typedef multimap<string, node>::iterator childiterator;
}
and this implementation:
sss.cpp
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include "sss.hpp"
using namespace std;
using sss::node;
using sss::valueiterator;
using sss::childiterator;
string& node::value(const string name,int index) {
valueiterator it = values.find(name);
try {
int i;
if (it == values.end()) throw 0;
for (i = 0; i<index; i++) {
it++;
if (it == values.end()) throw 0;
}
} catch (int e) {
it = values.insert(sss::value(name,string()));
}
return it->second;
}
node& node::child(const string name,int index) {
childiterator it = children.find(name);
try {
int i;
if (it == children.end()) throw 0;
for (i = 0; i<index; i++) {
it++;
if (it == children.end()) throw 0;
}
} catch (int e) {
it = children.insert(sss::child(name, node()));
}
return it->second;
}
How would I merge these two functions? They have so much it common! It seemed redundant... Is there a way with templates and/or with a function that has the shared code?
Thanks in advance,