$darkmode
string.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
26 #pragma once
27 #ifndef FABLE_SCHEMA_STRING_HPP_
28 #define FABLE_SCHEMA_STRING_HPP_
29 
30 #include <limits> // for numeric_limits<>
31 #include <string> // for string
32 #include <utility> // for move
33 #include <vector> // for vector<>
34 
35 #include <fable/schema/interface.hpp> // for Base<>
36 
37 namespace fable {
38 
39 // Forward declarations:
40 class Environment; // from <fable/environment.hpp>
41 
42 namespace schema {
43 
50 #define FABLE_REGEX_C_IDENTIFIER "^[a-zA-Z_][a-zA-Z0-9_]*$"
51 
66 class String : public Base<String> {
67  public: // Types and Constructors
68  using Type = std::string;
69 
70  String(Type* ptr, std::string&& desc) : Base(JsonType::string, std::move(desc)), ptr_(ptr) {}
71 
72  public: // Special
80  String not_empty() &&;
81 
87  size_t min_length() const;
88 
95  String min_length(size_t value) &&;
96 
102  void set_min_length(size_t value);
103 
109  size_t max_length() const;
110 
117  String max_length(size_t value) &&;
118 
124  void set_max_length(size_t value);
125 
131  const std::string& pattern() const;
132 
139  String pattern(const std::string& value) &&;
140 
146  void set_pattern(const std::string& value);
147 
154  String c_identifier() &&;
155 
161  bool interpolate() const;
162 
168  String interpolate(bool value) &&;
169 
190  void set_interpolate(bool value);
191 
197  Environment* environment() const;
198 
207  String environment(Environment* env) &&;
208 
216  void set_environment(Environment* env);
217 
223  const std::vector<std::string>& enum_of() const;
224 
234  String enum_of(std::vector<std::string>&& init);
235 
244  void set_enum_of(std::vector<std::string>&& init);
245 
246  public: // Overrides
247  Json json_schema() const override;
248  void validate(const Conf& c) const override;
249  using Interface::to_json;
250  void to_json(Json& j) const override;
251  void from_conf(const Conf& c) override;
252  Json serialize(const Type& x) const;
253  Type deserialize(const Conf& c) const;
254  void reset_ptr() override;
255 
256  private:
257  bool interpolate_{false};
258  size_t min_length_{0};
259  size_t max_length_{std::numeric_limits<size_t>::max()};
260  std::string pattern_{};
261  std::vector<std::string> enum_{};
262  Environment* env_{nullptr};
263  Type* ptr_{nullptr};
264 };
265 
266 inline String make_schema(std::string* ptr, std::string&& desc) {
267  return String(ptr, std::move(desc));
268 }
269 
270 } // namespace schema
271 } // namespace fable
272 
273 #endif // FABLE_SCHEMA_STRING_HPP_
Definition: conf.hpp:74
void from_conf(const Conf &c) override
Definition: string.cpp:144
Environment * environment() const
Definition: string.cpp:73
void set_enum_of(std::vector< std::string > &&init)
Definition: string.cpp:81
Definition: string.hpp:66
bool interpolate() const
Definition: string.cpp:66
const std::string & pattern() const
Definition: string.cpp:54
size_t min_length() const
Definition: string.cpp:35
void set_interpolate(bool value)
Definition: string.cpp:67
void set_min_length(size_t value)
Definition: string.cpp:36
nlohmann::json Json
Definition: json.hpp:62
Definition: conf.hpp:70
const std::vector< std::string > & enum_of() const
Definition: string.cpp:80
Definition: environment.hpp:37
String c_identifier() &&
Definition: string.cpp:61
virtual Json to_json() const
Definition: interface.hpp:220
void set_pattern(const std::string &value)
Definition: string.cpp:55
void reset_ptr() override
Definition: string.cpp:130
size_t max_length() const
Definition: string.cpp:47
void set_environment(Environment *env)
Definition: string.cpp:74
void set_max_length(size_t value)
Definition: string.cpp:48
String not_empty() &&
Definition: string.cpp:42
void validate(const Conf &c) const override
Definition: string.cpp:107
Definition: interface.hpp:354
Json json_schema() const override
Definition: string.cpp:87