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 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 
10 /// \file sdf/allowed.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/sdf/api.h"
14 #include "pxr/base/tf/diagnostic.h"
15 
16 #include <optional>
17 #include <string>
18 #include <utility>
19 
21 
22 /// \class SdfAllowed
23 ///
24 /// Indicates if an operation is allowed and, if not, why not.
25 ///
26 /// A \c SdfAllowed either evaluates to \c true in a boolean context
27 /// or evaluates to \c false and has a string annotation.
28 ///
29 class SdfAllowed {
30 private:
31  typedef std::optional<std::string> _State;
32 
33 public:
34  typedef std::pair<bool, std::string> Pair;
35 
36  /// Construct \c true.
37  SdfAllowed() { }
38  /// Construct \c true.
39  SdfAllowed(bool x) { TF_AXIOM(x); }
40  /// Construct \c false with annotation \p whyNot.
41  SdfAllowed(const char* whyNot) : _state(std::string(whyNot)) { }
42  /// Construct \c false with annotation \p whyNot.
43  SdfAllowed(const std::string& whyNot) : _state(whyNot) { }
44  /// Construct in \p condition with annotation \p whyNot if \c false.
45  SdfAllowed(bool condition, const char* whyNot) :
46  SdfAllowed(condition, std::string(whyNot)) { }
47  /// Construct in \p condition with annotation \p whyNot if \c false.
48  SdfAllowed(bool condition, const std::string& whyNot) :
49  _state(condition ? std::nullopt :
50  std::make_optional(whyNot)) { }
51  /// Construct from bool,string pair \p x.
52  SdfAllowed(const Pair& x) : SdfAllowed(x.first, x.second) { }
54 
55 #if !defined(doxygen)
56  typedef _State SdfAllowed::*UnspecifiedBoolType;
57 #endif
58 
59  /// Returns \c true in a boolean context if allowed, \c false otherwise.
60  operator UnspecifiedBoolType() const
61  {
62  return _state ? NULL : &SdfAllowed::_state;
63  }
64 
65  /// Returns \c false in a boolean context if allowed, \c true otherwise.
66  bool operator!() const
67  {
68  return static_cast<bool>(_state);
69  }
70 
71  /// Returns the reason why the operation is not allowed. If the
72  /// operation is allowed this returns the empty string.
73  operator const std::string&() const
74  {
75  return GetWhyNot();
76  }
77 
78  /// Returns the reason why the operation is not allowed. If the
79  /// operation is allowed this returns the empty string.
80  SDF_API const std::string& GetWhyNot() const;
81 
82  /// Returns \c true if allowed, otherwise fills \p whyNot if not \c NULL
83  /// and returns \c false.
84  bool IsAllowed(std::string* whyNot) const
85  {
86  if (whyNot && _state) {
87  *whyNot = *_state;
88  }
89  return !_state;
90  }
91 
92  /// Compare to \p other. Returns \c true if both are \c true or
93  /// both are \c false and reasons why not are identical.
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 
104 private:
105  _State _state;
106 };
107 
109 
110 #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:45
bool operator!() const
Returns false in a boolean context if allowed, true otherwise.
Definition: allowed.h:66
bool IsAllowed(std::string *whyNot) const
Definition: allowed.h:84
bool operator==(const SdfAllowed &other) const
Definition: allowed.h:94
std::pair< bool, std::string > Pair
Definition: allowed.h:34
SdfAllowed(bool condition, const std::string &whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:48
SDF_API const std::string & GetWhyNot() const
SdfAllowed(const std::string &whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:43
SdfAllowed(bool x)
Construct true.
Definition: allowed.h:39
GLint GLenum GLint x
Definition: glcorearb.h:409
SdfAllowed(const char *whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:41
#define SDF_API
Definition: api.h:23
#define TF_AXIOM(cond)
SdfAllowed()
Construct true.
Definition: allowed.h:37
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
~SdfAllowed()
Definition: allowed.h:53
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
bool operator!=(const SdfAllowed &other) const
Definition: allowed.h:99
SdfAllowed(const Pair &x)
Construct from bool,string pair x.
Definition: allowed.h:52
_State SdfAllowed::* UnspecifiedBoolType
Definition: allowed.h:56