OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022

#include <KvStore.hpp>

Public Member Functions

 KvStore ()=default
 
 KvStore (const std::string &file)
 
 KvStore (const std::string &file, read_callback callback)
 
bool load (const std::string &file)
 
bool load (std::istream &in)
 
bool load_line (const std::string &line)
 
group_iterator group_begin () const
 
group_iterator group_end () const
 
iterator begin ()
 
iterator end ()
 
const_iterator cbegin () const
 
const_iterator cend () const
 
group_filtered_iterator begin (const std::string &group) const
 
group_filtered_iterator end (const std::string &group) const
 
std::stringoperator[] (const std::string &k)
 
std::stringoperator[] (std::string &&k)
 
std::stringat (const std::string &k)
 
std::stringat (std::string &&k)
 
KvStore::iterator find (const std::string &k)
 
KvStore::iterator find (std::string &&k)
 
KvStore::const_iterator find (std::string &k) const
 
KvStore::const_iterator find (std::string &&k) const
 
size_t count (const std::string &k) const
 
size_t count (std::string &&k) const
 
bool command (std::string &&cmd, std::string &&args)
 
bool command_group (std::string &&group)
 
bool command_include (std::string &&file)
 
bool command_include_local (std::string &&file)
 
void group_separator (const std::string &s)
 
const std::stringgroup_separator ()
 
void callback (read_callback callback)
 
read_callback callback ()
 
void switch_group (const std::string &group)
 

Friends

std::ostream & operator<< (std::ostream &os, const KvStore &kv)
 

Detailed Description

Key-value file reader (commonly called par files)

Format

A simple file with various quoting and no commands (test1.par):

A key-value file with groups and another key-value file loaded first (test2.par):

Examples

Creating a simple map out of key-value files:

Walking though a key-value file as it is being read:

Definition at line 35 of file KvStore.hpp.

Constructor & Destructor Documentation

◆ KvStore() [1/3]

KvStore ( )
default

Default constructor, use load(std::string file) to read files.

◆ KvStore() [2/3]

KvStore ( const std::string file)
explicit

Construct a key-value map, loading the given file upon creation.

Parameters
filePath to key-value store to read
Exceptions
not::sureif any lines read are invalid

◆ KvStore() [3/3]

KvStore ( const std::string file,
read_callback  callback 
)

Construct a key-value map, with default file and processing callback.

Parameters
filePath to key-value store to read
callbackFunction to call for each value read
Exceptions
not::sureif any lines read are invalid

Member Function Documentation

◆ at() [1/2]

Returns a reference to the mapped value of the element identified with key k.

◆ at() [2/2]

std::string& at ( std::string &&  k)

Returns a reference to the mapped value of the element identified with key k.

◆ begin() [1/2]

iterator begin ( )

std::unordered_map iterator to recurse key-values

Returns
Start iterator

◆ begin() [2/2]

group_filtered_iterator begin ( const std::string group) const

focs::FilterIterator iterator to recurse key-values of the input group

Returns
Start iterator

◆ callback() [1/2]

read_callback callback ( )
inline

Get the key-value callback.

Returns
the current key-value callback

Definition at line 263 of file KvStore.hpp.

◆ callback() [2/2]

void callback ( read_callback  callback)
inline

Set a function to be called for each key-value read.

Parameters
callbackNew callback

Definition at line 257 of file KvStore.hpp.

◆ cbegin()

const_iterator cbegin ( ) const

std::unordered_map iterator to recurse key-values

Returns
Start iterator

◆ cend()

const_iterator cend ( ) const

std::unordered_map end iterator to recurse key-values

Returns
End iterator

◆ command()

bool command ( std::string &&  cmd,
std::string &&  args 
)

Process a key-value store command by name.

Parameters
cmdCommand name to run
argsArguments to pass to command function
Returns
true on error, false on success

◆ command_group()

bool command_group ( std::string &&  group)

Change the current key group.

Parameters
groupNew group name
Returns
true on error, false on success

◆ command_include()

bool command_include ( std::string &&  file)

Include another key-value store, relative to current file, into the global group.

Parameters
filefile relative to current key-value file path
Returns
true on error, false on success

◆ command_include_local()

bool command_include_local ( std::string &&  file)

Include another key-value store, relative to current file, into the current group.

Parameters
filefile relative to current key-value file path
Returns
true on error, false on success

◆ count() [1/2]

