Beatmup
Beatmup::Listing::Block Class Reference

Set of key-value pairs. More...

#include <listing.h>

Public Member Functions

 Block ()
 
 Block (const std::map< std::string, std::string > &mapping)
 
void printOut (std::ostream &stream)
 Prints out the block to an output stream. More...
 
std::string operator[] (const std::string &key) const
 Retrieves a value by key. More...
 
bool has (const std::string &key) const
 Returns true if a value is defined for a specific key in the block. More...
 
template<typename T >
get (const std::string &key) const
 Returns a value by key casted to a given type. More...
 
template<typename T >
get (const std::string &key, const T defaultValue) const
 Returns a value by key casted to a given type. More...
 
template<typename T >
void set (const std::string &key, T value)
 Sets a value for a specific key. More...
 
int getLineNumber () const
 Returns line number the block starts at. More...
 
template<>
std::string get (const std::string &key) const
 
template<>
void set (const std::string &key, std::string value)
 
template<>
void set (const std::string &key, float value)
 
template<>
void set (const std::string &key, int value)
 

Protected Member Functions

 Block (int lineNumber)
 

Private Attributes

std::map< std::string, std::string > mapping
 
int lineNumber
 

Friends

class Parser
 

Detailed Description

Set of key-value pairs.

Definition at line 46 of file listing.h.

Constructor & Destructor Documentation

◆ Block() [1/3]

Beatmup::Listing::Block::Block ( int  lineNumber)
inlineprotected

Definition at line 54 of file listing.h.

◆ Block() [2/3]

Beatmup::Listing::Block::Block ( )
inline

Definition at line 56 of file listing.h.

56 : lineNumber(-1) {}

◆ Block() [3/3]

Beatmup::Listing::Block::Block ( const std::map< std::string, std::string > &  mapping)
inline

Definition at line 57 of file listing.h.

57 : mapping(mapping), lineNumber(-1) {}
std::map< std::string, std::string > mapping
Definition: listing.h:50

Member Function Documentation

◆ printOut()

void Listing::Block::printOut ( std::ostream &  stream)

Prints out the block to an output stream.

Parameters
[in,out]streamThe output stream

Definition at line 152 of file listing.cpp.

152  {
153  bool first = true;
154  for (auto& it : mapping) {
155  str << (first ? " - " : " ") << it.first << ": " << it.second << std::endl;
156  first = false;
157  }
158 }
JNIEnv jobject jstring str

◆ operator[]()

std::string Listing::Block::operator[] ( const std::string &  key) const

Retrieves a value by key.

If not found, an exception is thrown.

Parameters
[in]keyThe key

Definition at line 161 of file listing.cpp.

161  {
162  auto it = mapping.find(key);
163  if (it == mapping.cend())
164  if (lineNumber > 0) {
165  throw InvalidArgument("Key not found in a block at line " + std::to_string(lineNumber) + ": " + key);
166  }
167  else {
168  throw InvalidArgument("Key not found: " + key);
169  }
170  return it->second;
171 }
std::string to_string(Beatmup::NNets::ActivationFunction function)

◆ has()

bool Beatmup::Listing::Block::has ( const std::string &  key) const
inline

Returns true if a value is defined for a specific key in the block.

Parameters
[in]keyThe key

Definition at line 76 of file listing.h.

76 { return mapping.find(key) != mapping.cend(); }

◆ get() [1/3]

template<typename T >
T Beatmup::Listing::Block::get ( const std::string &  key) const
inline

Returns a value by key casted to a given type.

An exception is thrown if no value defined for the given key.

Template Parameters
TThe value type
Parameters
[in]keyThe key

◆ get() [2/3]

template<typename T >
T Beatmup::Listing::Block::get ( const std::string &  key,
const T  defaultValue 
) const
inline

Returns a value by key casted to a given type.

Template Parameters
TThe value type
Parameters
[in]keyThe key
[in]defaultValueDefault value returned if no value is defined for the key.

Definition at line 94 of file listing.h.

94  {
95  return has(key) ? get<T>(key) : defaultValue;
96  }
bool has(const std::string &key) const
Returns true if a value is defined for a specific key in the block.
Definition: listing.h:76

◆ set() [1/4]

template<typename T >
void Beatmup::Listing::Block::set ( const std::string &  key,
value 
)

Sets a value for a specific key.

Template Parameters
TThe value type
Parameters
[in]keyThe key
[in]valueThe new value

◆ getLineNumber()

int Beatmup::Listing::Block::getLineNumber ( ) const
inline

Returns line number the block starts at.

Definition at line 110 of file listing.h.

110 { return lineNumber; }

◆ get() [3/3]

bool Beatmup::Listing::Block::get ( const std::string &  key) const
inline

Definition at line 150 of file listing.h.

150  {
151  return (*this)[key];
152  }

◆ set() [2/4]

template<>
void Beatmup::Listing::Block::set ( const std::string &  key,
std::string  value 
)
inline

Definition at line 170 of file listing.h.

170  {
171  mapping[key] = value;
172  }

◆ set() [3/4]

template<>
void Beatmup::Listing::Block::set ( const std::string &  key,
float  value 
)
inline

Definition at line 175 of file listing.h.

175  {
176  mapping[key] = std::to_string(value);
177  }

◆ set() [4/4]

template<>
void Beatmup::Listing::Block::set ( const std::string &  key,
int  value 
)
inline

Definition at line 180 of file listing.h.

180  {
181  mapping[key] = std::to_string(value);
182  }

Friends And Related Function Documentation

◆ Parser

friend class Parser
friend

Definition at line 47 of file listing.h.

Member Data Documentation

◆ mapping

std::map<std::string, std::string> Beatmup::Listing::Block::mapping
private

Definition at line 50 of file listing.h.

◆ lineNumber

int Beatmup::Listing::Block::lineNumber
private

Definition at line 51 of file listing.h.


The documentation for this class was generated from the following files: