Created
December 11, 2012 16:52
-
-
Save bastih/4260227 to your computer and use it in GitHub Desktop.
concerns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <string> | |
#include <iostream> | |
enum class clt { | |
integer, | |
fp | |
}; | |
class ColumnMetadata { | |
const std::string _name; | |
const clt _ct; | |
public: | |
ColumnMetadata(std::string name, clt coltype) : _name(name), _ct(coltype) {} | |
const std::string& getName() const { return _name; } | |
}; | |
class AbstractMetadata { | |
public: | |
virtual ~AbstractMetadata() {} | |
virtual const std::vector<ColumnMetadata>& getMetadata() const = 0; | |
}; | |
class AbstractTable : public virtual AbstractMetadata { | |
public: | |
virtual ~AbstractTable() {} | |
}; | |
class ConcreteMetadata : public virtual AbstractMetadata { | |
private: | |
std::vector<ColumnMetadata> _metadata; | |
public: | |
const void setMetadata(std::vector<ColumnMetadata>&& meta) { | |
_metadata = std::move(meta); | |
} | |
const std::vector<ColumnMetadata>& getMetadata() const { | |
return _metadata; | |
} | |
}; | |
template<typename T> | |
class DerivedMetadata : public virtual AbstractMetadata { | |
public: | |
const std::vector<ColumnMetadata>& getMetadata() const { | |
return dynamic_cast<const T*>(this)->collectMetadata(); | |
} | |
}; | |
/* Concrete tables have metadata */ | |
class ConcreteTable : public virtual ConcreteMetadata, | |
public AbstractTable { | |
public: | |
ConcreteTable() {} | |
}; | |
/* concrete views are based on table but don't have their own metadata, | |
yet need to be able to tell you their underlying metadata, which is | |
computed by a call to collectMetadata */ | |
class ConcreteView : public virtual DerivedMetadata<ConcreteView>, | |
public AbstractTable { | |
private: | |
const AbstractTable& _table; | |
public: | |
explicit ConcreteView(const AbstractTable& table) : _table(table) {} | |
const std::vector<ColumnMetadata>& collectMetadata() const { | |
return _table.getMetadata(); | |
} | |
}; | |
int main (int argc, char const *argv[]) { | |
ConcreteTable ct; | |
ct.setMetadata({ {"test", clt::integer}, {"col2", clt::integer} }); | |
std::cout << ct.getMetadata()[0].getName() << std::endl; | |
ConcreteView cv(ct); | |
std::cout << cv.getMetadata()[1].getName() << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment