MADARA  3.2.3
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,
35  bool & key_expansion_necessary,
36  std::vector< std::string> & splitters,
37  std::vector< std::string> & tokens,
38  std::vector< std::string> & pivot_list)
39  {
41 
42  // this key requires expansion. We do the compilation and error checking here
43  // as the key shouldn't change, and this allows us to only have to do this
44  // once
45  if (key.find ("{") != key.npos)
46  {
48  "%s: Variable %s requires variable expansion.\n",
49  print_prefix.c_str (), key.c_str ());
50 
51  key_expansion_necessary = true;
52  splitters.push_back ("{");
53  splitters.push_back ("}");
54 
55  utility::tokenizer (key, splitters, tokens, pivot_list);
56 
57  if (pivot_list.size () % 2 != 0)
58  {
59  std::stringstream buffer;
60  buffer << print_prefix;
61  buffer << ": KARL COMPILE ERROR: matching braces not found in ";
62  buffer << key << "\n";
64  buffer.str ().c_str ());
65 
66  throw KarlException (buffer.str ());
67  }
68 
69  // check for braces that are not properly closed
70  std::vector<std::string>::const_iterator pivot = pivot_list.begin ();
71  unsigned int num_opens = 0;
72  unsigned int num_closes = 0;
73 
74  for (; pivot != pivot_list.end (); ++pivot)
75  {
76  if (*pivot == "{")
77  {
78  ++num_opens;
79  }
80  else if (*pivot == "}")
81  {
82  ++num_closes;
83  }
84  }
85 
86  if (num_opens > num_closes)
87  {
88  std::stringstream buffer;
89  buffer << print_prefix;
90  buffer << ": KARL COMPILE ERROR: more opening braces than closing in ";
91  buffer << key << "\n";
93  buffer.str ().c_str ());
94 
95  throw KarlException (buffer.str ());
96  }
97  else if (num_closes > num_opens)
98  {
99  std::stringstream buffer;
100  buffer << print_prefix;
101  buffer << ": KARL COMPILE ERROR: more closing braces than opening in ";
102  buffer << key << "\n";
104  buffer.str ().c_str ());
105 
106  throw KarlException (buffer.str ());
107  }
108  }
109  // no variable expansion necessary. Create a hard link to the record_->
110  // this will save us lots of clock cycles each variable access or
111  // mutation.
112  else
113  {
114  ref = context.get_ref (key);
115  }
116 
117  return ref;
118  }
119  };
120  }
121 }
122 
123 #endif // _MADARA_NO_KARL_
124 
125 #endif /* _MADARA_EXPRESSION_VARIABLEEXPANDER_H_ */
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:242
This class stores variables and their values for use by any entity needing state information in a thr...
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.
Optimized reference to a variable within the knowledge base.
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:88
static struct madara::knowledge::tags::string_t string
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:18
Copyright (c) 2015 Carnegie Mellon University.