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/operators.hpp>
36 #include <hboost/optional.hpp>
37 
39 
40 /// \class SdfAllowed
41 ///
42 /// Indicates if an operation is allowed and, if not, why not.
43 ///
44 /// A \c SdfAllowed either evaluates to \c true in a boolean context
45 /// or evaluates to \c false and has a string annotation.
46 ///
47 class SdfAllowed : private hboost::equality_comparable<SdfAllowed> {
48 private:
49  typedef hboost::optional<std::string> _State;
50 
51 public:
52  typedef std::pair<bool, std::string> Pair;
53 
54  /// Construct \c true.
55  SdfAllowed() { }
56  /// Construct \c true.
57  SdfAllowed(bool x) { TF_AXIOM(x); }
58  /// Construct \c false with annotation \p whyNot.
59  SdfAllowed(const char* whyNot) : _state(std::string(whyNot)) { }
60  /// Construct \c false with annotation \p whyNot.
61  SdfAllowed(const std::string& whyNot) : _state(whyNot) { }
62  /// Construct in \p condition with annotation \p whyNot if \c false.
63  SdfAllowed(bool condition, const char* whyNot) :
64  _state(!condition, std::string(whyNot)) { }
65  /// Construct in \p condition with annotation \p whyNot if \c false.
66  SdfAllowed(bool condition, const std::string& whyNot) :
67  _state(!condition, whyNot) { }
68  /// Construct from bool,string pair \p x.
69  SdfAllowed(const Pair& x) : _state(!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 private:
117  _State _state;
118 };
119 
121 
122 #endif // PXR_USD_SDF_ALLOWED_H
GLint first
Definition: glcorearb.h:404
SdfAllowed(bool condition, const char *whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:63
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
GLint GLenum GLint x
Definition: glcorearb.h:408
std::pair< bool, std::string > Pair
Definition: allowed.h:52
SdfAllowed(bool condition, const std::string &whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:66
SDF_API const std::string & GetWhyNot() const
SdfAllowed(const std::string &whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:61
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
SdfAllowed(bool x)
Construct true.
Definition: allowed.h:57
SdfAllowed(const char *whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:59
#define SDF_API
Definition: api.h:40
#define TF_AXIOM(cond)
SdfAllowed()
Construct true.
Definition: allowed.h:55
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
~SdfAllowed()
Definition: allowed.h:70
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GLenum condition
Definition: glew.h:13027
SdfAllowed(const Pair &x)
Construct from bool,string pair x.
Definition: allowed.h:69
_State SdfAllowed::* UnspecifiedBoolType
Definition: allowed.h:73