DCCL v5
Loading...
Searching...
No Matches
dynamic_conditions.h
1// Copyright 2022-2023:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Dynamic Compact Control Language Library
9// ("DCCL").
10//
11// DCCL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// DCCL is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
23#ifndef DCCLDYNAMICCONDITIONALS20220214H
24#define DCCLDYNAMICCONDITIONALS20220214H
25
26#include <google/protobuf/descriptor.h>
27#include <google/protobuf/message.h>
28
29#include "dccl/def.h"
30#include "option_extensions.pb.h"
31
32namespace sol
33{
34class state;
35}
36
37namespace dccl
38{
40{
41 public:
44
45 void set_field(const google::protobuf::FieldDescriptor* field_desc)
46 {
47 field_desc_ = field_desc;
48 }
49
50 void set_descriptor(const google::protobuf::Descriptor* msg_desc) { msg_desc_ = msg_desc; }
51
52 void set_repeated_index(int index) { index_ = index; }
53
54 void regenerate(const google::protobuf::Message* this_msg,
55 const google::protobuf::Message* root_msg, int repeated_index = -1);
56
57 const dccl::DCCLFieldOptions::Conditions& conditions();
58
59 bool has_required_if() { return conditions().has_required_if() || conditions().has_only_if(); }
60 bool has_omit_if() { return conditions().has_omit_if() || conditions().has_only_if(); }
61 bool has_only_if() { return conditions().has_only_if(); }
62 bool has_min() { return conditions().has_min(); }
63 bool has_max() { return conditions().has_max(); }
64
65 // message-level dynamic conditions
66 bool has_max_bytes();
67
68 bool required();
69 bool omit();
70
71 // required if true, omit if false
72 bool only();
73 double min();
74 double max();
75
76 // message-level: returns the dynamic max_bytes limit
77 uint32_t max_bytes();
78
79 private:
80 std::string return_prefix(const std::string& script)
81 {
82 if (script.find("return") == std::string::npos)
83 return "return " + script;
84 else
85 return script;
86 }
87
88 bool is_initialized() { return root_msg_ && this_msg_ && field_desc_; }
89 bool is_message_initialized() { return root_msg_ && this_msg_ && msg_desc_; }
90 const google::protobuf::FieldDescriptor* field_desc_{nullptr};
91 const google::protobuf::Descriptor* msg_desc_{nullptr};
92 const google::protobuf::Message* this_msg_{nullptr};
93 const google::protobuf::Message* root_msg_{nullptr};
94 int index_{0};
95
96#if DCCL_HAS_LUA
97 sol::state* lua_{nullptr};
98#endif
99};
100
101} // namespace dccl
102
103#endif
Dynamic Compact Control Language namespace.
Definition any.h:28