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 <string>
34 #include <utility>
35 #include <hboost/optional.hpp>
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 hboost::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  _state(!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, whyNot) { }
67  /// Construct from bool,string pair \p x.
68  SdfAllowed(const Pair& x) : _state(!x.first, x.second) { }
70 
71 #if !defined(doxygen)
72  typedef _State SdfAllowed::*UnspecifiedBoolType;
73 #endif
74 
75  /// Returns \c true in a boolean context if allowed, \c false otherwise.
76  operator UnspecifiedBoolType() const
77  {
78  return _state ? NULL : &SdfAllowed::_state;
79  }
80 
81  /// Returns \c false in a boolean context if allowed, \c true otherwise.
82  bool operator!() const
83  {
84  return static_cast<bool>(_state);
85  }
86 
87  /// Returns the reason why the operation is not allowed. If the
88  /// operation is allowed this returns the empty string.
89  operator const std::string&() const
90  {
91  return GetWhyNot();
92  }
93 
94  /// Returns the reason why the operation is not allowed. If the
95  /// operation is allowed this returns the empty string.
96  SDF_API const std::string& GetWhyNot() const;
97 
98  /// Returns \c true if allowed, otherwise fills \p whyNot if not \c NULL
99  /// and returns \c false.
100  bool IsAllowed(std::string* whyNot) const
101  {
102  if (whyNot && _state) {
103  *whyNot = *_state;
104  }
105  return !_state;
106  }
107 
108  /// Compare to \p other. Returns \c true if both are \c true or
109  /// both are \c false and reasons why not are identical.
110  bool operator==(const SdfAllowed& other) const
111  {
112  return _state == other._state;
113  }
114 
115  bool operator!=(const SdfAllowed& other) const
116  {
117  return !(*this == other);
118  }
119 
120 private:
121  _State _state;
122 };
123 
125 
126 #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:82
bool IsAllowed(std::string *whyNot) const
Definition: allowed.h:100
bool operator==(const SdfAllowed &other) const
Definition: allowed.h:110
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:1441
~SdfAllowed()
Definition: allowed.h:69
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool operator!=(const SdfAllowed &other) const
Definition: allowed.h:115
SdfAllowed(const Pair &x)
Construct from bool,string pair x.
Definition: allowed.h:68
_State SdfAllowed::* UnspecifiedBoolType
Definition: allowed.h:72