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 #include <hboost/preprocessor/if.hpp>
36 
37 #include <vector>
38 #include <string>
39 
41 
42 /// \class TfScopeDescription
43 ///
44 /// This class is used to provide high-level descriptions about scopes of
45 /// execution that could possibly block, or to provide relevant information
46 /// about high-level action that would be useful in a crash report.
47 ///
48 /// This class is reasonably fast to use, especially if the message strings are
49 /// not dynamically created, however it should not be used in very highly
50 /// performance sensitive contexts. The cost to push & pop is essentially a TLS
51 /// lookup plus a couple of atomic operations.
53 {
54  TfScopeDescription() = delete;
55  TfScopeDescription(TfScopeDescription const &) = delete;
56  TfScopeDescription &operator=(TfScopeDescription const &) = delete;
57 public:
58  /// Construct with a description. Push \a description on the stack of
59  /// descriptions for this thread. Caller guarantees that the string
60  /// \p description lives at least as long as this TfScopeDescription object.
61  TF_API explicit
62  TfScopeDescription(std::string const &description,
64 
65  /// Construct with a description. Push \a description on the stack of
66  /// descriptions for this thread. This object adopts ownership of the
67  /// rvalue \p description.
68  TF_API explicit
69  TfScopeDescription(std::string &&description,
71 
72  /// Construct with a description. Push \a description on the stack of
73  /// descriptions for this thread. Caller guarantees that the string
74  /// \p description lives at least as long as this TfScopeDescription object.
75  TF_API explicit
76  TfScopeDescription(char const *description,
78 
79  /// Destructor.
80  /// Pop the description stack in this thread.
82 
83  /// Replace the description stack entry for this scope description. Caller
84  /// guarantees that the string \p description lives at least as long as this
85  /// TfScopeDescription object.
86  TF_API void SetDescription(std::string const &description);
87 
88  /// Replace the description stack entry for this scope description. This
89  /// object adopts ownership of the rvalue \p description.
90  TF_API void SetDescription(std::string &&description);
91 
92  /// Replace the description stack entry for this scope description. Caller
93  /// guarantees that the string \p description lives at least as long as this
94  /// TfScopeDescription object.
95  TF_API void SetDescription(char const *description);
96 
97 private:
98  friend inline TfScopeDescription *
99  Tf_GetPreviousScopeDescription(TfScopeDescription *d) {
100  return d->_prev;
101  }
102  friend inline char const *
103  Tf_GetScopeDescriptionText(TfScopeDescription *d) {
104  return d->_description;
105  }
106  friend inline TfCallContext const &
107  Tf_GetScopeDescriptionContext(TfScopeDescription *d) {
108  return d->_context;
109  }
110 
111  inline void _Push();
112  inline void _Pop() const;
113 
114  hboost::optional<std::string> _ownedString;
115  char const *_description;
116  TfCallContext _context;
117  void *_localStack;
118  TfScopeDescription *_prev; // link to parent scope.
119 };
120 
121 /// Return a copy of the current description stack for the "main" thread as
122 /// identified by ArchGetMainThreadId() as a vector of strings. The most
123 /// recently pushed description is at back(), and the least recently pushed
124 /// description is at front().
125 TF_API std::vector<std::string>
127 
128 /// Return a copy of the current description stack for the current thread of
129 /// execution as a vector of strings. The most recently pushed description is
130 /// at back(), and the least recently pushed description is at front().
131 TF_API std::vector<std::string>
133 
134 /// Macro that accepts either a single string, or printf-style arguments and
135 /// creates a scope description local variable with the resulting string.
136 #define TF_DESCRIBE_SCOPE(fmt, ...) \
137  TfScopeDescription __scope_description__ \
138  (HBOOST_PP_IF(TF_NUM_ARGS(__VA_ARGS__), \
139  TfStringPrintf(fmt, __VA_ARGS__), fmt), TF_CALL_CONTEXT)
140 
142 
143 #endif // PXR_BASE_TF_SCOPE_DESCRIPTION_H
TF_API void SetDescription(std::string const &description)
friend TfScopeDescription * Tf_GetPreviousScopeDescription(TfScopeDescription *d)
#define TF_API
Definition: api.h:40
TF_API ~TfScopeDescription()
friend TfCallContext const & Tf_GetScopeDescriptionContext(TfScopeDescription *d)
TF_API std::vector< std::string > TfGetCurrentScopeDescriptionStack()
friend char const * Tf_GetScopeDescriptionText(TfScopeDescription *d)
GLsizei const GLchar *const * string
Definition: glew.h:1844
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
TF_API std::vector< std::string > TfGetThisThreadScopeDescriptionStack()
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91