39#include <drain/Sprinter.h>
64template <
class T,
class K=std::
string>
65class Bank :
protected std::map<K, ClonerBase<T> *> {
79 typedef std::map<K, cloner_t *>
map_t;
94 static Cloner<T,D> & cloner = Static::get<Cloner<T,D>, bank_id>();
118 set(cloner.
src.getName(), cloner);
142 bool has(
const K & key)
const {
143 return (this->find(
resolve(key)) != this->end());
149 typename map_t::const_iterator it = this->find(
resolve(key));
150 if (it != this->end()){
151 return it->second->getCloned();
154 throw std::runtime_error(
resolve(key) +
'[' + key +
"]: no such entry");
160 const T &
get(
const K & key)
const {
161 typename map_t::const_iterator it = this->find(
resolve(key));
162 if (it != this->end()){
163 return it->second->getSource();
166 throw std::runtime_error(
resolve(key) +
'[' + key +
"]: no such entry");
173 typename map_t::iterator it = this->find(
resolve(key));
174 if (it != this->end()){
175 return it->second->getSource();
178 throw std::runtime_error(
resolve(key) +
'[' + key +
"]: no such entry");
184 typename map_t::const_iterator
get()
const {
186 for (
typename map_t::const_iterator it = this->begin(); it != this->end(); ++it){
187 if (it->second == &cloner)
190 throw std::runtime_error(
"find(): no such entry");
202 typename map_t::const_iterator it = this->find(
resolve(key));
203 if (it != this->end()){
204 return *(it->second);
207 throw std::runtime_error(key +
": no such entry");
234 const map_t & getMap()
const{
245 const std::string &
resolve(
const K & key)
const {
249 void toStream(std::ostream & ostr = std::cout)
const {
256 for (
const auto & entry: *this) {
257 ostr << entry.first <<
':' << entry.second->getSource() <<
'\n';
279 const typename map_t::iterator it = this->find(key);
281 if (it != this->end()){
283 if (it->second != &cloner){
284 std::cerr << __FILE__ <<
" warning: changing cloner source for: " << key <<
"\n";
289 std::cerr << __FILE__ <<
" warning: re-defining: " << key <<
"\n";
291 it->second = &cloner;
295 this->operator [](key) = &cloner;
306std::ostream & operator<<(std::ostream & ostr,
const Bank<T> & bank) {
323 typedef typename bank_t::bank_id bank_id;
331 std::cerr << __FUNCTION__ <<
": copy const" << std::endl;
338 D & add(
const std::string & key,
char brief=0){
339 static Cloner<T,D> & cloner = Static::get<Cloner<T,D>, bank_id>();
351 static Cloner<T,D> & cloner = Static::get<Cloner<T,D>, bank_id>();
367 D & addExternal(
const D & entry,
const std::string & key,
char brief = 0){
390 briefKeys.add(brief, value);
394 bool hasAlias(
const std::string & value)
const {
397 else if (value.length()==1)
398 return hasAlias(value.at(0));
404 bool hasAlias(
char brief)
const {
405 return briefKeys.hasKey(brief);
409 char getAlias(
const std::string & value)
const {
410 if (value.length() > 1)
411 return briefKeys.
getKey(value);
412 else if (value.length()==1){
414 char c = value.at(0);
415 if (briefKeys.hasKey(c))
435 const std::string &
resolve(
const key_t & value)
const {
437 if (value.length() == 1)
438 return briefKeys.
getValue(value.at(0));
451 FlagResolver::ivalue_t addSection(
const FlagResolver::dict_t::key_t &
title,
const FlagResolver::dict_t::value_t index=0){
489 typedef typename cloner_t::entry_t entry_t;
496 typedef std::map<cloner_t *, std::set<index_t> > book_t;
502 for (
typename book_t::value_type & v : this->book){
504 mout.
debug() <<
"Freeing: ";
505 for (index_t i : v.second){
516 cloner_t & cloner = bank.getCloner(key);
517 entry_t entry = cloner.getClonerEntry();
519 book[&cloner].insert(entry.first);
520 return *entry.second;
527 typename bank_t::map_t::const_iterator it = bank.template get<T>();
529 entry_t entry = cloner->getClonerEntry();
531 book[cloner].insert(entry.first);
532 return *entry.second;
542 index_t getIndex()
const{
570class BankOLD : public Registry<ClonerBase<T> > {
576 void add2(const std::string & key){
577 static drain::Cloner<T,D> cloner;
578 this->add(cloner, key);
582 T & clone(const std::string & key) const {
583 // return this->template Registry<ClonerBase<T> >::get(key).clone();
584 return this->Registry<ClonerBase<T> >::get(key).getCloned();
588 T & get(const std::string & key){
589 return this->Registry<ClonerBase<T> >::get(key).getSource();
590 //return this->Registry<ClonerBase<T> >::get(key).get();
A Bank with additional support for brief, single char keys.
Definition Bank.h:317
virtual const std::string & resolve(const key_t &value) const
Given brief or long key, returns the long key .
Definition Bank.h:435
void setBriefKey(char brief, const std::string &value)
Now, D::getName() is required.
Definition Bank.h:389
void setTitle(const std::string &title)
Set name and brief description of a program, to appear in help dumps.
Definition Bank.h:446
std::string title
For example, name of the program, to appear in help dumps etc.
Definition Bank.h:471
D & add(char brief=0)
Add something that has getName()
Definition Bank.h:350
std::map< K, cloner_t * > map_t
Base class.
Definition Bank.h:79
bool has(const K &key) const
Check if a cloner is defined for this key.
Definition Bank.h:142
drain::ClonerBase< T > & set(const std::string &key, drain::ClonerBase< T > &cloner)
Definition Bank.h:277
K key_t
Public key type. (Key type used in the public interface.)
Definition Bank.h:70
map_t::const_iterator get() const
Returns a map entry: pair<Key,D *>()
Definition Bank.h:184
T & get(const K &key)
Returns the base instance.
Definition Bank.h:172
const T & get(const K &key) const
Returns the base instance.
Definition Bank.h:160
T & clone(const K &key) const
Return a copy of the base instance.
Definition Bank.h:148
D & addExternal(const D &entry, const K &key)
Adds class D using a copy constructor on an external instance.
Definition Bank.h:132
D & add(const K &key)
Adds class D as an internal instance.
Definition Bank.h:108
T value_t
Base type of cloned objects.
Definition Bank.h:73
cloner_t & getCloner(const K &key) const
Return the internal static entry.
Definition Bank.h:201
std::set< K > key_set_t
For enumerating keys.
Definition Bank.h:82
virtual const std::string & resolve(const K &key) const
Definition Bank.h:245
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:63
const K & getKey(const V &value, bool lenient=true) const
Identity mapping useful for type deduction of template arguments in functions.
Definition Dictionary.h:172
const V & getValue(const K &key, bool lenient=true) const
Given a key, return the first value associated with it.
Definition Dictionary.h:149
bool hasValue(const V &value) const
Given a key, return the first value associated with it.
Definition Dictionary.h:143
drain::Dictionary< key_t, ivalue_t > dict_t
"Recommended" dictionary type. All the methods are templates, however.
Definition FlagBase.h:68
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.
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:666
Creates an entry of desired type and destroys it upon exit.
Definition Bank.h:479
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:117
S src
Default instance, also the source for cloning.
Definition Cloner.h:274