size_t count ( const std::string k) const

Count elements with a specific key.

Searches the container for an element with k as key and returns an iterator to it if found, otherwise it returns an iterator to unordered_map::end (the element past the end of the container).

Parameters
kKey to look up
Returns
1 if an element with a key equivalent to k is found, or zero otherwise.

◆ count() [2/2]

size_t count ( std::string &&  k) const

◆ end() [1/2]

iterator end ( )

std::unordered_map end iterator to recurse key-values

Returns
End iterator

◆ end() [2/2]

group_filtered_iterator end ( const std::string group) const

focs::FilterIterator end iterator to recurse key-values of the input group

Returns
End iterator

◆ find() [1/4]

KvStore::iterator find ( const std::string k)

Get iterator to element.

Searches the container for an element with k as key and returns an iterator to it if found, otherwise it returns an iterator to unordered_map::end (the element past the end of the container).

Parameters
kKey to look up
Returns
An iterator to the element, if the specified key value is found, or unordered_map::end if the specified key is not found in the container.

◆ find() [2/4]

KvStore::iterator find ( std::string &&  k)

Get iterator to element.

Searches the container for an element with k as key and returns an iterator to it if found, otherwise it returns an iterator to unordered_map::end (the element past the end of the container).

Parameters
kKey to look up
Returns
An iterator to the element, if the specified key value is found, or unordered_map::end if the specified key is not found in the container.

◆ find() [3/4]

KvStore::const_iterator find ( std::string &&  k) const

Get iterator to element.

Searches the container for an element with k as key and returns an iterator to it if found, otherwise it returns an iterator to unordered_map::end (the element past the end of the container).

Parameters
kKey to look up
Returns
An iterator to the element, if the specified key value is found, or unordered_map::end if the specified key is not found in the container.

◆ find() [4/4]

KvStore::const_iterator find ( std::string k) const

Get iterator to element.

Searches the container for an element with k as key and returns an iterator to it if found, otherwise it returns an iterator to unordered_map::end (the element past the end of the container).

Parameters
kKey to look up
Returns
An iterator to the element, if the specified key value is found, or unordered_map::end if the specified key is not found in the container.

◆ group_begin()

group_iterator group_begin ( ) const

std::vector iterator to recurse group names

Returns
Start iterator

◆ group_end()

group_iterator group_end ( ) const

std::vector end iterator to recurse group names

Returns
End iterator

◆ group_separator() [1/2]

const std::string& group_separator ( )
inline

Get the current group separator.

Returns
the current group separator

Definition at line 250 of file KvStore.hpp.

◆ group_separator() [2/2]

void group_separator ( const std::string s)
inline

Set the character(s) to use between group and key when inserting into map.

Parameters
sNew separator

Definition at line 244 of file KvStore.hpp.

◆ load() [1/2]

bool load ( const std::string file)

Read key-value file, appending values to current map.

Parameters
filePath to key-value store to read
Exceptions
not::sureif any lines read are invalid
Returns
true on failure, false on success

◆ load() [2/2]

bool load ( std::istream &  in)

Read key-values from input stream, appending values to current map.

Parameters
inInput stream containing lines of key-value definitions
Exceptions
not::sureif any lines read are invalid
Returns
true on failure, false on success

◆ load_line()

bool load_line ( const std::string line)

Process key-value line, appending value to current map.

Parameters
lineSingle key-value line to process
Exceptions
not::sureif any lines read are invalid
Returns
true on failure, false on success

◆ operator[]() [1/2]

std::string& operator[] ( const std::string k)

If k matches the key of an element in the KvStore, the function returns a reference to its mapped value.

If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).

Parameters
kKey to look up
Returns
A reference to the mapped value

◆ operator[]() [2/2]

std::string& operator[] ( std::string &&  k)

If k matches the key of an element in the KvStore, the function returns a reference to its mapped value.

If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).

Parameters
kKey to look up
Returns
A reference to the mapped value

◆ switch_group()

void switch_group ( const std::string group)

Switch default group for retrieving values.

Parameters
groupGroup to use by default

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const KvStore kv 
)
friend

Output KvStore to stream operator.

Parameters
osTarget output stream
kvKvStore to output
Returns
Output stream, for chaining

The documentation for this class was generated from the following file:
  • /gfs-oceanweb/web/ocssw/ocssw_src/include/focs/KvStore.hpp