-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
allowed.h
110 lines (92 loc) · 3.07 KB
/
allowed.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Copyright 2016 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#ifndef PXR_USD_SDF_ALLOWED_H
#define PXR_USD_SDF_ALLOWED_H
/// \file sdf/allowed.h
#include "pxr/pxr.h"
#include "pxr/usd/sdf/api.h"
#include "pxr/base/tf/diagnostic.h"
#include <optional>
#include <string>
#include <utility>
PXR_NAMESPACE_OPEN_SCOPE
/// \class SdfAllowed
///
/// Indicates if an operation is allowed and, if not, why not.
///
/// A \c SdfAllowed either evaluates to \c true in a boolean context
/// or evaluates to \c false and has a string annotation.
///
class SdfAllowed {
private:
typedef std::optional<std::string> _State;
public:
typedef std::pair<bool, std::string> Pair;
/// Construct \c true.
SdfAllowed() { }
/// Construct \c true.
SdfAllowed(bool x) { TF_AXIOM(x); }
/// Construct \c false with annotation \p whyNot.
SdfAllowed(const char* whyNot) : _state(std::string(whyNot)) { }
/// Construct \c false with annotation \p whyNot.
SdfAllowed(const std::string& whyNot) : _state(whyNot) { }
/// Construct in \p condition with annotation \p whyNot if \c false.
SdfAllowed(bool condition, const char* whyNot) :
SdfAllowed(condition, std::string(whyNot)) { }
/// Construct in \p condition with annotation \p whyNot if \c false.
SdfAllowed(bool condition, const std::string& whyNot) :
_state(condition ? std::nullopt :
std::make_optional(whyNot)) { }
/// Construct from bool,string pair \p x.
SdfAllowed(const Pair& x) : SdfAllowed(x.first, x.second) { }
~SdfAllowed() { }
#if !defined(doxygen)
typedef _State SdfAllowed::*UnspecifiedBoolType;
#endif
/// Returns \c true in a boolean context if allowed, \c false otherwise.
operator UnspecifiedBoolType() const
{
return _state ? NULL : &SdfAllowed::_state;
}
/// Returns \c false in a boolean context if allowed, \c true otherwise.
bool operator!() const
{
return static_cast<bool>(_state);
}
/// Returns the reason why the operation is not allowed. If the
/// operation is allowed this returns the empty string.
operator const std::string&() const
{
return GetWhyNot();
}
/// Returns the reason why the operation is not allowed. If the
/// operation is allowed this returns the empty string.
SDF_API const std::string& GetWhyNot() const;
/// Returns \c true if allowed, otherwise fills \p whyNot if not \c NULL
/// and returns \c false.
bool IsAllowed(std::string* whyNot) const
{
if (whyNot && _state) {
*whyNot = *_state;
}
return !_state;
}
/// Compare to \p other. Returns \c true if both are \c true or
/// both are \c false and reasons why not are identical.
bool operator==(const SdfAllowed& other) const
{
return _state == other._state;
}
bool operator!=(const SdfAllowed& other) const
{
return !(*this == other);
}
private:
_State _state;
};
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_SDF_ALLOWED_H