All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KeyValuesData.cxx
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Decode/DecoderTools/details/KeyValuesData.cxx
3  * @brief Simple parsed data format.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date May 9, 2021
6  * @see icaruscode/Decode/DecoderTools/details/KeyValuesData.h
7  */
8 
9 // ICARUS libraries
11 
12 // C++ standard libraries
13 #include <ostream>
14 #include <utility> // std::move(), std::as_const()
15 
16 
17 // -----------------------------------------------------------------------------
18 // --- icarus::KeyValuesData
19 // -----------------------------------------------------------------------------
20 auto icarus::KeyValuesData::makeItem(std::string key) -> Item& {
21  if (hasItem(key)) throw DuplicateKey{ std::move(key) };
22  return makeItemImpl(std::move(key));
23 } // icarus::KeyValuesData<>::makeItem()
24 
25 
26 // -----------------------------------------------------------------------------
27 auto icarus::KeyValuesData::makeOrFetchItem(std::string const& key) -> Item& {
28 
29  Item* item = findItem(key);
30  return item? *item: makeItemImpl(key);
31 
32 } // icarus::KeyValuesData<>::makeOrFetchItem()
33 
34 
35 // -----------------------------------------------------------------------------
37  (std::string const& key) const noexcept -> Item const*
38 {
39  for (auto const& item: fItems) if (key == item.key()) return &item;
40  return nullptr;
41 } // icarus::KeyValuesData<>::findItem() const
42 
43 
44 // -----------------------------------------------------------------------------
46  (std::string const& key) noexcept -> Item*
47 {
48  // no violations here: this is a non-const method, with the right to modify
49  // object data; and this avoids code duplication.
50  return const_cast<Item*>(std::as_const(*this).findItem(key));
51 } // icarus::KeyValuesData<>::findItem()
52 
53 
54 // -----------------------------------------------------------------------------
55 auto icarus::KeyValuesData::getItem(std::string const& key) const
56  -> Item const&
57 {
58  if (auto item = findItem(key); item) return *item;
59  throw ItemNotFound(key);
60 } // icarus::KeyValuesData<>::getItem()
61 
62 
63 // -----------------------------------------------------------------------------
65  (std::string const& key) const noexcept
66 {
67  return findItem(key);
68 } // icarus::KeyValuesData<>::hasItem()
69 
70 
71 // -----------------------------------------------------------------------------
73  { return fItems.empty(); }
74 
75 
76 // -----------------------------------------------------------------------------
77 std::size_t icarus::KeyValuesData::size() const noexcept
78  { return fItems.size(); }
79 
80 
81 // -----------------------------------------------------------------------------
82 auto icarus::KeyValuesData::makeItemImpl(std::string key) -> Item& {
83  fItems.emplace_back(std::move(key));
84  return fItems.back();
85 } // icarus::KeyValuesData<>::makeItemImpl()
86 
87 
88 // -----------------------------------------------------------------------------
89 std::ostream& icarus::operator<<
90  (std::ostream& out, KeyValuesData::Item const& item)
91 {
92  out << "'" << item.key() << "' (" << item.nValues() << ")";
93  if (!item.values().empty()) {
94  out << ':';
95  for (auto const& value: item.values()) out << " '" << value << '\'';
96  }
97  return out;
98 } // icarus::operator<< (KeyValuesData::Item)
99 
100 
101 // -----------------------------------------------------------------------------
102 std::ostream& icarus::operator<< (std::ostream& out, KeyValuesData const& data)
103 {
104  out << data.size() << " items";
105  if (!data.empty()) {
106  out << ':';
107  for (auto const& item: data.items()) out << "\n " << item;
108  } // if has data
109  return out << "\n";
110 } // icarus::operator<< (KeyValuesData)
111 
112 
113 // -----------------------------------------------------------------------------
bool hasItem(std::string const &key) const noexcept
Returns whether an item with the specified key is present.
Item * findItem(std::string const &key) noexcept
Returns the item with specified key, nullptr if none.
decltype(auto) items() const noexcept
Returns a forward-iterable list of references to items.
Item const & getItem(std::string const &key) const
Returns the item with specified key, throws std::out_of_range if none.
Item & makeItemImpl(std::string key)
Creates, registers and return a new item (assumed not to exist yet).
Representation of a single item of data: a key and several values.
std::ostream & operator<<(std::ostream &out, IntegerRanges< T, CheckGrowing > const &ranges)
Simple parsed data format.
Collection of items with key/values structure.
std::size_t nValues() const noexcept
Returns the number of values currently present.
std::vector< Item > fItems
Collection of data items.
std::size_t size() const noexcept
Returns the number of items in the data.
temporary value
Item & makeItem(std::string key)
Creates and registers a new item with the specified key.
Item & makeOrFetchItem(std::string const &key)
Creates or retrieves an item with the specified key.
bool empty() const noexcept
Returns whether there is no item in data.