$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 
28 #include <limits> // for numeric_limits<>
29 #include <string> // for string
30 #include <utility> // for move
31 #include <vector> // for vector<>
32 
33 #include <fable/fable_fwd.hpp> // for Environment
34 #include <fable/schema/interface.hpp> // for Base<>
35 
36 namespace fable::schema {
37 
44 constexpr auto FABLE_REGEX_C_IDENTIFIER = "^[a-zA-Z_][a-zA-Z0-9_]*$";
45 
60 class String : public Base<String> {
61  public: // Types and Constructors
62  using Type = std::string;
63 
64  String(Type* ptr, std::string desc) : Base(JsonType::string, std::move(desc)), ptr_(ptr) {}
65 
66  public: // Special
74  String not_empty() &&;
75 
81  [[nodiscard]] size_t min_length() const;
82 
89  String min_length(size_t value) &&;
90 
96  void set_min_length(size_t value);
97 
103  [[nodiscard]] size_t max_length() const;
104 
111  String max_length(size_t value) &&;
112 
118  void set_max_length(size_t value);
119 
125  [[nodiscard]] const std::string& pattern() const;
126 
133  String pattern(const std::string& value) &&;
134 
140  void set_pattern(const std::string& value);
141 
148  String c_identifier() &&;
149 
155  [[nodiscard]] bool interpolate() const;
156 
162  String interpolate(bool value) &&;
163 
184  void set_interpolate(bool value);
185 
191  [[nodiscard]] Environment* environment() const;
192 
201  String environment(Environment* env) &&;
202 
210  void set_environment(Environment* env);
211 
217  [[nodiscard]] const std::vector<std::string>& enum_of() const;
218 
228  String enum_of(std::vector<std::string>&& init);
229 
238  void set_enum_of(std::vector<std::string>&& init);
239 
240  public: // Overrides
241  [[nodiscard]] Json json_schema() const override;
242  bool validate(const Conf& c, std::optional<SchemaError>& err) const override;
243  using Interface::to_json;
244  void to_json(Json& j) const override;
245  void from_conf(const Conf& c) override;
246  [[nodiscard]] Json serialize(const Type& x) const;
247  [[nodiscard]] Type deserialize(const Conf& c) const;
248  void serialize_into(Json& j, const Type& x) const { j = serialize(x); }
249  void deserialize_into(const Conf& c, Type& x) const { x = deserialize(c); }
250  void reset_ptr() override;
251 
252  private:
253  bool interpolate_{false};
254  size_t min_length_{0};
255  size_t max_length_{std::numeric_limits<size_t>::max()};
256  std::string pattern_{};
257  std::vector<std::string> enum_{};
258  Environment* env_{nullptr};
259  Type* ptr_{nullptr};
260 };
261 
262 inline String make_schema(std::string* ptr, std::string desc) {
263  return {ptr, std::move(desc)};
264 }
265 
266 } // namespace fable::schema
Definition: conf.hpp:76
Definition: environment.hpp:34
Definition: interface.hpp:398
virtual Json to_json() const
Definition: interface.hpp:254
Definition: string.hpp:60
void set_enum_of(std::vector< std::string > &&init)
Definition: string.cpp:80
void set_pattern(const std::string &value)
Definition: string.cpp:54
size_t min_length() const
Definition: string.cpp:34
Environment * environment() const
Definition: string.cpp:72
void set_environment(Environment *env)
Definition: string.cpp:73
virtual Json to_json() const
Definition: interface.hpp:254
size_t max_length() const
Definition: string.cpp:46
void set_interpolate(bool value)
Definition: string.cpp:66
Json json_schema() const override
Definition: string.cpp:86
void set_min_length(size_t value)
Definition: string.cpp:35
void reset_ptr() override
Definition: string.cpp:137
const std::string & pattern() const
Definition: string.cpp:53
void set_max_length(size_t value)
Definition: string.cpp:47
String not_empty() &&
Definition: string.cpp:41
const std::vector< std::string > & enum_of() const
Definition: string.cpp:79
void from_conf(const Conf &c) override
Definition: string.cpp:151
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: string.cpp:106
bool interpolate() const
Definition: string.cpp:65
String c_identifier() &&
Definition: string.cpp:60
nlohmann::json Json
Definition: fable_fwd.hpp:35
constexpr auto FABLE_REGEX_C_IDENTIFIER
Definition: string.hpp:44