PLCnext API Documentation  22.9.0.33
PlaceholderExpression.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/Enumerator.hxx"
9 #include "Arp/System/Commons/Exceptions/Exceptions.h"
10 #include "Arp/System/Commons/Environment.hpp"
11 #include <sstream>
12 
13 namespace Arp { namespace System { namespace Commons { namespace Configuration
14 {
15 
17 {
18 public: // nested types
19  class Enumerator : public Arp::Enumerator<const String&>
20  {
21  public: // construcion
22  Enumerator(const String& input);
23 
24  public: // Enumerator<String> operations
25  bool MoveNext(void)override;
26  bool IsCurrentBuiltIn(void); // placeholder starts with '!'
27 
28  private:
29  size_t currentStart = 0;
30  size_t currentEnd = 0;
31  String input;
32  };
33 
34 public: // construction/destruction
36  PlaceholderExpression(void) = default;
42  ~PlaceholderExpression(void) = default;
43 
44 public: // operators
45 
46 public: // static operations
47  static bool HasPlaceholder(const String& input, const String& placeholder);
48  static bool HasPlaceholder(const String& input);
49  static bool HasLogicalExpression(const String& input);
50  static String GetPlaceholderName(const String& placeholderToken);
51  static Enumerator GetEnumerator(const String& input);
52 
53 public: // setter/getter operations
54 
55 public: // operations
56  String Resolve(const String& input);
57  bool TryResolve(const String& input, String& result);
58  bool ResolveCondition(const String& input);
59  String ResolveLogicalExpression(const String& input);
60 
61  template<class T>
62  T ResolveTo(const String& input);
63 
64 protected: // operations
65 
66 private: // static methods
67 
68 private: // methods
69  static bool TryResolveLogicalExpression(const String& input, bool& result);
70  static bool TryResolveLogicalExpression(const String& input, String& result);
71  static bool HasLogicalExpression(const std::string& input);
72 
73 private: // fields
74 
75 private: // static fields
76  static const String envVarSeperator;
77 };
78 
80 // inline methods of class PlaceholderExpression
81 
82 inline bool PlaceholderExpression::HasLogicalExpression(const String& input)
83 {
84  return PlaceholderExpression::HasLogicalExpression(input.GetBaseString());
85 }
86 
87 template<class T>
88 inline T PlaceholderExpression::ResolveTo(const String& input)
89 {
90  if (input.IsEmpty())
91  {
92  throw ArgumentException("Input is empty.");
93  }
94  // else
95  std::istringstream iss(this->Resolve(input));
96  T result;
97  iss >> std::boolalpha >> result;
98  if (iss.fail())
99  {
100  throw ArgumentException("Input is not of required type.");
101  }
102  // else
103  return result;
104 }
105 
106 inline PlaceholderExpression::Enumerator PlaceholderExpression::GetEnumerator(const String& input)
107 {
108  return Enumerator(input);
109 }
110 
112 // inline methods of class PlaceholderExpression::Eumerator
113 inline PlaceholderExpression::Enumerator::Enumerator(const String& input)
114  : input(input)
115 {
116 }
117 
118 }}}} // end of namespace Arp::System::Commons::Configuration
This class defines a base class for all enumerator implementations and some predefined enumerators as...
Definition: Enumerator.hxx:22
Enumerator(void)=default
Constructs an Enumerator instance.
This exception is used when an invalid argument occurs.
Definition: ArgumentException.hpp:15
bool MoveNext(void) override
Moves this enumerator to the next position.
Definition: PlaceholderExpression.hpp:17
PlaceholderExpression(const PlaceholderExpression &arg)=default
Copy constructor.
~PlaceholderExpression(void)=default
Destructs this instance and frees all resources.
PlaceholderExpression & operator=(const PlaceholderExpression &arg)=default
Assignment operator.
PlaceholderExpression(void)=default
Constructs an PlaceholderExpression instance.
bool IsEmpty() const
Determines if this string is empty.
Definition: BasicString.hxx:1076
const BaseString & GetBaseString() const
Gets the basic std string.
Definition: BasicString.hxx:1501
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API