HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
27 /// \file sdf/allowed.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/sdf/api.h"
31 #include "pxr/base/tf/diagnostic.h"
32 
33 #include <optional>
34 #include <string>
35 #include <utility>
36 
38 
39 /// \class SdfAllowed
40 ///
41 /// Indicates if an operation is allowed and, if not, why not.
42 ///
43 /// A \c SdfAllowed either evaluates to \c true in a boolean context
44 /// or evaluates to \c false and has a string annotation.
45 ///
46 class SdfAllowed {
47 private:
48  typedef std::optional<std::string> _State;
49 
50 public:
51  typedef std::pair<bool, std::string> Pair;
52 
53  /// Construct \c true.
54  SdfAllowed() { }
55  /// Construct \c true.
56  SdfAllowed(bool x) { TF_AXIOM(x); }
57  /// Construct \c false with annotation \p whyNot.
58  SdfAllowed(const char* whyNot) : _state(std::string(whyNot)) { }
59  /// Construct \c false with annotation \p whyNot.
60  SdfAllowed(const std::string& whyNot) : _state(whyNot) { }
61  /// Construct in \p condition with annotation \p whyNot if \c false.
62  SdfAllowed(bool condition, const char* whyNot) :
63  SdfAllowed(condition, std::string(whyNot)) { }
64  /// Construct in \p condition with annotation \p whyNot if \c false.
65  SdfAllowed(bool condition, const std::string& whyNot) :
66  _state(condition ? std::nullopt :
67  std::make_optional(whyNot)) { }
68  /// Construct from bool,string pair \p x.
69  SdfAllowed(const Pair& x) : SdfAllowed(x.first, x.second) { }
71 
72 #if !defined(doxygen)
73  typedef _State SdfAllowed::*UnspecifiedBoolType;
74 #endif
75 
76  /// Returns \c true in a boolean context if allowed, \c false otherwise.
77  operator UnspecifiedBoolType() const
78  {
79  return _state ? NULL : &SdfAllowed::_state;
80  }
81 
82  /// Returns \c false in a boolean context if allowed, \c true otherwise.
83  bool operator!() const
84  {
85  return static_cast<bool>(_state);
86  }
87 
88  /// Returns the reason why the operation is not allowed. If the
89  /// operation is allowed this returns the empty string.
90  operator const std::string&() const
91  {
92  return GetWhyNot();
93  }
94 
95  /// Returns the reason why the operation is not allowed. If the
96  /// operation is allowed this returns the empty string.
97  SDF_API const std::string& GetWhyNot() const;
98 
99  /// Returns \c true if allowed, otherwise fills \p whyNot if not \c NULL
100  /// and returns \c false.
101  bool IsAllowed(std::string* whyNot) const
102  {
103  if (whyNot && _state) {
104  *whyNot = *_state;
105  }
106  return !_state;
107  }
108 
109  /// Compare to \p other. Returns \c true if both are \c true or
110  /// both are \c false and reasons why not are identical.
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 
121 private:
122  _State _state;
123 };
124 
126 
127 #endif // PXR_USD_SDF_ALLOWED_H
GLint first
Definition: glcorearb.h:405
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
SdfAllowed(bool condition, const char *whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:62
bool operator!() const
Returns false in a boolean context if allowed, true otherwise.
Definition: allowed.h:83
bool IsAllowed(std::string *whyNot) const
Definition: allowed.h:101
bool operator==(const SdfAllowed &other) const
Definition: allowed.h:111
std::pair< bool, std::string > Pair
Definition: allowed.h:51
SdfAllowed(bool condition, const std::string &whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:65
SDF_API const std::string & GetWhyNot() const
SdfAllowed(const std::string &whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:60
SdfAllowed(bool x)
Construct true.
Definition: allowed.h:56
GLint GLenum GLint x
Definition: glcorearb.h:409
SdfAllowed(const char *whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:58
#define SDF_API
Definition: api.h:40
#define TF_AXIOM(cond)
SdfAllowed()
Construct true.
Definition: allowed.h:54
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
~SdfAllowed()
Definition: allowed.h:70
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool operator!=(const SdfAllowed &other) const
Definition: allowed.h:116
SdfAllowed(const Pair &x)
Construct from bool,string pair x.
Definition: allowed.h:69
_State SdfAllowed::* UnspecifiedBoolType
Definition: allowed.h:73