rippled
KnownFormats.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_PROTOCOL_KNOWNFORMATS_H_INCLUDED
21 #define RIPPLE_PROTOCOL_KNOWNFORMATS_H_INCLUDED
22 
23 #include <ripple/basics/contract.h>
24 #include <ripple/protocol/SOTemplate.h>
25 #include <boost/container/flat_map.hpp>
26 #include <algorithm>
27 #include <forward_list>
28 
29 namespace ripple {
30 
38 template <class KeyType>
40 {
41 public:
44  class Item
45  {
46  public:
48  char const* name,
49  KeyType type,
52  : soTemplate_(uniqueFields, commonFields), name_(name), type_(type)
53  {
54  // Verify that KeyType is appropriate.
55  static_assert(
58  "KnownFormats KeyType must be integral or enum.");
59  }
60 
63  std::string const&
64  getName() const
65  {
66  return name_;
67  }
68 
71  KeyType
72  getType() const
73  {
74  return type_;
75  }
76 
77  SOTemplate const&
78  getSOTemplate() const
79  {
80  return soTemplate_;
81  }
82 
83  private:
86  KeyType const type_;
87  };
88 
93  KnownFormats() = default;
94 
99  virtual ~KnownFormats() = default;
100  KnownFormats(KnownFormats const&) = delete;
101  KnownFormats&
102  operator=(KnownFormats const&) = delete;
103 
111  KeyType
112  findTypeByName(std::string const& name) const
113  {
114  Item const* const result = findByName(name);
115 
116  if (result != nullptr)
117  return result->getType();
118  Throw<std::runtime_error>("Unknown format name");
119  return {}; // Silence compiler warning.
120  }
121 
124  Item const*
125  findByType(KeyType type) const
126  {
127  auto const itr = types_.find(type);
128  if (itr == types_.end())
129  return nullptr;
130  return itr->second;
131  }
132 
133  // begin() and end() are provided for testing purposes.
135  begin() const
136  {
137  return formats_.begin();
138  }
139 
141  end() const
142  {
143  return formats_.end();
144  }
145 
146 protected:
149  Item const*
150  findByName(std::string const& name) const
151  {
152  auto const itr = names_.find(name);
153  if (itr == names_.end())
154  return nullptr;
155  return itr->second;
156  }
157 
167  Item const&
168  add(char const* name,
169  KeyType type,
171  std::initializer_list<SOElement> commonFields = {})
172  {
173  formats_.emplace_front(name, type, uniqueFields, commonFields);
174  Item const& item{formats_.front()};
175 
176  names_[name] = &item;
177  types_[type] = &item;
178 
179  return item;
180  }
181 
182 private:
183  // One of the situations where a std::forward_list is useful. We want to
184  // store each Item in a place where its address won't change. So a node-
185  // based container is appropriate. But we don't need searchability.
187 
188  boost::container::flat_map<std::string, Item const*> names_;
189  boost::container::flat_map<KeyType, Item const*> types_;
190 };
191 
192 } // namespace ripple
193 
194 #endif
ripple::KnownFormats::add
Item const & add(char const *name, KeyType type, std::initializer_list< SOElement > uniqueFields, std::initializer_list< SOElement > commonFields={})
Add a new format.
Definition: KnownFormats.h:168
ripple::KnownFormats::findTypeByName
KeyType findTypeByName(std::string const &name) const
Retrieve the type for a format specified by name.
Definition: KnownFormats.h:112
std::string
STL class.
ripple::KnownFormats::Item::getName
std::string const & getName() const
Retrieve the name of the format.
Definition: KnownFormats.h:64
ripple::KnownFormats::formats_
std::forward_list< Item > formats_
Definition: KnownFormats.h:186
ripple::KnownFormats::Item::type_
const KeyType type_
Definition: KnownFormats.h:86
ripple::KnownFormats::operator=
KnownFormats & operator=(KnownFormats const &)=delete
ripple::KnownFormats::Item::soTemplate_
SOTemplate soTemplate_
Definition: KnownFormats.h:84
algorithm
ripple::KnownFormats::Item::name_
const std::string name_
Definition: KnownFormats.h:85
ripple::KnownFormats::Item
A known format.
Definition: KnownFormats.h:44
forward_list
std::is_enum
ripple::SOTemplate
Defines the fields and their attributes within a STObject.
Definition: SOTemplate.h:81
ripple::KnownFormats::names_
boost::container::flat_map< std::string, Item const * > names_
Definition: KnownFormats.h:188
ripple::KnownFormats
Manages a list of known formats.
Definition: KnownFormats.h:39
ripple::KnownFormats::findByType
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:125
ripple::KnownFormats::Item::getSOTemplate
SOTemplate const & getSOTemplate() const
Definition: KnownFormats.h:78
std::is_integral
ripple::KeyType
KeyType
Definition: KeyType.h:28
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::KnownFormats::~KnownFormats
virtual ~KnownFormats()=default
Destroy the known formats object.
ripple::KnownFormats::findByName
Item const * findByName(std::string const &name) const
Retrieve a format based on its name.
Definition: KnownFormats.h:150
ripple::KnownFormats::Item::getType
KeyType getType() const
Retrieve the transaction type this format represents.
Definition: KnownFormats.h:72
ripple::KnownFormats::KnownFormats
KnownFormats()=default
Create the known formats object.
ripple::KnownFormats::types_
boost::container::flat_map< KeyType, Item const * > types_
Definition: KnownFormats.h:189
ripple::KnownFormats::end
std::forward_list< Item >::const_iterator end() const
Definition: KnownFormats.h:141
ripple::KnownFormats::begin
std::forward_list< Item >::const_iterator begin() const
Definition: KnownFormats.h:135
ripple::KnownFormats::Item::Item
Item(char const *name, KeyType type, std::initializer_list< SOElement > uniqueFields, std::initializer_list< SOElement > commonFields)
Definition: KnownFormats.h:47
std::initializer_list