35 #include <drain/Log.h>
39 #include <drain/Sprinter.h>
63 template <
class T,
class K=std::
string>
64 class Bank :
protected std::map<K, ClonerBase<T> *> {
78 typedef std::map<K, cloner_t *>
map_t;
93 static Cloner<T,D> & cloner = Static::get<Cloner<T,D>, bank_id>();
117 set(cloner.
src.getName(), cloner);
141 bool has(
const K & key)
const {
142 return (this->find(
resolve(key)) != this->end());
148 typename map_t::const_iterator it = this->find(
resolve(key));
149 if (it != this->end()){
150 return it->second->getCloned();
153 throw std::runtime_error(
resolve(key) +
'[' + key +
"]: no such entry");
159 const T &
get(
const K & key)
const {
160 typename map_t::const_iterator it = this->find(
resolve(key));
161 if (it != this->end()){
162 return it->second->getSource();
165 throw std::runtime_error(
resolve(key) +
'[' + key +
"]: no such entry");
172 typename map_t::iterator it = this->find(
resolve(key));
173 if (it != this->end()){
174 return it->second->getSource();
177 throw std::runtime_error(
resolve(key) +
'[' + key +
"]: no such entry");
183 typename map_t::const_iterator
get()
const {
185 for (
typename map_t::const_iterator it = this->begin(); it != this->end(); ++it){
186 if (it->second == &cloner)
189 throw std::runtime_error(
"find(): no such entry");
201 typename map_t::const_iterator it = this->find(
resolve(key));
202 if (it != this->end()){
203 return *(it->second);
206 throw std::runtime_error(key +
": no such entry");
233 const map_t & getMap()
const{
242 const std::string &
resolve(
const K & key)
const {
246 void toStream(std::ostream & ostr = std::cout)
const {
253 for (
const auto & entry: *
this) {
254 ostr << entry.first <<
':' << entry.second->getSource() <<
'\n';
276 const typename map_t::iterator it = this->find(key);
278 if (it != this->end()){
280 if (it->second != &cloner){
281 std::cerr << __FILE__ <<
" warning: changing cloner source for: " << key <<
"\n";
286 std::cerr << __FILE__ <<
" warning: re-defining: " << key <<
"\n";
288 it->second = &cloner;
292 this->operator [](key) = &cloner;
303 std::ostream & operator<<(std::ostream & ostr,
const Bank<T> & bank) {
320 typedef typename bank_t::bank_id bank_id;
328 std::cerr << __FUNCTION__ <<
": copy const" << std::endl;
335 D & add(
const std::string & key,
char brief=0){
336 static Cloner<T,D> & cloner = Static::get<Cloner<T,D>, bank_id>();
348 static Cloner<T,D> & cloner = Static::get<Cloner<T,D>, bank_id>();
364 D & addExternal(
const D & entry,
const std::string & key,
char brief = 0){
387 briefKeys.add(brief, value);
391 bool hasAlias(
const std::string & value)
const {
394 else if (value.length()==1)
395 return hasAlias(value.at(0));
401 bool hasAlias(
char brief)
const {
402 return briefKeys.hasKey(brief);
406 char getAlias(
const std::string & value)
const {
407 if (value.length() > 1)
408 return briefKeys.
getKey(value);
409 else if (value.length()==1){
411 char c = value.at(0);
412 if (briefKeys.hasKey(c))
432 const std::string &
resolve(
const key_t & value)
const {
434 if (value.length() == 1)
435 return briefKeys.
getValue(value.at(0));
448 Flagger::ivalue_t addSection(
const FlagResolver::dict_t::key_t &
title,
const FlagResolver::dict_t::value_t index=0){
486 typedef typename cloner_t::entry_t entry_t;
493 typedef std::map<cloner_t *, std::set<index_t> > book_t;
499 for (
typename book_t::value_type & v : this->book){
501 mout.
debug() <<
"Freeing: ";
502 for (index_t i : v.second){
513 cloner_t & cloner = bank.getCloner(key);
514 entry_t entry = cloner.getClonerEntry();
516 book[&cloner].insert(entry.first);
517 return *entry.second;
524 typename bank_t::map_t::const_iterator it = bank.template get<T>();
526 entry_t entry = cloner->getClonerEntry();
528 book[cloner].insert(entry.first);
529 return *entry.second;
539 index_t getIndex()
const{
A Bank with additional support for brief, single char keys.
Definition: Bank.h:314
void setBriefKey(char brief, const std::string &value)
Now, D::getName() is required.
Definition: Bank.h:386
void setTitle(const std::string &title)
Set name and brief description of a program, to appear in help dumps.
Definition: Bank.h:443
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:347
virtual const std::string & resolve(const key_t &value) const
Given brief or long key, returns the long key .
Definition: Bank.h:432
std::map< K, cloner_t * > map_t
Base class.
Definition: Bank.h:78
bool has(const K &key) const
Check if a cloner is defined for this key.
Definition: Bank.h:141
D & add(const K &key)
Adds class D as an internal instance.
Definition: Bank.h:107
T & get(const K &key)
Returns the base instance.
Definition: Bank.h:171
cloner_t & getCloner(const K &key) const
Return the internal static entry.
Definition: Bank.h:200
virtual const std::string & resolve(const K &key) const
Definition: Bank.h:242
K key_t
Public key type. (Key type used in the public interface.)
Definition: Bank.h:69
map_t::const_iterator get() const
Returns a map entry: pair<Key,D *>()
Definition: Bank.h:183
drain::ClonerBase< T > & set(const std::string &key, drain::ClonerBase< T > &cloner)
Definition: Bank.h:274
const T & get(const K &key) const
Returns the base instance.
Definition: Bank.h:159
T & clone(const K &key) const
Return a copy of the base instance.
Definition: Bank.h:147
T value_t
Base type of cloned objects.
Definition: Bank.h:72
D & addExternal(const D &entry, const K &key)
Adds class D using a copy constructor on an external instance.
Definition: Bank.h:131
std::set< K > key_set_t
For enumerating keys.
Definition: Bank.h:81
Two-way mapping between strings and objects of template class T.
Definition: Dictionary.h:63
const V & getValue(const K &key) const
Given a key, return the first value associated with it.
Definition: Dictionary.h:149
const K & getKey(const V &value) const
Given a value, return the first key associated with it.
Definition: Dictionary.h:161
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: Flags.h:67
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:308
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:676
Creates an entry of desired type and destroys it upon exit.
Definition: Bank.h:476
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