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_StringHolder::theSentinel)
31  , myLabel(UT_StringHolder::theSentinel)
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(thetoken
45  ? UT_StringHolder(UT_StringHolder::REFERENCE, thetoken)
46  : UT_StringHolder::theSentinel)
47  , myLabel(thelabel
48  ? UT_StringHolder(UT_StringHolder::REFERENCE, thelabel)
49  : UT_StringHolder::theSentinel)
50  , myFlags(theflags)
51  {
52  }
53 
54  enum PRM_NameCopy { COPY };
55 
56  /// Constructor for doing deep copies of the strings
58  const UT_StringHolder &thetoken,
59  const UT_StringHolder &thelabel,
60  int theflags = 0)
61  : myToken(thetoken)
62  , myLabel(thelabel)
63  , myFlags(theflags)
64  {
65  }
66 
67  /// Check if this is a sentinel
68  bool isSentinel() const
69  {
70  return myToken.isSentinel();
71  }
72 
73  /// Set to a sentinel value
75  {
78  }
79 
80  /// Set to a separator value
81  void setAsSeparator();
82 
83  const char *getToken() const
84  {
85  return myToken.isSentinel() ? nullptr : myToken.c_str();
86  }
87  const char *getLabel() const
88  {
89  return myLabel.isSentinel() ? getToken() : myLabel.c_str();
90  }
91  int getExpressionFlag() const { return myFlags; }
92  unsigned getHash() const { return myToken.hash(); }
93 
94  /// Use this for hash tables, might be the sentinel value
95  const UT_StringRef &getTokenRef() const { return myToken; }
96 
97  /// Get label UT_StringRef &, might be the sentinel value
98  const UT_StringRef &getLabelRef() const { return myLabel; }
99 
100  /// Do a deep copy of its internal references
101  void harden()
102  {
103  if (!myToken.isSentinel())
104  myToken = UT_StringHolder(myToken);
105  if (!myLabel.isSentinel())
106  myLabel = UT_StringHolder(myLabel);
107  }
108  /// Sets the token, doing a deep copy
109  void setToken(const char *s)
110  {
111  myToken = s ? UT_StringHolder(s)
113  }
114  /// Sets the label, doing a deep copy
115  void setLabel(const char *s)
116  {
117  myLabel = s ? UT_StringHolder(s)
119  }
120  /// Sets the token and label, doing deep copies
121  void setTokenAndLabel(const char *token, const char *label)
122  {
123  setToken(token);
124  setLabel(label);
125  }
126  /// Sets the token and label, doing shallow copies.
127  /// @note There's no way to use this method to set sentinels, use
128  /// setTokenAndLabel(nullptr, nullptr) instead.
130  const UT_StringRef &label)
131  {
132  myToken = token;
133  myLabel = label;
134  }
135 
136  void instance(const int *instance, int num);
137  void instance(const UT_IntArray &instance_num);
138 
139  static void instanceToken(UT_String &token, const int *instance, int num);
140  static void instanceToken(UT_String &token,
141  const UT_IntArray &instance_num);
142  static void instanceToken(UT_WorkBuffer &result, const UT_StringRef &token,
143  const int *instance, int num);
144  static void instanceToken(UT_WorkBuffer &result, const UT_StringRef &token,
145  const UT_IntArray &instance_num);
146 
147  /// Returns if this a valid menu choice
148  bool isValidChoice() const
149  {
150  if (isSentinel())
151  return false;
152  return myToken.isstring() || myLabel.isstring();
153  }
154 
155  int64 getMemoryUsage(bool inclusive) const
156  {
157  exint mem = inclusive ? sizeof(*this) : 0;
158  mem += myToken.getMemoryUsage(false);
159  mem += myLabel.getMemoryUsage(false);
160  return mem;
161  }
162 
163  static const char *const mySeparator;
164 
165 private:
166  UT_StringRef myToken;
167  UT_StringRef myLabel;
168  int myFlags;
169 };
170 
171 // Overload for custom formatting of PRM_Name with UTformat.
172 PRM_API size_t format(char *buffer, size_t buffer_size, const PRM_Name &v);
173 
174 #endif
const UT_StringRef & getLabelRef() const
Get label UT_StringRef &, might be the sentinel value.
Definition: PRM_Name.h:98
PRM_Name()
Default PRM_Name constructor is a sentinel value.
Definition: PRM_Name.h:29
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2544
int64 getMemoryUsage(bool inclusive) const
Definition: PRM_Name.h:155
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)
Definition: PRM_Name.h:129
unsigned getHash() const
Definition: PRM_Name.h:92
const GLdouble * v
Definition: glcorearb.h:836
void setToken(const char *s)
Sets the token, doing a deep copy.
Definition: PRM_Name.h:109
const char * getLabel() const
Definition: PRM_Name.h:87
GLuint buffer
Definition: glcorearb.h:659
static const char *const mySeparator
Definition: PRM_Name.h:163
long long int64
Definition: SYS_Types.h:107
PRM_NameCopy
Definition: PRM_Name.h:54
const UT_StringRef & getTokenRef() const
Use this for hash tables, might be the sentinel value.
Definition: PRM_Name.h:95
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:57
PRM_API size_t format(char *buffer, size_t buffer_size, const PRM_Name &v)
int64 exint
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:121
png_infop png_bytep png_size_t buffer_size
Definition: png.h:2124
void harden()
Do a deep copy of its internal references.
Definition: PRM_Name.h:101
int getExpressionFlag() const
Definition: PRM_Name.h:91
png_infop png_uint_32 int num
Definition: png.h:2158
bool isValidChoice() const
Returns if this a valid menu choice.
Definition: PRM_Name.h:148
void setAsSentinel()
Set to a sentinel value.
Definition: PRM_Name.h:74
bool isSentinel() const
Check if this is a sentinel.
Definition: PRM_Name.h:68
#define PRM_API
Definition: PRM_API.h:10
const char * getToken() const
Definition: PRM_Name.h:83
static const UT_StringHolder theSentinel
void setLabel(const char *s)
Sets the label, doing a deep copy.
Definition: PRM_Name.h:115