HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UN_Utils.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: UN_Utils.h ( UN Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UN_Utils_h__
13 #define __UN_Utils_h__
14 
15 #include "UN_API.h"
16 #include <UT/UT_String.h>
17 #include <UT/UT_StringHolder.h>
18 
19 /// A namespace for UN library utility functions.
20 namespace UN_Utils
21 {
22  /// @{ Returns true if the given name is a valid C-like variable.
23  /// Ie, contains only aplphanumeric or underscore charactedrs,
24  /// but does not start with a numeric character. Ohterwise, returns false.
25  /// @p safe_chars - other additional (non-alphanumeric) characters
26  /// that are allowed in the name.
27  static inline bool
28  isValidName( const UT_StringRef &name,
29  const UT_StringRef &safe_chars = UT_StringRef() )
30  {
31  // TODO: FIXME: including test for `!name` to be compatible
32  // with the original call to isValidVariable() without safe chars,
33  // which accepts an empty string as a valid name (bug?).
34  // A call with safe chars rejects an empty name.
35  // Empty strings arise for the root node.
36  // Revisit this and add specific validation exeptions
37  // for the root node only.
38  return !name || UT_StringWrap(name).isValidVariableName(safe_chars);
39  }
40 
41  static inline bool isValidCategoryName( const UT_StringRef &name )
42  { return isValidName(name); }
43  static inline bool isValidTypeName( const UT_StringRef &name )
44  { return isValidName(name); }
45  UN_API bool isValidNodeTypeName( const UT_StringRef &name,
46  const UT_StringRef &safe_chars = UT_StringRef());
47  static inline bool isValidPortTypeName( const UT_StringRef &name,
48  const UT_StringRef &safe_chars = UT_StringRef())
49  // TODO: FIXME: see the todo comment above about name
50  { return name && isValidName(name, safe_chars); }
51  static inline bool isValidParmTypeName( const UT_StringRef &name )
52  { return isValidTypeName(name); }
53 
54  static inline bool isValidGraphItemName( const UT_StringRef &name,
55  const UT_StringRef &safe_chars = UT_StringRef())
56  { return isValidName(name, safe_chars); }
57  static inline bool isValidNodeName( const UT_StringRef &name,
58  const UT_StringRef &safe_chars = UT_StringRef())
59  { return isValidGraphItemName(name, safe_chars); }
60  static inline bool isValidPortName( const UT_StringRef &name )
61  { return isValidName(name); }
62  static inline bool isValidInputName( const UT_StringRef &name )
63  { return isValidPortName(name); }
64  static inline bool isValidOutputName( const UT_StringRef &name )
65  { return isValidPortName(name); }
66  static inline bool isValidParmName( const UT_StringRef &name )
67  { return isValidName(name); }
68  /// @}
69 
70 
71  /// @{ Converts the given name to a one containing only valid characters.
72  /// @p safe_chars - other additional (non-alphanumeric) characters
73  /// that are allowed in the name.
74  static inline UT_StringHolder
75  makeValidName( const UT_StringRef &name,
76  const UT_StringRef &safe_chars = UT_StringRef())
77  {
78  return UT_StringHolder(name).forceValidVariableName(safe_chars);
79  }
80 
81  static inline UT_StringHolder
82  makeValidCategoryName( const UT_StringRef &name )
83  {
84  return makeValidName(name);
85  }
86 
87  static inline UT_StringHolder
88  makeValidGraphItemName( const UT_StringRef &name,
89  const UT_StringRef &safe_chars = UT_StringRef())
90  {
91  return makeValidName(name, safe_chars);
92  }
93 
94  static inline UT_StringHolder
95  makeValidPortName( const UT_StringRef &name )
96  {
97  return makeValidName(name);
98  }
99  /// @}
100 
101 
102  /// @{ Returns a valid name, converting the given one if it is not valid.
103  static inline UT_StringHolder
104  ensureValidCategoryName( const UT_StringRef &name )
105  {
106  return isValidCategoryName( name )
107  ? name : makeValidCategoryName( name );
108  }
109  /// @}
110 
111 
112 }
113 
114 #endif
115 
bool isValidVariableName(const char *safechars=nullptr) const
#define UN_API
Definition: UN_API.h:11
GLuint const GLchar * name
Definition: glcorearb.h:786
UN_API bool isValidNodeTypeName(const UT_StringRef &name, const UT_StringRef &safe_chars=UT_StringRef())
SYS_NO_DISCARD_RESULT UT_StringRef forceValidVariableName(const char *safechars=nullptr) const
MX_CORE_API bool isValidName(const string &name)
Return true if the given string is a valid MaterialX name.