PLCnext API Documentation 23.6.0.37
PlaceholderExpression.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
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
13namespace Arp { namespace System { namespace Commons { namespace Configuration
14{
15
17{
18public: // 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
34public: // construction/destruction
36 PlaceholderExpression(void) = default;
42 ~PlaceholderExpression(void) = default;
43
44public: // operators
45
46public: // 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
53public: // setter/getter operations
54
55public: // 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
64protected: // operations
65
66private: // static methods
67
68private: // 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
73private: // fields
74
75private: // static fields
76 static const String envVarSeperator;
77};
78
80// inline methods of class PlaceholderExpression
81
82inline bool PlaceholderExpression::HasLogicalExpression(const String& input)
83{
84 return PlaceholderExpression::HasLogicalExpression(input.GetBaseString());
85}
86
87template<class T>
88inline 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
106inline PlaceholderExpression::Enumerator PlaceholderExpression::GetEnumerator(const String& input)
107{
108 return Enumerator(input);
109}
110
112// inline methods of class PlaceholderExpression::Eumerator
113inline 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:1086
const BaseString & GetBaseString() const
Gets the basic std string.
Definition: BasicString.hxx:1511
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API