If I understand your question correctly, if you have 30 structures (tables) in your "database", you want to find all structures that have "empid" as a column (i.e. member of structure)? In C++ I'm not aware of a mechanism to test whether a structure has a particular member by name, at least not a name provided at runtime. You need to store this in some form of "meta data" - e.g. maps with string keys that lists a set of structures for example (or encoded in logic with string comparisons etc.)...
Here is a rough sketch...
A "table" class typed on the row type (i.e. structure)
class table
{
// some virtual methods common to all classes..
};
template <typename _Row>
class data_table : public table
{
// methods
// real data
std::vector<_Row> data;
};
then you need a database class to hold all the tables..
class database
{
// methods
std::map<int, boost::shared_ptr<table> > tables; // here int is for a unique id for table
};
Now you have a collection of tables, where each table is a set of rows (structures). You then need to define some metadata for the database, i.e. map columns to tables etc..
std::multimap<std::string, boost::shared_ptr<table> > columns;
This you have to populate using your knowledge of the structures, for example, this map may contain, for your code above:
columns["empid"] = id of table<emp>;
columns["empid"] = id of table<payroll>;
etc.
Now, you can pass a "query" to your "database" class, where you say something like, "empid" = 1, and then you lookup in the columns map and find all instances of tables, and then each table you can query with "empid" = 1. As to how to map "empid" to the real empid
field in the structure emp
, you have to embed that logic into methods in the emp
structure which table<emp>
can delegate to (or if you can think of a smart way of storing references to members... )
It's only a suggestion...
Or alternatively, consider using one of the many databases out there which will support real queries, and run either as standalone or embedded!
Edit:
struct emp
{
int empid;
string empname;
template <typename T>
bool matches(const string& field_name, const T& value)
{
if (field_name == "empid")
return empid == value;
else if (field_name == "empname")
return empname == value;
// etc
return false;
}
};
So the "metadata" is encoded in the logic for test, you pass in a string field name, and then depending on what it is, you compare the appropriate value... This isn't very efficient, but it works...