coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parameter_list_base.h
Go to the documentation of this file.
1 #ifndef PARAMETER_LIST_BASE_H
2 #define PARAMETER_LIST_BASE_H
3 #pragma once
4 /***************************************************************************
5  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
6  * All rights reserved. *
7  * *
8  * Redistribution and use in source and binary forms, with or without *
9  * modification, are permitted provided that the following conditions *
10  * are met: *
11  * *
12  * Redistributions of source code must retain the above copyright *
13  * notice, this list of conditions and the following disclaimer. *
14  * *
15  * Redistributions in binary form must reproduce the above copyright *
16  * notice, this list of conditions and the following disclaimer in the *
17  * documentation and/or other materials provided with the distribution. *
18  * *
19  * Neither the name of Mike Kinghan nor the names of its contributors *
20  * may be used to endorse or promote products derived from this software *
21  * without specific prior written permission. *
22  * *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
30  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
33  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
34  * DAMAGE. *
35  * *
36  **************************************************************************/
37 #include "chew.h"
38 #include <string>
39 #include <vector>
40 #include <memory>
41 #include <stdexcept>
42 
47 // Forward decl
48 struct symbol;
49 
50 namespace innards {
51 
57 
65  explicit parameter_list_base(size_t n = 0);
66 
74  template<class CharSeq>
76  : _defect(none),_variadic(false) {
77  read(chew);
78  }
79 
82  : _params(
83  other._params ?
84  new std::vector<std::string>(*other._params) : nullptr),
85  _defect(other._defect),
86  _variadic(other._variadic){}
87 
89  void swap(parameter_list_base & other) {
90  std::swap(_params,other._params);
91  std::swap(_defect,other._defect);
92  std::swap(_variadic,other._variadic);
93  }
94 
97  swap(other);
98  return *this;
99  }
100 
102  bool operator==(parameter_list_base const & other) const {
103  return str() == other.str();
104  }
105 
107  bool operator!=(parameter_list_base const & other) const {
108  return !(*this == other);
109  }
110 
112  bool well_formed() const {
113  return _defect == none;
114  }
115 
117  bool variadic() const {
118  return _variadic;
119  }
120 
126  size_t size() const {
127  return _params ? _params->size() : 0;
128  }
129 
131  bool null() const {
132  return !_params;
133  }
134 
136  operator bool () const {
137  return !null();
138  }
139 
141  std::string str() const;
142 
143 
145 
150  std::vector<std::string>::iterator begin() {
151  using namespace std;
152  return null() ? vector<string>::iterator() : _params->begin();
153  }
154 
155  std::vector<std::string>::const_iterator begin() const {
156  using namespace std;
157  return null() ? vector<string>::const_iterator() : _params->begin();
158  }
160 
162 
167  std::vector<std::string>::iterator end() {
168  using namespace std;
169  return null() ? vector<string>::iterator() : _params->end();
170  }
171 
172  std::vector<std::string>::const_iterator end() const {
173  using namespace std;
174  return null() ? vector<string>::const_iterator() : _params->end();
175  }
177 
179 
185  std::string const & at(size_t n) const {
186  using namespace std;
187  if (!size()) {
188  throw out_of_range("Out of range in parameter_list_base::at(size_t)");
189  }
190  return (*_params)[n];
191  }
192 
193  std::string & at(size_t n) {
194  using namespace std;
195  if (!size()) {
196  throw out_of_range("Out of range in parameter_list_base::at(size_t)");
197  }
198  return (*_params)[n];
199  }
201 
203 
208  std::string const & operator[](size_t n) const {
209  return (*_params)[n];
210  }
211 
212  std::string & operator[](size_t n) {
213  return (*_params)[n];
214  }
216 
217 
219  size_t which(std::string const & str) const;
220 
231  template<class CharSeq>
232  void read(chewer<CharSeq> & chew);
233 
243  template<typename BiIter>
244  static std::string make(BiIter first, BiIter last) {
245  std::string s(1,'(');
246  if (first != last) {
247  auto last_but_one = last;
248  std::advance(last_but_one,-1);
249  for ( ;first != last_but_one; ++first) {
250  s += std::string(*first) + ',';
251  }
252  s += std::string(first);
253  }
254  s += ')';
255  return s;
256  }
257 
258 protected:
259 
261  enum defect {
270  };
271 
273  std::shared_ptr<std::vector<std::string>> _params;
277  bool _variadic;
278 };
279 
280 } // namespace innards
281 
282 #endif //EOF
bool well_formed() const
Say whether the parameter_list_base is well-formed.
std::string const & at(size_t n) const
Get a range-checked [const] reference to the nth parameter.
bool operator!=(parameter_list_base const &other) const
Inequality.
std::string str() const
Cast the parameter list to its canonical string representation.
size_t size() const
Get the number of parameters in the parameter_list_base
void read(chewer< CharSeq > &chew)
Read the parameter_list_base from chewer<CharSeq>
parameter_list_base(parameter_list_base const &other)
Copy constructor.
template struct innards::parameter_list_base<Tag> generically defines a common interface of types rep...
static std::string make(BiIter first, BiIter last)
Make a parameter list from a range of objects that are convertible to std::string.
defect
Enumeration of possible defects in a parameter_list_base
std::vector< std::string >::iterator begin()
Get a [const] iterator to the start of the parameter list.
parameter_list_base & operator=(parameter_list_base other)
Assignment.
parameter_list_base(size_t n=0)
Constructor for n parameters.
bool operator==(parameter_list_base const &other) const
Equality.
Non-identifier where parameter expected.
void swap(parameter_list_base &other)
Swap with another parameter_list_base
std::vector< std::string >::iterator end()
Get a [const] iterator to the end of the parameter list.
std::shared_ptr< std::vector< std::string > > _params
The list of parameters.
parameter_list_base(chewer< CharSeq > &chew)
Explicitly construct a given a chewer<CharSeq>.
defect _defect
Is the parameter_list_base well-formed?
`template struct chewer<CharSeq> is a cursor-like type that is associated with a character-sequence t...
Definition: chew.h:248
bool variadic() const
Say whether the parameter_list_base is variadic.
std::string const & operator[](size_t n) const
Get an un-range-checked [const] reference to the nth parameter.
struct symbol encapsulates a preprocessor symbol's state
Definition: symbol.h:58
size_t which(std::string const &str) const
Get the index of the parameter that matches a string, if any, else -1.
bool null() const
Say whether the parameter_list_base is null, i.e. is not even "()".
bool _variadic
Is the parameter_list_base variadic?