forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allowed.h
126 lines (108 loc) · 3.95 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache 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 <string>
#include <utility>
#include <boost/optional.hpp>
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 boost::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) :
_state(!condition, std::string(whyNot)) { }
/// Construct in \p condition with annotation \p whyNot if \c false.
SdfAllowed(bool condition, const std::string& whyNot) :
_state(!condition, whyNot) { }
/// Construct from bool,string pair \p x.
SdfAllowed(const Pair& x) : _state(!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