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 SDF_ALLOWED_H
25 #define SDF_ALLOWED_H
26 
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 <boost/operators.hpp>
36 #include <boost/optional.hpp>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
47 class SdfAllowed : private boost::equality_comparable<SdfAllowed> {
48 private:
49  typedef boost::optional<std::string> _State;
50 
51 public:
52  typedef std::pair<bool, std::string> Pair;
53 
55  SdfAllowed() { }
57  SdfAllowed(bool x) { TF_AXIOM(x); }
59  SdfAllowed(const char* whyNot) : _state(std::string(whyNot)) { }
61  SdfAllowed(const std::string& whyNot) : _state(whyNot) { }
63  SdfAllowed(bool condition, const char* whyNot) :
64  _state(!condition, std::string(whyNot)) { }
66  SdfAllowed(bool condition, const std::string& whyNot) :
67  _state(!condition, whyNot) { }
69  SdfAllowed(const Pair& x) : _state(!x.first, x.second) { }
70  ~SdfAllowed() { }
71 
72 #if !defined(doxygen)
73  typedef _State SdfAllowed::*UnspecifiedBoolType;
74 #endif
75 
77  operator UnspecifiedBoolType() const
78  {
79  return _state ? NULL : &SdfAllowed::_state;
80  }
81 
83  bool operator!() const
84  {
85  return static_cast<bool>(_state);
86  }
87 
90  operator const std::string&() const
91  {
92  return GetWhyNot();
93  }
94 
97  SDF_API const std::string& GetWhyNot() const;
98 
101  bool IsAllowed(std::string* whyNot) const
102  {
103  if (whyNot && _state) {
104  *whyNot = *_state;
105  }
106  return !_state;
107  }
108 
111  bool operator==(const SdfAllowed& other) const
112  {
113  return _state == other._state;
114  }
115 
116 private:
117  _State _state;
118 };
119 
120 PXR_NAMESPACE_CLOSE_SCOPE
121 
122 #endif // SDF_ALLOWED_H
bool operator==(const SdfAllowed &other) const
Compare to other.
Definition: allowed.h:111
SdfAllowed()
Construct true.
Definition: allowed.h:55
bool operator!() const
Returns false in a boolean context if allowed, true otherwise.
Definition: allowed.h:83
SdfAllowed(const Pair &x)
Construct from bool,string pair x.
Definition: allowed.h:69
SDF_API const std::string & GetWhyNot() const
Returns the reason why the operation is not allowed.
Indicates if an operation is allowed and, if not, why not.
Definition: allowed.h:47
SdfAllowed(bool x)
Construct true.
Definition: allowed.h:57
SdfAllowed(bool condition, const char *whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:63
SdfAllowed(const char *whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:59
bool IsAllowed(std::string *whyNot) const
Returns true if allowed, otherwise fills whyNot if not NULL and returns false.
Definition: allowed.h:101
#define TF_AXIOM(cond)
Aborts if the condition cond is not met.
Definition: diagnostic.h:215
SdfAllowed(const std::string &whyNot)
Construct false with annotation whyNot.
Definition: allowed.h:61
SdfAllowed(bool condition, const std::string &whyNot)
Construct in condition with annotation whyNot if false.
Definition: allowed.h:66