coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
expansion_base.h
Go to the documentation of this file.
1 #ifndef EXPANSION_BASE_H
2 #define EXPANSION_BASE_H
3 #pragma once
4 /***************************************************************************
5  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
6  * All rights reserved. *
7  * *
8  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
9  * *
10  * Redistribution and use in source and binary forms, with or without *
11  * modification, are permitted provided that the following conditions *
12  * are met: *
13  * *
14  * Redistributions of source code must retain the above copyright *
15  * notice, this list of conditions and the following disclaimer. *
16  * *
17  * Redistributions in binary form must reproduce the above copyright *
18  * notice, this list of conditions and the following disclaimer in the *
19  * documentation and/or other materials provided with the distribution. *
20  * *
21  * Neither the name of Mike Kinghan nor the names of its contributors *
22  * may be used to endorse or promote products derived from this software *
23  * without specific prior written permission. *
24  * *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
32  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
35  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
36  * DAMAGE. *
37  * *
38  **************************************************************************/
39 #include "reference.h"
40 #include <string>
41 #include <memory>
42 
51 {
53  explicit expansion_base(reference const & ref);
54 
56  std::string const & value() const {
57  return _value;
58  }
59 
63  virtual unsigned expand() = 0;
64 
66  virtual void throw_self() const = 0;
67 
71  std::string const & invocation() const override {
72  static std::string s;
73  s = id();
74  if (args()) {
75  s += args().str();
76  }
77  return s;
78  }
79 
88  static std::unique_ptr<expansion_base>
89  factory(bool explain, reference const & ref);
90 
92  static constexpr unsigned max_expansion_size() {
93  return 4196;
94  }
95 
96 protected:
97 
104  void set_expansion_flags();
105 
110  for ( ;_cur_arg < args().size() && !args().is_expandable(_cur_arg);
111  ++_cur_arg){}
112  return _cur_arg;
113  }
114 
122  void edit( std::string & str,
123  size_t at, size_t len,
124  std::string const & replacement) {
125  size_t next_size = str.size() - len + replacement.size();
126  if (next_size > max_expansion_size()) {
127  throw_self();
128  }
129  str.replace(at,len,replacement);
130  }
131 
140  unsigned edit_buf(
141  std::string & str,
142  expansion_base const & e,
143  size_t off = 0);
144 
156  unsigned edit_trailing_args(
157  expansion_base const & e,
158  size_t start = 0);
159 
161  bool args_expansion_done() const {
162  return _cur_arg == args().size();
163  }
164 
166  virtual unsigned expand(std::string & str) = 0;
167 
172  bool substitute();
173 
174 
176  std::string _value;
178  size_t _cur_arg = 0;
179 };
180 
181 #endif // EOF
unsigned edit_buf(std::string &str, expansion_base const &e, size_t off=0)
Replace all remaining occurrences of a reference throughout a string.
std::string str() const
Cast the parameter list to its canonical string representation.
void set_expansion_flags()
Assign the expand-flags of the reference's arguments, if any, in accordance with the definition...
argument_list const & args() const
Get a [const] reference to the argument_list of the reference.
Definition: reference.h:152
expansion_base(reference const &ref)
Construct from a reference.
size_t size() const
Get the number of parameters in the parameter_list_base
unsigned edit_trailing_args(expansion_base const &e, size_t start=0)
Replace all occurrences of a reference throughout a terminal segment of this expansions's arguments...
size_t _cur_arg
Index of the first argument not yet fully expanded.
virtual void throw_self() const =0
Throw the runtime object.
size_t seek_expandable_arg()
Seek to the next argument of the reference, if any, that is eligible for expansion, returning its index if found, else args.size()
bool is_expandable(size_t n) const
Say whether the nth argument, if any, is eligible for macro expansion.
std::string const & id() const
Get the name of the symbol referenced.
Definition: reference.h:146
bool substitute()
Substitute the fully expanded arguments into the definition of the reference, returning true if the c...
static std::unique_ptr< expansion_base > factory(bool explain, reference const &ref)
Global factory of subclasses of expansion_base
void edit(std::string &str, size_t at, size_t len, std::string const &replacement)
static constexpr unsigned max_expansion_size()
Cut-off size for macro-expansions.
std::string _value
The current expanded value.
std::string const & value() const
Get the current expanded value of the reference.
struct expansion_base is an abstract base for classes that encapsulate a mode of macro-expansion of a...
std::string const & invocation() const override
Get a string representing the invocation of the reference with the current expansions of its argument...
bool args_expansion_done() const
Say whether all arguments are fully expanded.
bool explain() const
Say whether we are to explain this reference.
Definition: reference.h:195
virtual unsigned expand()=0
Perform the expansion of the reference returning the total number of edits applied.