FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
variant_list.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 #pragma once
17 #ifndef H_VARIANTLIST
18 #define H_VARIANTLIST
19 
20 #include "APITypes.h"
21 
22 namespace FB
23 {
25  // detail forward declarations
26 
27  namespace detail
28  {
29  class VariantListInserter;
30  }
31 
33  // declarations
34 
38  template<typename Cont>
39  inline FB::VariantList make_variant_list(const Cont& cont);
44  template<class InputIterator>
45  inline FB::VariantList make_variant_list(InputIterator begin, InputIterator end);
50  template<class InputIterator>
51  inline void make_variant_list(InputIterator begin, InputIterator end, FB::VariantList::iterator result);
52 
55  template<class Cont>
56  inline Cont convert_variant_list(const FB::VariantList& v);
60  template<class Cont>
61  inline void convert_variant_list(const FB::VariantList& from, Cont& to);
64  template<class Cont>
65  inline Cont convert_variant_list(FB::VariantList::const_iterator begin,
66  FB::VariantList::const_iterator end);
71  template<typename To, class OutputIterator>
72  inline void convert_variant_list(FB::VariantList::const_iterator begin,
73  FB::VariantList::const_iterator end,
74  OutputIterator result);
75 
84  inline FB::detail::VariantListInserter variant_list_of(FB::variant v);
88 
90  // detail definitions
91 
92  namespace detail
93  {
94  // helper for variant_list_of
95  class VariantListInserter
96  {
97  public:
98  VariantListInserter() : m_l() {}
99  VariantListInserter(FB::variant v) : m_l(1, v) {}
100  VariantListInserter& operator()(FB::variant v)
101  {
102  m_l.insert(m_l.end(), v);
103  return *this;
104  }
105  operator FB::VariantList& ()
106  {
107  return m_l;
108  }
109  operator FB::variant()
110  {
111  return FB::variant(m_l);
112  }
113  private:
114  FB::VariantList m_l;
115  };
116  }
117 
119  // definitions
120 
121  inline FB::detail::VariantListInserter
123  {
124  return FB::detail::VariantListInserter(v);
125  }
126 
127  inline FB::VariantList
129  {
130  return FB::VariantList();
131  }
132 
133  template<class InputIterator>
134  inline void make_variant_list(InputIterator begin,
135  InputIterator end,
136  FB::VariantList::iterator result)
137  {
138  while(begin != end)
139  *result++ = *begin++;
140  }
141 
142  template<class InputIterator>
143  inline FB::VariantList make_variant_list(InputIterator begin, InputIterator end)
144  {
145  FB::VariantList result(end-begin);
146  std::copy(begin, end, result.begin());
147  return result;
148  }
149 
150  template<class Cont>
151  inline FB::VariantList make_variant_list(const Cont& c)
152  {
153  FB::VariantList result;
154  std::copy(c.begin(), c.end(), std::back_inserter(result));
155  return result;
156  }
157 
158  template<typename To, class OutputIterator>
159  inline void convert_variant_list(FB::VariantList::const_iterator begin,
160  FB::VariantList::const_iterator end,
161  OutputIterator result)
162  {
163  while(begin != end){
164  *result++ = (begin++)->convert_cast<To>();
165  }
166  }
167 
168  template<class Cont>
169  inline Cont convert_variant_list(FB::VariantList::const_iterator begin,
170  FB::VariantList::const_iterator end)
171  {
172  Cont to;
173  convert_variant_list<typename Cont::value_type>(begin, end, std::back_inserter(to));
174  return to;
175  }
176 
177  template<class Cont>
178  inline Cont convert_variant_list(const FB::VariantList& from)
179  {
180  Cont to;
181  convert_variant_list<typename Cont::value_type>(from.begin(), from.end(), std::back_inserter(to));
182  return to;
183  }
184 
185  template<class Cont>
186  inline void convert_variant_list(const FB::VariantList& from, Cont& to)
187  {
188  convert_variant_list<typename Cont::value_type>(from.begin(), from.end(), std::back_inserter(to));
189  }
190 }
191 
192 #endif
Cont convert_variant_list(const FB::VariantList &v)
Convert a FB::VariantList to STL-style container (i.e. supports value_type and back-insert-iterators)...
Definition: variant_list.h:178
FB::detail::VariantListInserter variant_list_of(FB::variant v)
Allows convenient creation of an FB::VariantList.
Definition: variant_list.h:122
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
FB::VariantList make_variant_list(const Cont &cont)
Create a FB::VariantList from any STL-style container (i.e. exposes begin() and end()) ...
Definition: variant_list.h:151
std::vector< variant > VariantList
Defines an alias representing list of variants.
Definition: APITypes.h:64