HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scopeDescription.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_BASE_TF_SCOPE_DESCRIPTION_H
25 #define PXR_BASE_TF_SCOPE_DESCRIPTION_H
26 
27 #include "pxr/pxr.h"
28 
32 #include "pxr/base/tf/api.h"
33 
34 #include <hboost/optional.hpp>
35 
36 #include <vector>
37 #include <string>
38 
40 
41 /// \class TfScopeDescription
42 ///
43 /// This class is used to provide high-level descriptions about scopes of
44 /// execution that could possibly block, or to provide relevant information
45 /// about high-level action that would be useful in a crash report.
46 ///
47 /// This class is reasonably fast to use, especially if the message strings are
48 /// not dynamically created, however it should not be used in very highly
49 /// performance sensitive contexts. The cost to push & pop is essentially a TLS
50 /// lookup plus a couple of atomic operations.
52 {
53  TfScopeDescription() = delete;
54  TfScopeDescription(TfScopeDescription const &) = delete;
55  TfScopeDescription &operator=(TfScopeDescription const &) = delete;
56 public:
57  /// Construct with a description. Push \a description on the stack of
58  /// descriptions for this thread. Caller guarantees that the string
59  /// \p description lives at least as long as this TfScopeDescription object.
60  TF_API explicit
61  TfScopeDescription(std::string const &description,
62  TfCallContext const &context = TfCallContext());
63 
64  /// Construct with a description. Push \a description on the stack of
65  /// descriptions for this thread. This object adopts ownership of the
66  /// rvalue \p description.
67  TF_API explicit
68  TfScopeDescription(std::string &&description,
69  TfCallContext const &context = TfCallContext());
70 
71  /// Construct with a description. Push \a description on the stack of
72  /// descriptions for this thread. Caller guarantees that the string
73  /// \p description lives at least as long as this TfScopeDescription object.
74  TF_API explicit
75  TfScopeDescription(char const *description,
76  TfCallContext const &context = TfCallContext());
77 
78  /// Destructor.
79  /// Pop the description stack in this thread.
81 
82  /// Replace the description stack entry for this scope description. Caller
83  /// guarantees that the string \p description lives at least as long as this
84  /// TfScopeDescription object.
85  TF_API void SetDescription(std::string const &description);
86 
87  /// Replace the description stack entry for this scope description. This
88  /// object adopts ownership of the rvalue \p description.
89  TF_API void SetDescription(std::string &&description);
90 
91  /// Replace the description stack entry for this scope description. Caller
92  /// guarantees that the string \p description lives at least as long as this
93  /// TfScopeDescription object.
94  TF_API void SetDescription(char const *description);
95 
96 private:
97  friend inline TfScopeDescription *
98  Tf_GetPreviousScopeDescription(TfScopeDescription *d) {
99  return d->_prev;
100  }
101  friend inline char const *
102  Tf_GetScopeDescriptionText(TfScopeDescription *d) {
103  return d->_description;
104  }
105  friend inline TfCallContext const &
106  Tf_GetScopeDescriptionContext(TfScopeDescription *d) {
107  return d->_context;
108  }
109 
110  inline void _Push();
111  inline void _Pop() const;
112 
113  hboost::optional<std::string> _ownedString;
114  char const *_description;
115  TfCallContext _context;
116  void *_localStack;
117  TfScopeDescription *_prev; // link to parent scope.
118 };
119 
120 /// Return a copy of the current description stack for the "main" thread as
121 /// identified by ArchGetMainThreadId() as a vector of strings. The most
122 /// recently pushed description is at back(), and the least recently pushed
123 /// description is at front().
124 TF_API std::vector<std::string>
126 
127 /// Return a copy of the current description stack for the current thread of
128 /// execution as a vector of strings. The most recently pushed description is
129 /// at back(), and the least recently pushed description is at front().
130 TF_API std::vector<std::string>
132 
133 /// Macro that accepts either a single string, or printf-style arguments and
134 /// creates a scope description local variable with the resulting string.
135 #define TF_DESCRIBE_SCOPE(...) \
136  TfScopeDescription __scope_description__ \
137  (Tf_DescribeScopeFormat(__VA_ARGS__), TF_CALL_CONTEXT); \
138 
139 template <typename... Args>
140 inline std::string
141 Tf_DescribeScopeFormat(const char* fmt, Args&&... args) {
142  return TfStringPrintf(fmt, std::forward<Args>(args)...);
143 }
144 
145 // If there are no formatting arguments, the string can be forwarded to the
146 // scope description constructor. In C++17, consider if std::string_view could
147 // reduce the need for as many of these overloads
148 inline const char*
149 Tf_DescribeScopeFormat(const char* fmt) { return fmt; }
150 
151 inline std::string&&
152 Tf_DescribeScopeFormat(std::string&& fmt) { return std::move(fmt); }
153 
154 inline const std::string&
155 Tf_DescribeScopeFormat(const std::string& fmt) { return fmt; }
156 
158 
159 #endif // PXR_BASE_TF_SCOPE_DESCRIPTION_H
TF_API std::string TfStringPrintf(const char *fmt,...)
TF_API void SetDescription(std::string const &description)
friend TfScopeDescription * Tf_GetPreviousScopeDescription(TfScopeDescription *d)
#define TF_API
Definition: api.h:40
TF_API ~TfScopeDescription()
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
friend TfCallContext const & Tf_GetScopeDescriptionContext(TfScopeDescription *d)
TF_API std::vector< std::string > TfGetCurrentScopeDescriptionStack()
friend char const * Tf_GetScopeDescriptionText(TfScopeDescription *d)
std::string Tf_DescribeScopeFormat(const char *fmt, Args &&...args)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
TF_API std::vector< std::string > TfGetThisThreadScopeDescriptionStack()
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
**If you just want to fire and args
Definition: thread.h:609