I want to design a data dashboard framework with c++ with the following requirements:
- clients can register data type to the dashboard using a key (string or integer are both acceptable)
- clients can store/access the object with the known data type and key
The following example code demonstrates how I want to use it
// data.hpp
struct DataA
{
DataA();
int a;
};
struct DataF
{
DataF();
float f;
};
// data.cpp
static DataA s_da;
static DataF s_df;
DataA::DataA()
{
if(!Dashboard::Register("A", s_da)) {
cerr << "unable to register DataA, key is used";
}
}
DataF::DataF()
{
if(!Dashboard::Register("F", s_df)) {
cerr << "unable to register DataF, key is used";
}
}
// main.cpp
int main ()
{
DataA da;
da.a = 123;
Dashboard::Set("A", da);
DataF df;
df.f = 3.14;
Dashboard::Set("F", df);
cout << ((DataA)Dashboard::Get("A")).a << endl; // 123
cout << ((DataF)Dashboard::Get("F")).f << endl; // 3.14
return 0;
}
However, I can't come up with any idea to implement the Dashboard
class to provide the interface.
How could I dynamically register an object with a given datatype and key? Is there any design pattern that addresses this requirement?
Aucun commentaire:
Enregistrer un commentaire