1 // Copyright 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef EMBOSS_RUNTIME_CPP_EMBOSS_CONSTANT_VIEW_H_ 16 #define EMBOSS_RUNTIME_CPP_EMBOSS_CONSTANT_VIEW_H_ 17 18 #include "runtime/cpp/emboss_maybe.h" 19 20 namespace emboss { 21 namespace support { 22 23 // MaybeConstantView is a "view" type that "reads" a value passed into its 24 // constructor. 25 // 26 // This is used internally by generated structure view classes to provide views 27 // of parameters; in this way, parameters can be treated like fields in the 28 // generated code. 29 template <typename ValueT> 30 class MaybeConstantView { 31 public: MaybeConstantView()32 MaybeConstantView() : value_() {} MaybeConstantView(ValueT value)33 constexpr explicit MaybeConstantView(ValueT value) : value_(value) {} 34 MaybeConstantView(const MaybeConstantView &) = default; 35 MaybeConstantView(MaybeConstantView &&) = default; 36 MaybeConstantView &operator=(const MaybeConstantView &) = default; 37 MaybeConstantView &operator=(MaybeConstantView &&) = default; 38 ~MaybeConstantView() = default; 39 Read()40 constexpr ValueT Read() const { return value_.Value(); } UncheckedRead()41 constexpr ValueT UncheckedRead() const { return value_.ValueOrDefault(); } Ok()42 constexpr bool Ok() const { return value_.Known(); } 43 44 private: 45 ::emboss::support::Maybe<ValueT> value_; 46 }; 47 48 } // namespace support 49 } // namespace emboss 50 51 #endif // EMBOSS_RUNTIME_CPP_EMBOSS_CONSTANT_VIEW_H_ 52