MADARA  3.4.1
DynamicPrefixIntConvert.h
Go to the documentation of this file.
1 
2 
3 #ifndef _MADARA_FILTERS_DYNAMIC_PREFIX_INTCONVERT_H_
4 #define _MADARA_FILTERS_DYNAMIC_PREFIX_INTCONVERT_H_
5 
13 #include <sstream>
14 
15 #include "AggregateFilter.h"
17 #include "madara/utility/Utility.h"
18 
19 namespace madara
20 {
21 namespace filters
22 {
27 {
28 public:
36  DynamicPrefixIntConvert(const std::string& string_vector_name = ".prefixes",
38  : initialized_(false),
39  name(string_vector_name),
40  value(new_value),
41  prefixes_()
42  {
43  }
44 
48  virtual ~DynamicPrefixIntConvert() = default;
49 
55  inline virtual void filter(knowledge::KnowledgeMap& records,
57  {
58  // local copy (fast access to prefix vector)
59  std::vector<std::string> prefixes;
60 
61  if (!initialized_)
62  {
63  // set the initial vector up
64  prefixes_.set_name(name, vars);
65  initialized_ = true;
66  }
67  else
68  {
69  // resize for what the current vector is indicating
70  prefixes_.resize();
71  }
72 
73  // copy the knowledge base prefixes to a STL vector for speed
74  prefixes_.copy_to(prefixes);
75 
76  // because of the usage of erase, don't auto inc record in for loop
77  for (auto record : records)
78  {
79  // check for valid prefix
80  bool accepted_prefix = false;
81 
82  if (prefixes.size() > 0)
83  {
84  for (auto prefix : prefixes)
85  {
86  if (utility::begins_with(record.first, prefix))
87  {
88  accepted_prefix = true;
89  }
90  }
91  }
92  else
93  {
94  accepted_prefix = true;
95  }
96 
97  // if not valid, remove the record and update iterator
98  if (!accepted_prefix)
99  {
102  "DynamicPrefixIntConvert::filter: "
103  "not integerizing variable %s\n",
104  record.first.c_str())
105  } // end not valid prefix
106  else
107  {
108  // valid prefix so set the value
109  record.second = value;
110  } // end valid prefix
111  } // end iteration over all filter records
112  } // end filter function
113 
114 private:
119 
120 public:
125 
128 
129 protected:
134 };
135 }
136 }
137 
138 #endif // _MADARA_FILTERS_DYNAMIC_PREFIX_INTCONVERT_H_
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
Abstract base class for implementing aggregate record filters via a functor interface.
Filter for integerizing variables (usually before sending)
virtual void filter(knowledge::KnowledgeMap &records, const transport::TransportContext &, knowledge::Variables &vars)
Converts records to integers.
bool initialized_
Tracks if the prefix vector has been initialized.
std::string name
The string vector name to bind to inside the knowledge base.
DynamicPrefixIntConvert(const std::string &string_vector_name=".prefixes", knowledge::KnowledgeRecord::Integer new_value=0)
Constructor.
virtual ~DynamicPrefixIntConvert()=default
Destructor.
knowledge::containers::StringVector prefixes_
A map of discovered peers.
knowledge::KnowledgeRecord::Integer value
integer to convert all valid records to
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
This class stores a vector of strings inside of KaRL.
Definition: StringVector.h:32
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
void resize(int size=-1, bool delete_vars=true)
Resizes the vector.
Provides context about the transport.
constexpr string_t string
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
::std::map< std::string, KnowledgeRecord > KnowledgeMap
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
Definition: Utility.inl:638
Copyright(c) 2020 Galois.