HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PRM_Name.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: PRM_Name.h (Parameter Library)
7  *
8  * COMMENTS:
9  * This class is used to store names of things which may have
10  * a nice "label" name, and a "token" (more parsable) name.
11  */
12 
13 #ifndef __PRM_Label__
14 #define __PRM_Label__
15 
16 #include "PRM_API.h"
17 #include <UT/UT_Assert.h>
18 #include <UT/UT_IntArray.h>
19 #include <UT/UT_StringHolder.h>
20 #include <SYS/SYS_Types.h>
21 
22 class UT_WorkBuffer;
23 
25 {
26 public:
27 
28  /// Default PRM_Name constructor is a sentinel value
30  : myToken(UT_StringRef::SENTINEL)
31  , myLabel(UT_StringRef::SENTINEL)
32  , myFlags(0)
33  {
34  }
35 
36  /// If thelabel is set to 0, then the thetoken is used as
37  /// the label. Currently the flags parameter is only used for
38  /// the parm expression flag. Set theflags to 1 to make the parm
39  /// default to expression mode.
40  /// @note This constructor will only REFERENCE the given strings
41  explicit PRM_Name(const char *thetoken,
42  const char *thelabel = nullptr,
43  int theflags = 0)
44  : myToken(UT_StringRef::SENTINEL, thetoken)
45  , myLabel(UT_StringRef::SENTINEL, thelabel)
46  , myFlags(theflags)
47  {
48  }
49 
50  enum PRM_NameCopy { COPY };
51 
52  /// Constructor for doing deep copies of the strings
54  const UT_StringHolder &thetoken,
55  const UT_StringHolder &thelabel,
56  int theflags = 0)
57  : myToken(thetoken)
58  , myLabel(thelabel)
59  , myFlags(theflags)
60  {
61  }
62 
63  /// Check if this is a sentinel
64  bool isSentinel() const
65  {
66  return myToken.isSentinel();
67  }
68 
69  /// Set to a sentinel value
71  {
72  myToken.makeSentinel();
73  myLabel.makeSentinel();
74  }
75 
76  /// Set to a separator value
77  void setAsSeparator();
78 
79  const char *getToken() const
80  {
81  return myToken.isSentinel() ? nullptr : myToken.c_str();
82  }
83  const char *getLabel() const
84  {
85  return myLabel.isSentinel() ? getToken() : myLabel.c_str();
86  }
87  int getExpressionFlag() const { return myFlags; }
88  unsigned getHash() const { return myToken.hash(); }
89 
90  /// Use this for the common use of passing the token to a function
91  operator const UT_StringRef&() const { return myToken; }
92 
93  /// Use this for hash tables, might be the sentinel value
94  const UT_StringRef &getTokenRef() const { return myToken; }
95 
96  /// Get label UT_StringRef &, might be the sentinel value
97  const UT_StringRef &getLabelRef() const { return myLabel; }
98 
99  /// Do a deep copy of its internal references
100  void harden()
101  {
102  myToken = UT_StringHolder(myToken);
103  myLabel = UT_StringHolder(myLabel);
104  }
105  /// Sets the token, doing a deep copy
106  void setToken(const char *s)
107  {
108  if (s)
109  myToken.harden(s);
110  else
111  myToken.makeSentinel();
112  }
113  /// Sets the label, doing a deep copy
114  void setLabel(const char *s)
115  {
116  if (s)
117  myLabel.harden(s);
118  else
119  myLabel.makeSentinel();
120  }
121  /// Sets the token and label, doing deep copies
122  void setTokenAndLabel(const char *token, const char *label)
123  {
124  setToken(token);
125  setLabel(label);
126  }
127  /// Sets the token and label, doing shallow copies.
129  const UT_StringRef &label)
130  {
131  myToken = token;
132  myLabel = label;
133  }
134 
135  void instance(const int *instance, int num);
136  void instance(const UT_IntArray &instance_num);
137 
138  static void instanceToken(UT_String &token, const int *instance, int num);
139  static void instanceToken(UT_String &token,
140  const UT_IntArray &instance_num);
141  static void instanceToken(UT_WorkBuffer &result, const UT_StringRef &token,
142  const int *instance, int num);
143  static void instanceToken(UT_WorkBuffer &result, const UT_StringRef &token,
144  const UT_IntArray &instance_num);
145 
146  /// Returns if this a valid menu choice
147  bool isValidChoice() const
148  {
149  if (isSentinel())
150  return false;
151  return myToken.isstring() || myLabel.isstring();
152  }
153 
154  int64 getMemoryUsage(bool inclusive) const
155  {
156  exint mem = inclusive ? sizeof(*this) : 0;
157  mem += myToken.getMemoryUsage(false);
158  mem += myLabel.getMemoryUsage(false);
159  return mem;
160  }
161 
162  static const char *const mySeparator;
163 
164 private:
165  UT_StringRef myToken;
166  UT_StringRef myLabel;
167  int myFlags;
168 };
169 
170 // Overload for custom formatting of PRM_Name with UTformat.
171 PRM_API size_t format(char *buffer, size_t buffer_size, const PRM_Name &v);
172 
173 #endif
const UT_StringRef & getLabelRef() const
Get label UT_StringRef &, might be the sentinel value.
Definition: PRM_Name.h:97
PRM_Name()
Default PRM_Name constructor is a sentinel value.
Definition: PRM_Name.h:29
int64 getMemoryUsage(bool inclusive) const
Definition: PRM_Name.h:154
PRM_Name(const char *thetoken, const char *thelabel=nullptr, int theflags=0)
Definition: PRM_Name.h:41
void setTokenAndLabelRef(const UT_StringRef &token, const UT_StringRef &label)
Sets the token and label, doing shallow copies.
Definition: PRM_Name.h:128
unsigned getHash() const
Definition: PRM_Name.h:88
void setToken(const char *s)
Sets the token, doing a deep copy.
Definition: PRM_Name.h:106
int64 exint
Definition: SYS_Types.h:125
const char * getLabel() const
Definition: PRM_Name.h:83
static const char *const mySeparator
Definition: PRM_Name.h:162
GLuint64EXT * result
Definition: glew.h:14311
Definition: core.h:760
PRM_NameCopy
Definition: PRM_Name.h:50
const UT_StringRef & getTokenRef() const
Use this for hash tables, might be the sentinel value.
Definition: PRM_Name.h:94
PRM_Name(PRM_NameCopy, const UT_StringHolder &thetoken, const UT_StringHolder &thelabel, int theflags=0)
Constructor for doing deep copies of the strings.
Definition: PRM_Name.h:53
PRM_API size_t format(char *buffer, size_t buffer_size, const PRM_Name &v)
const GLdouble * v
Definition: glcorearb.h:837
GLuint num
Definition: glew.h:2695
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
long long int64
Definition: SYS_Types.h:116
void setTokenAndLabel(const char *token, const char *label)
Sets the token and label, doing deep copies.
Definition: PRM_Name.h:122
void harden()
Do a deep copy of its internal references.
Definition: PRM_Name.h:100
int getExpressionFlag() const
Definition: PRM_Name.h:87
bool isValidChoice() const
Returns if this a valid menu choice.
Definition: PRM_Name.h:147
void setAsSentinel()
Set to a sentinel value.
Definition: PRM_Name.h:70
bool isSentinel() const
Check if this is a sentinel.
Definition: PRM_Name.h:64
#define PRM_API
Definition: PRM_API.h:10
GLdouble s
Definition: glew.h:1395
const char * getToken() const
Definition: PRM_Name.h:79
void setLabel(const char *s)
Sets the label, doing a deep copy.
Definition: PRM_Name.h:114