Anyway In my 2D Engine I am developing I have made my own Data Table system, I should probably explain what it does. It is a template based class that creates a virtual table with 2 columns (DataLabel, DataValue), from there you can add rows of data, here is the code snippet, you are free to use this at your own will:
Code: Select all
template<class DataLabel, class DataValue>
class DataTable {
private:
const char* name;
std::map<DataLabel, DataValue> Reg;
public:
DataTable(const char* tablelabel);
void AddElement(DataLabel label, DataValue value);
void RemoveElement(DataLabel label);
void ChangeLabel(DataLabel oldlabel, DataLabel newlabel);
void ChangeValue(DataLabel label, DataValue newvalue);
const char* GetTableLabel();
DataLabel GetDataLabel(DataLabel label);
DataValue GetDataValue(DataLabel label);
bool DoesLabelExist(DataLabel label);
bool DoesValueExist(DataLabel label, DataValue value);
};
template <class DataLabel, class DataValue>
DataTable<DataLabel, DataValue>::DataTable(const char* tablelabel) {
name = tablelabel;
}
template<class DataLabel, class DataValue>
void DataTable<DataLabel, DataValue>::AddElement(DataLabel label, DataValue value) {
Reg[label] = value;
}
template<class DataLabel, class DataValue>
void DataTable<DataLabel, DataValue>::RemoveElement(DataLabel label) {
Reg.erase(label);
}
template<class DataLabel, class DataValue>
void DataTable<DataLabel, DataValue>::ChangeLabel(DataLabel oldlabel, DataLabel newlabel) {
if (Reg[oldlabel] && newlabel != oldlabel) {
Reg[newlabel] = Reg[oldlabel];
Reg.erase(oldlabel);
}
}
template<class DataLabel, class DataValue>
void DataTable<DataLabel, DataValue>::ChangeValue(DataLabel label, DataValue newvalue) {
if (Reg[label] && newvalue != Reg[label]) {
Reg[label] = newvalue;
}
}
template<class DataLabel, class DataValue>
DataLabel DataTable<DataLabel, DataValue>::GetDataLabel(DataLabel label) {
if (Reg[label]) {
return label;
}
return NULL;
}
template<class DataLabel, class DataValue>
const char* DataTable<DataLabel, DataValue>::GetTableLabel() {
return name;
}
template<class DataLabel, class DataValue>
DataValue DataTable<DataLabel, DataValue>::GetDataValue(DataLabel label) {
if (Reg[label]) {
return Reg[label];
}
return 0;
}
template<class DataLabel, class DataValue>
bool DataTable<DataLabel, DataValue>::DoesLabelExist(DataLabel label) {
if (Reg[label]) {
return true;
}
else {
return false;
}
}
template<class DataLabel, class DataValue>
bool DataTable<DataLabel, DataValue>::DoesValueExist(DataLabel label, DataValue value) {
if (Reg[label]) {
if (Reg[label] == value) {
return true;
} else {
return false;
}
} else {
return false;
}
}
Code: Select all
//Lets make a table that holds our friends names and ages
DataTable<const char*, int> MyFriends("Friends");
MyFriends.AddElement("Bob", 17);
MyFriends.AddElement("Fred", 12);
MyFriends.AddElement("Emma", 22);
//What if Fred had a birthday?
MyFriends.ChangeDataValue("Fred", MyTable.GetDataValue("Fred")+1));
//What if Emma changed her name to Bob? Lol
MyFriends.ChangeDataLabel("Emma", MyFriends.GetDataLabel("Bob"));
//Say we had a massive table and wanted to know if David existed?
if (MyFriends.DoesLabelExist("David")) == true {
cout << "David doesn't exist! OOOHHHH NOOEEESSS!" << endl;
}
Feedback Suggestions: Have I wrote the code keeping in mind good programming practices are have I just pooped a pile of words?