62template <
class T,
class K=std::
string>
63class Bank :
protected std::map<K, ClonerBase<T> *> {
77 typedef std::map<K, cloner_t *>
map_t;
92 static Cloner<T,D> & cloner = Static::get<Cloner<T,D>, bank_id>();
116 set(cloner.
src.getName(), cloner);
140 bool has(
const K & key)
const {
141 return (this->find(
resolve(key)) != this->end());
147 typename map_t::const_iterator it = this->find(
resolve(key));
148 if (it != this->end()){
149 return it->second->getCloned();
152 throw std::runtime_error(
resolve(key) +
'[' + key +
"]: no such entry");
158 const T &
get(
const K & key)
const {
159 typename map_t::const_iterator it = this->find(
resolve(key));
160 if (it != this->end()){
161 return it->second->getSource();
164 throw std::runtime_error(
resolve(key) +
'[' + key +
"]: no such entry");
171 typename map_t::iterator it = this->find(
resolve(key));
172 if (it != this->end()){
173 return it->second->getSource();
176 throw std::runtime_error(
resolve(key) +
'[' + key +
"]: no such entry");
182 typename map_t::const_iterator
get()
const {
184 for (
typename map_t::const_iterator it = this->begin(); it != this->end(); ++it){
185 if (it->second == &cloner)
188 throw std::runtime_error(
"find(): no such entry");
200 typename map_t::const_iterator it = this->find(
resolve(key));
201 if (it != this->end()){
202 return *(it->second);
205 throw std::runtime_error(key +
": no such entry");
232 const map_t & getMap()
const{
243 const std::string &
resolve(
const K & key)
const {
247 void toStream(std::ostream & ostr = std::cout)
const {
254 for (
const auto & entry: *this) {
255 ostr << entry.first <<
':' << entry.second->getSource() <<
'\n';
277 const typename map_t::iterator it = this->find(key);
279 if (it != this->end()){
281 if (it->second != &cloner){
282 std::cerr << __FILE__ <<
" warning: changing cloner source for: " << key <<
"\n";
287 std::cerr << __FILE__ <<
" warning: re-defining: " << key <<
"\n";
289 it->second = &cloner;
293 this->operator [](key) = &cloner;
304std::ostream & operator<<(std::ostream & ostr,
const Bank<T> & bank) {
321 typedef typename bank_t::bank_id bank_id;
329 std::cerr << __FUNCTION__ <<
": copy const" << std::endl;
336 D & add(
const std::string & key,
char brief=0){
337 static Cloner<T,D> & cloner = Static::get<Cloner<T,D>, bank_id>();
349 static Cloner<T,D> & cloner = Static::get<Cloner<T,D>, bank_id>();
365 D & addExternal(
const D & entry,
const std::string & key,
char brief = 0){
388 briefKeys.add(brief, value);
392 bool hasAlias(
const std::string & value)
const {
395 else if (value.length()==1)
396 return hasAlias(value.at(0));
402 bool hasAlias(
char brief)
const {
403 return briefKeys.hasKey(brief);
407 char getAlias(
const std::string & value)
const {
408 if (value.length() > 1)
409 return briefKeys.
getKey(value);
410 else if (value.length()==1){
412 char c = value.at(0);
413 if (briefKeys.hasKey(c))
433 const std::string &
resolve(
const key_t & value)
const {
435 if (value.length() == 1)
436 return briefKeys.
getValue(value.at(0));
454 FlagResolver::ivalue_t addSection(
const FlagResolver::dict_t::key_t &
title,
const FlagResolver::dict_t::value_t index=0){
492 typedef typename cloner_t::entry_t entry_t;
499 typedef std::map<cloner_t *, std::set<index_t> > book_t;
505 for (
typename book_t::value_type & v : this->book){
507 mout.
debug() <<
"Freeing: ";
508 for (index_t i : v.second){
519 cloner_t & cloner = bank.getCloner(key);
520 entry_t entry = cloner.getClonerEntry();
522 book[&cloner].insert(entry.first);
523 return *entry.second;
530 typename bank_t::map_t::const_iterator it = bank.template get<T>();
532 entry_t entry = cloner->getClonerEntry();
534 book[cloner].insert(entry.first);
535 return *entry.second;
545 index_t getIndex()
const{
573class BankOLD : public Registry<ClonerBase<T> > {
579 void add2(const std::string & key){
580 static drain::Cloner<T,D> cloner;
581 this->add(cloner, key);
585 T & clone(const std::string & key) const {
586 // return this->template Registry<ClonerBase<T> >::get(key).clone();
587 return this->Registry<ClonerBase<T> >::get(key).getCloned();
591 T & get(const std::string & key){
592 return this->Registry<ClonerBase<T> >::get(key).getSource();
593 //return this->Registry<ClonerBase<T> >::get(key).get();
A Bank with additional support for brief, single char keys.
Definition Bank.h:315
const std::string & getTitle() const
Get the name of this program (command set)
Definition Bank.h:450
virtual const std::string & resolve(const key_t &value) const
Given brief or long key, returns the long key .
Definition Bank.h:433
void setBriefKey(char brief, const std::string &value)
Now, D::getName() is required.
Definition Bank.h:387
void setTitle(const std::string &title)
Set name and brief description of a program, to appear in help dumps.
Definition Bank.h:444
std::string title
For example, name of the program, to appear in help dumps etc.
Definition Bank.h:468
D & add(char brief=0)
Add something that has getName()
Definition Bank.h:348
std::map< K, cloner_t * > map_t
Base class.
Definition Bank.h:77
bool has(const K &key) const
Check if a cloner is defined for this key.
Definition Bank.h:140
drain::ClonerBase< T > & set(const std::string &key, drain::ClonerBase< T > &cloner)
Definition Bank.h:275
K key_t
Public key type. (Key type used in the public interface.)
Definition Bank.h:68
map_t::const_iterator get() const
Returns a map entry: pair<Key,D *>()
Definition Bank.h:182
T & get(const K &key)
Returns the base instance.
Definition Bank.h:170
const T & get(const K &key) const
Returns the base instance.
Definition Bank.h:158
T & clone(const K &key) const
Return a copy of the base instance.
Definition Bank.h:146
D & addExternal(const D &entry, const K &key)
Adds class D using a copy constructor on an external instance.
Definition Bank.h:130
D & add(const K &key)
Adds class D as an internal instance.
Definition Bank.h:106
T value_t
Base type of cloned objects.
Definition Bank.h:71
cloner_t & getCloner(const K &key) const
Return the internal static entry.
Definition Bank.h:199
std::set< K > key_set_t
For enumerating keys.
Definition Bank.h:80
virtual const std::string & resolve(const K &key) const
Definition Bank.h:243
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:61
const K & getKey(const V &value, bool lenient=true) const
Identity mapping useful for type deduction of template arguments in functions.
Definition Dictionary.h:170
const V & getValue(const K &key, bool lenient=true) const
Given a key, return the first value associated with it.
Definition Dictionary.h:147
bool hasValue(const V &value) const
Given a key, return the first value associated with it.
Definition Dictionary.h:141
drain::Dictionary< key_t, ivalue_t > dict_t
"Recommended" dictionary type. All the methods are templates, however.
Definition FlagBase.h:64
static ivalue_t addEntry(drain::Dictionary< key_t, T > &dict, const key_t &key, ivalue_t i=0)
Add a new entry in the dictionary.
Creates an entries offered by a bank and destroys them upon exit.
Definition Bank.h:482
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:667
Definition DataSelector.cpp:1277
size_t index_t
Each cloned entry has an index.
Definition Cloner.h:52
Wrapper for derived class S, returning base class T.
Definition Cloner.h:118
S src
Default instance, also the source for cloning.
Definition Cloner.h:275