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:
47  Item (char const* name,
48  KeyType type,
51  : soTemplate_ (uniqueFields, commonFields)
52  , name_ (name)
53  , type_ (type)
54  {
55  // Verify that KeyType is appropriate.
56  static_assert (
59  "KnownFormats KeyType must be integral or enum.");
60  }
61 
64  std::string const& getName () const
65  {
66  return name_;
67  }
68 
71  KeyType getType () const
72  {
73  return type_;
74  }
75 
76  SOTemplate const& getSOTemplate() const
77  {
78  return soTemplate_;
79  }
80 
81  private:
84  KeyType const type_;
85  };
86 
91  KnownFormats () = default;
92 
97  virtual ~KnownFormats () = default;
98  KnownFormats(KnownFormats const&) = delete;
99  KnownFormats& operator=(KnownFormats const&) = delete;
100 
108  KeyType findTypeByName (std::string const& name) const
109  {
110  Item const* const result = findByName (name);
111 
112  if (result != nullptr)
113  return result->getType ();
114  Throw<std::runtime_error> ("Unknown format name");
115  return {}; // Silence compiler warning.
116  }
117 
120  Item const* findByType (KeyType type) const
121  {
122  auto const itr = types_.find (type);
123  if (itr == types_.end())
124  return nullptr;
125  return itr->second;
126  }
127 
128 protected:
131  Item const* findByName (std::string const& name) const
132  {
133  auto const itr = names_.find (name);
134  if (itr == names_.end())
135  return nullptr;
136  return itr->second;
137  }
138 
148  Item const& add (char const* name, KeyType type,
150  std::initializer_list<SOElement> commonFields = {})
151  {
152  formats_.emplace_front (name, type, uniqueFields, commonFields);
153  Item const& item {formats_.front()};
154 
155  names_[name] = &item;
156  types_[type] = &item;
157 
158  return item;
159  }
160 
161 private:
162  // One of the situations where a std::forward_list is useful. We want to
163  // store each Item in a place where its address won't change. So a node-
164  // based container is appropriate. But we don't need searchability.
166 
167  boost::container::flat_map<std::string, Item const*> names_;
168  boost::container::flat_map<KeyType, Item const*> types_;
169 };
170 
171 } // ripple
172 
173 #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:148
ripple::KnownFormats::findTypeByName
KeyType findTypeByName(std::string const &name) const
Retrieve the type for a format specified by name.
Definition: KnownFormats.h:108
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:165
ripple::KnownFormats::Item::type_
const KeyType type_
Definition: KnownFormats.h:84
ripple::KnownFormats::operator=
KnownFormats & operator=(KnownFormats const &)=delete
ripple::KnownFormats::Item::soTemplate_
SOTemplate soTemplate_
Definition: KnownFormats.h:82
algorithm
ripple::KnownFormats::Item::name_
const std::string name_
Definition: KnownFormats.h:83
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:75
ripple::KnownFormats::names_
boost::container::flat_map< std::string, Item const * > names_
Definition: KnownFormats.h:167
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:120
ripple::KnownFormats::Item::getSOTemplate
SOTemplate const & getSOTemplate() const
Definition: KnownFormats.h:76
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:131
ripple::KnownFormats::Item::getType
KeyType getType() const
Retrieve the transaction type this format represents.
Definition: KnownFormats.h:71
ripple::KnownFormats::KnownFormats
KnownFormats()=default
Create the known formats object.
ripple::KnownFormats::types_
boost::container::flat_map< KeyType, Item const * > types_
Definition: KnownFormats.h:168
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