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 protected:
136  Item const*
137  findByName(std::string const& name) const
138  {
139  auto const itr = names_.find(name);
140  if (itr == names_.end())
141  return nullptr;
142  return itr->second;
143  }
144 
154  Item const&
155  add(char const* name,
156  KeyType type,
158  std::initializer_list<SOElement> commonFields = {})
159  {
160  formats_.emplace_front(name, type, uniqueFields, commonFields);
161  Item const& item{formats_.front()};
162 
163  names_[name] = &item;
164  types_[type] = &item;
165 
166  return item;
167  }
168 
169 private:
170  // One of the situations where a std::forward_list is useful. We want to
171  // store each Item in a place where its address won't change. So a node-
172  // based container is appropriate. But we don't need searchability.
174 
175  boost::container::flat_map<std::string, Item const*> names_;
176  boost::container::flat_map<KeyType, Item const*> types_;
177 };
178 
179 } // namespace ripple
180 
181 #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:155
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:173
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:175
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:137
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:176
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