Loading...
Searching...
No Matches
allowed.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_USD_SDF_ALLOWED_H
25#define PXR_USD_SDF_ALLOWED_H
26
28
29#include "pxr/pxr.h"
30#include "pxr/usd/sdf/api.h"
32
33#include <optional>
34#include <string>
35#include <utility>
36
37PXR_NAMESPACE_OPEN_SCOPE
38
47private:
48 typedef std::optional<std::string> _State;
49
50public:
51 typedef std::pair<bool, std::string> Pair;
52
56 SdfAllowed(bool x) { TF_AXIOM(x); }
58 SdfAllowed(const char* whyNot) : _state(std::string(whyNot)) { }
60 SdfAllowed(const std::string& whyNot) : _state(whyNot) { }
62 SdfAllowed(bool condition, const char* whyNot) :
63 SdfAllowed(condition, std::string(whyNot)) { }
65 SdfAllowed(bool condition, const std::string& whyNot) :
66 _state(condition ? std::nullopt :
67 std::make_optional(whyNot)) { }
69 SdfAllowed(const Pair& x) : SdfAllowed(x.first, x.second) { }
70 ~SdfAllowed() { }
71
72#if !defined(doxygen)
73 typedef _State SdfAllowed::*UnspecifiedBoolType;
74#endif
75
77 operator UnspecifiedBoolType() const
78 {
79 return _state ? NULL : &SdfAllowed::_state;
80 }
81
83 bool operator!() const
84 {
85 return static_cast<bool>(_state);
86 }
87
90 operator const std::string&() const
91 {
92 return GetWhyNot();
93 }
94
97 SDF_API const std::string& GetWhyNot() const;
98
101 bool IsAllowed(std::string* whyNot) const
102 {
103 if (whyNot && _state) {
104 *whyNot = *_state;
105 }
106 return !_state;
107 }
108
111 bool operator==(const SdfAllowed& other) const
112 {
113 return _state == other._state;
114 }
115
116 bool operator!=(const SdfAllowed& other) const
117 {
118 return !(*this == other);
119 }
120
121private:
122 _State _state;
123};
124
125PXR_NAMESPACE_CLOSE_SCOPE
126
127#endif // PXR_USD_SDF_ALLOWED_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
Indicates if an operation is allowed and, if not, why not.
Definition: allowed.h:46
SDF_API const std::string & GetWhyNot() const
Returns the reason why the operation is not allowed.
SdfAllowed()
Construct true.
Definition: allowed.h:54
bool IsAllowed(std::string *whyNot) const
Returns true if allowed, otherwise fills whyNot if not NULL and returns false.
Definition: allowed.h:101
SdfAllowed(const Pair &x)
Construct from bool,string pair x.
Definition: allowed.h:69
SdfAllowed(bool x)
Construct true.
Definition: allowed.h:56
bool operator==(const SdfAllowed &other) const
Compare to other.
Definition: allowed.h:111
bool operator!() const
Returns false in a boolean context if allowed, true otherwise.
Definition: allowed.h:83
SdfAllowed(bool condition, const char *whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:62
SdfAllowed(const std::string &whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:60
SdfAllowed(const char *whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:58
SdfAllowed(bool condition, const std::string &whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:65
#define TF_AXIOM(cond)
Aborts if the condition cond is not met.
Definition: diagnostic.h:210
STL namespace.