This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
allowed.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_SDF_ALLOWED_H
8#define PXR_USD_SDF_ALLOWED_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/usd/sdf/api.h"
15
16#include <optional>
17#include <string>
18#include <utility>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
30private:
31 typedef std::optional<std::string> _State;
32
33public:
34 typedef std::pair<bool, std::string> Pair;
35
39 SdfAllowed(bool x) { TF_AXIOM(x); }
41 SdfAllowed(const char* whyNot) : _state(std::string(whyNot)) { }
43 SdfAllowed(const std::string& whyNot) : _state(whyNot) { }
45 SdfAllowed(bool condition, const char* whyNot) :
46 SdfAllowed(condition, std::string(whyNot)) { }
48 SdfAllowed(bool condition, const std::string& whyNot) :
49 _state(condition ? std::nullopt :
50 std::make_optional(whyNot)) { }
52 SdfAllowed(const Pair& x) : SdfAllowed(x.first, x.second) { }
53 ~SdfAllowed() { }
54
55#if !defined(doxygen)
56 typedef _State SdfAllowed::*UnspecifiedBoolType;
57#endif
58
60 operator UnspecifiedBoolType() const
61 {
62 return _state ? NULL : &SdfAllowed::_state;
63 }
64
66 bool operator!() const
67 {
68 return static_cast<bool>(_state);
69 }
70
73 operator const std::string&() const
74 {
75 return GetWhyNot();
76 }
77
80 SDF_API const std::string& GetWhyNot() const;
81
84 bool IsAllowed(std::string* whyNot) const
85 {
86 if (whyNot && _state) {
87 *whyNot = *_state;
88 }
89 return !_state;
90 }
91
94 bool operator==(const SdfAllowed& other) const
95 {
96 return _state == other._state;
97 }
98
99 bool operator!=(const SdfAllowed& other) const
100 {
101 return !(*this == other);
102 }
103
104private:
105 _State _state;
106};
107
108PXR_NAMESPACE_CLOSE_SCOPE
109
110#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:29
SDF_API const std::string & GetWhyNot() const
Returns the reason why the operation is not allowed.
SdfAllowed()
Construct true.
Definition: allowed.h:37
bool IsAllowed(std::string *whyNot) const
Returns true if allowed, otherwise fills whyNot if not NULL and returns false.
Definition: allowed.h:84
SdfAllowed(const Pair &x)
Construct from bool,string pair x.
Definition: allowed.h:52
SdfAllowed(bool x)
Construct true.
Definition: allowed.h:39
bool operator==(const SdfAllowed &other) const
Compare to other.
Definition: allowed.h:94
bool operator!() const
Returns false in a boolean context if allowed, true otherwise.
Definition: allowed.h:66
SdfAllowed(bool condition, const char *whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:45
SdfAllowed(const std::string &whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:43
SdfAllowed(const char *whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:41
SdfAllowed(bool condition, const std::string &whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:48
#define TF_AXIOM(cond)
Aborts if the condition cond is not met.
Definition: diagnostic.h:193
STL namespace.