coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
reference_cache.h
Go to the documentation of this file.
1 #ifndef REFERENCE_CACHE_H
2 #define REFERENCE_CACHE_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 
40 #include "evaluation.h"
41 #include <string>
42 #include <map>
43 
52 {
54  struct entry
55  {
64  std::string const & expansion,
65  evaluation const & eval,
66  bool reported = false,
67  bool complete = true)
68  : _expansion(expansion),_eval(eval),_reported(reported),
70 
72  std::string const & expansion() const {
73  return _expansion;
74  }
75 
79  evaluation const & eval() const {
80  return _eval;
81  }
82  evaluation & eval() {
83  return _eval;
84  }
86 
88  bool reported() const {
89  return _reported;
90  }
91 
93  void set_reported(bool value = true) {
94  _reported = value;
95  }
96 
98  bool complete() const {
99  return _complete;
100  }
101 
103  void set_complete(bool value = true) {
104  _complete = value;
105  }
106 
107  private:
109  std::string _expansion;
113  bool _reported;
115  bool _complete;
116  };
117 
119  using map = std::map<std::string,entry>;
121  using value_type = map::value_type;
123  using iterator = map::iterator;
125  using const_iterator = map::const_iterator;
127  using insert_result = std::pair<iterator,bool>;
128 
134  static iterator insert(value_type const & v, iterator hint) {
135  return get_map().insert(hint,v);
136  }
137 
138 
145  static iterator
146  insert(std::string const & key, entry const & e, iterator hint) {
147  return insert(value_type(key,e),hint);
148  }
149 
151  static iterator lower_bound(std::string const & key) {
152  return get_map().lower_bound(key);
153  }
154 
156  static void erase_symbol(std::string const & id){
157  auto i = get_map().lower_bound(id);
158  while (i != get_map().end()) {
159  std::string const & key = i->first;
160  if (key.find(id) == 0 &&
161  (key.length() == id.length() || key[id.length()] == '(')) {
162  i = get_map().erase(i);
163  } else {
164  break;
165  }
166  }
167  }
168 
170  static void clear() {
171  get_map().clear();
172  }
173 
175  template<class Iter>
176  static bool at_end(Iter i) {
177  return i == get_map().end();
178  }
179 
180 private:
181 
183  static map & get_map() {
184  static map the_map;
185  return the_map;
186  }
187 };
188 
189 #endif //EOF
static iterator insert(value_type const &v, iterator hint)
Insert a value_type into the cache.
static bool at_end(Iter i)
Say whether an iterator points to the end of the cache.
void set_reported(bool value=true)
Mark the entry as reported, or not.
evaluation _eval
The evaluation of the cached reference.
bool _reported
Has the reference been reported.
static iterator lower_bound(std::string const &key)
Get an iteraror to the lower bound of a key in the cache.
static void clear()
Empty the cache.
map::iterator iterator
Iterator type on map
map::value_type value_type
Value-type of map
void set_complete(bool value=true)
Mark the entry as fully expanded, or not.
bool _complete
Is the cached expansion complete?
std::string _expansion
The expansion of the cached reference.
static iterator insert(std::string const &key, entry const &e, iterator hint)
Insert an entry by key into cache.
evaluation const & eval() const
Get a [const] reference to the evaluation of the cached reference.
std::map< std::string, entry > map
Type of map implementing the reference cache.
std::string const & expansion() const
Get the expansion of the cached reference.
static map & get_map()
The cache map.
struct reference_cache encapsulates a cache of the the expansions and evaluations of symbol reference...
entry(std::string const &expansion, evaluation const &eval, bool reported=false, bool complete=true)
std::pair< iterator, bool > insert_result
Type of result of insertion.
Encapsulates an entry in the reference cache.
static void erase_symbol(std::string const &id)
Delete all cached references of a given symbol.
bool complete() const
Is the cached reference fully expanded?
struct evaluation represents the result of evaluating a putative expression.
Definition: evaluation.h:48
map::const_iterator const_iterator
Const-iterator type on map
bool reported() const
Has the cached reference been reported.