FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
variant_map.h
1 /**********************************************************\
2 Original Author: Georg Fritzsche
3 
4 Created: Dec 13, 2009
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2009 Georg Fritzsche, Firebreath development team
13 \**********************************************************/
14 
15 #pragma once
16 #ifndef H_VARIANT_MAP
17 #define H_VARIANT_MAP
18 
19 #include "APITypes.h"
20 #include <iterator>
21 
22 namespace FB
23 {
25  // detail forward declarations
26 
27  namespace detail
28  {
29  template<typename T>
30  class VariantMapInserter;
31  }
32 
34  // declarations
35 
39  template<typename Dict>
40  inline FB::VariantMap make_variant_map(const Dict& dict);
45  template<class InputIterator>
46  inline FB::VariantMap make_variant_map(InputIterator begin, InputIterator end);
51  template<class InputIterator>
52  inline void make_variant_map(InputIterator begin, InputIterator end, FB::VariantMap::iterator result);
53 
56  template<class Dict>
57  inline Dict convert_variant_map(const FB::VariantMap& v);
61  template<class Dict>
62  inline void convert_variant_map(const FB::VariantMap& from, Dict& to);
65  template<class Dict>
66  inline Dict convert_variant_map(FB::VariantMap::const_iterator begin,
67  FB::VariantMap::const_iterator end);
72  template<typename To, class OutputIterator>
73  inline void convert_variant_map(FB::VariantMap::const_iterator begin,
74  FB::VariantMap::const_iterator end,
75  OutputIterator result);
76 
88  template<typename T>
89  FB::detail::VariantMapInserter<T> variant_map_of(const T& t, const FB::variant& v);
90 
93  template<typename T>
94  std::map<T,FB::variant> variant_map_of();
95 
97  // detail definitions
98 
99  namespace detail
100  {
101  // helper for variant_map_of
102  template<typename T>
103  class VariantMapInserter
104  {
105  public:
106  VariantMapInserter() : m_m() {}
107  VariantMapInserter(const T& t, const FB::variant& v) : m_m()
108  {
109  (*this)(t,v);
110  }
111  VariantMapInserter<T>& operator()(const T& t, const FB::variant& v)
112  {
113  m_m.insert(m_m.end(), std::make_pair(t,v));
114  return *this;
115  }
116  operator std::map<T,FB::variant> ()
117  {
118  return m_m;
119  }
120  operator FB::variant()
121  {
122  return FB::variant(m_m);
123  }
124  private:
125  std::map<T,FB::variant> m_m;
126  };
127  }
128 
130  // definitions
131 
132  template<typename T>
133  FB::detail::VariantMapInserter<T>
134  variant_map_of(const T& t, const FB::variant& v)
135  {
136  return FB::detail::VariantMapInserter<T>(t, v);
137  }
138 
139  template<typename T>
140  std::map<T,FB::variant>
142  {
143  return std::map<T, FB::variant>();
144  }
145 
146 
147  template<class InputIterator>
148  inline void make_variant_map(InputIterator begin,
149  InputIterator end,
150  FB::VariantMap::iterator result)
151  {
152  while(begin != end)
153  *result++ = *begin++;
154  }
155 
156  template<class InputIterator>
157  inline FB::VariantMap make_variant_map(InputIterator begin, InputIterator end)
158  {
159  FB::VariantMap result(end-begin);
160  std::copy(begin, end, result.begin());
161  return result;
162  }
163 
164  template<class Dict>
165  inline FB::VariantMap make_variant_map(const Dict& d)
166  {
167  FB::VariantMap result;
168  std::copy(d.begin(), d.end(), std::inserter(result, result.begin()));
169  return result;
170  }
171 
172  template<typename To, class OutputIterator>
173  inline void convert_variant_map(FB::VariantMap::const_iterator begin,
174  FB::VariantMap::const_iterator end,
175  OutputIterator result)
176  {
177  while(begin != end){
178  FB::VariantMap::const_iterator it = begin++;
179  *result++ = To(it->first, it->second.convert_cast<typename To::second_type>());
180  }
181  }
182 
183  template<class Dict>
184  inline Dict convert_variant_map(FB::VariantMap::const_iterator begin,
185  FB::VariantMap::const_iterator end)
186  {
187  Dict to;
188  convert_variant_map<typename Dict::value_type>(begin, end, std::inserter(to, to.begin()));
189  return to;
190  }
191 
192  template<class Dict>
193  inline Dict convert_variant_map(const FB::VariantMap& from)
194  {
195  Dict to;
196  convert_variant_map<typename Dict::value_type>(from.begin(), from.end(), std::inserter(to, to.begin()));
197  return to;
198  }
199 
200  template<class Dict>
201  inline void convert_variant_map(const FB::VariantMap& from, Dict& to)
202  {
203  convert_variant_map<typename Dict::value_type>(from.begin(), from.end(), std::inserter(to, to.begin()));
204  }
205 }
206 
207 #endif
208 
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
FB::VariantMap make_variant_map(const Dict &dict)
Create a FB::VariantMap from any STL-style dictionary (i.e. exposes begin() and end()) ...
Definition: variant_map.h:165
FB::detail::VariantMapInserter< T > variant_map_of(const T &t, const FB::variant &v)
Allows convenient creation of an FB::VariantMap.
Definition: variant_map.h:134
Dict convert_variant_map(const FB::VariantMap &v)
Convert a FB::VariantMap to STL-style dictionary (i.e. supports value_type and insert-iterators).
Definition: variant_map.h:193
std::map< std::string, variant > VariantMap
Defines an alias representing a string -> variant map.
Definition: APITypes.h:72