MADARA  3.4.1
VariableExpander.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _MADARA_EXPRESSION_VARIABLEEXPANDER_H_
3 #define _MADARA_EXPRESSION_VARIABLEEXPANDER_H_
4 
5 #ifndef _MADARA_NO_KARL_
6 
7 #include <string>
8 #include <vector>
9 #include <stdexcept>
10 
11 #include "madara/logger/Logger.h"
13 #include "madara/utility/Utility.h"
14 
15 namespace madara
16 {
17 namespace expression
18 {
22 class MADARA_EXPORT VariableExpander
23 {
24 public:
32  const std::string& print_prefix,
34  madara::logger::Logger* cur_logger, bool& key_expansion_necessary,
35  std::vector<std::string>& splitters, std::vector<std::string>& tokens,
36  std::vector<std::string>& pivot_list)
37  {
39 
40  // this key requires expansion. We do the compilation and error checking
41  // here as the key shouldn't change, and this allows us to only have to do
42  // this once
43  if (key.find("{") != key.npos)
44  {
46  "%s: Variable %s requires variable expansion.\n",
47  print_prefix.c_str(), key.c_str());
48 
49  key_expansion_necessary = true;
50  splitters.push_back("{");
51  splitters.push_back("}");
52 
53  utility::tokenizer(key, splitters, tokens, pivot_list);
54 
55  if (pivot_list.size() % 2 != 0)
56  {
57  std::stringstream buffer;
58  buffer << print_prefix;
59  buffer << ": KARL COMPILE ERROR: matching braces not found in ";
60  buffer << key << "\n";
62  cur_logger, logger::LOG_ERROR, buffer.str().c_str());
63 
64  throw exceptions::KarlException(buffer.str());
65  }
66 
67  // check for braces that are not properly closed
68  std::vector<std::string>::const_iterator pivot = pivot_list.begin();
69  unsigned int num_opens = 0;
70  unsigned int num_closes = 0;
71 
72  for (; pivot != pivot_list.end(); ++pivot)
73  {
74  if (*pivot == "{")
75  {
76  ++num_opens;
77  }
78  else if (*pivot == "}")
79  {
80  ++num_closes;
81  }
82  }
83 
84  if (num_opens > num_closes)
85  {
86  std::stringstream buffer;
87  buffer << print_prefix;
88  buffer << ": KARL COMPILE ERROR: more opening braces than closing in ";
89  buffer << key << "\n";
91  cur_logger, logger::LOG_ERROR, buffer.str().c_str());
92 
93  throw exceptions::KarlException(buffer.str());
94  }
95  else if (num_closes > num_opens)
96  {
97  std::stringstream buffer;
98  buffer << print_prefix;
99  buffer << ": KARL COMPILE ERROR: more closing braces than opening in ";
100  buffer << key << "\n";
102  cur_logger, logger::LOG_ERROR, buffer.str().c_str());
103 
104  throw exceptions::KarlException(buffer.str());
105  }
106  }
107  // no variable expansion necessary. Create a hard link to the record_->
108  // this will save us lots of clock cycles each variable access or
109  // mutation.
110  else
111  {
112  ref = context.get_ref(key);
113  }
114 
115  return ref;
116  }
117 };
118 }
119 }
120 
121 #endif // _MADARA_NO_KARL_
122 
123 #endif /* _MADARA_EXPRESSION_VARIABLEEXPANDER_H_ */
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:21
madara::knowledge::VariableReference expand(const std::string &key, const std::string &print_prefix, madara::knowledge::ThreadSafeContext &context, madara::logger::Logger *cur_logger, bool &key_expansion_necessary, std::vector< std::string > &splitters, std::vector< std::string > &tokens, std::vector< std::string > &pivot_list)
Expands a key into either a VariableReference or tokens.
This class stores variables and their values for use by any entity needing state information in a thr...
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Optimized reference to a variable within the knowledge base.
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:165
constexpr string_t string
void tokenizer(const std::string &input, const ::std::vector< std::string > &splitters, ::std::vector< std::string > &tokens, ::std::vector< std::string > &pivots)
Split a string into tokens.
Definition: Utility.cpp:224
Copyright(c) 2020 Galois.