ai_string.h
Go to the documentation of this file.
1 // Copyright 2020 Autodesk, Inc. All rights reserved.
2 //
3 // Use of this software is subject to the terms of the Autodesk license
4 // agreement provided at the time of installation or download, or which
5 // otherwise accompanies this software in either electronic or hard copy form.
6 
12 #pragma once
13 #include "ai_api.h"
14 
15 #include <stddef.h>
16 
18 // Do not use these functions directly. Instead use the AtString class
20 AI_API const char* AiCreateAtStringData_private(const char*);
21 AI_API AI_PURE size_t AiAtStringLength(const char*);
22 AI_API AI_PURE AI_DEVICE size_t AiAtStringHash(const char*);
25 
46 class AtString {
47 public:
48 #ifdef AI_CPU_COMPILER
49  AtString() : data(NULL) { }
50 #else
51  AI_DEVICE AtString() { }
52 #endif
53 
60  explicit AtString(const char* str) { data = AiCreateAtStringData_private(str); }
61 
65  AI_DEVICE bool operator== (const AtString &rhs) const { return data == rhs.data; }
66  AI_DEVICE bool operator!= (const AtString &rhs) const { return data != rhs.data; }
70  size_t length() const { return AiAtStringLength(data); }
71 
75 #ifdef AI_CPU_COMPILER
76  bool empty() const { return length() == 0; }
77 #else
78  AI_DEVICE bool empty() const;
79 #endif
80 
95  operator const char*() const { return c_str(); }
101  AI_DEVICE const char* c_str() const { return data; }
102 
103  AI_DEVICE void clear() { data = NULL; }
104 
110  AI_DEVICE size_t hash() const { return AiAtStringHash(data); }
111 
112 private:
113  const char* data;
115  friend void setAtStringInternalData(AtString &str, const char* data);
116 
117  // Comparing a char* with an AtString is not allowed since this would
118  // require using a strcmp or casting the char* to AtString. Both of these
119  // are slow and users expect comparisons with AtString to be fast. Since it
120  // is too easy to accidentally do this slow comparison without realizing
121  // it's slow, we make this a compiler error. Note these are not defined on
122  // purpose so that using these functions will cause a linker error.
123  bool operator== (const char* rhs) const;
124  bool operator!= (const char* rhs) const;
125  AI_UNAVAILABLE friend inline bool operator==(const char* lhs, const AtString& rhs);
126  AI_UNAVAILABLE friend inline bool operator!=(const char* lhs, const AtString& rhs);
128 };
129 
135 {
136 public:
137  size_t operator() (const AtString &s) const { return s.hash(); }
138 };
bool empty() const
Returns true if underlying char* is NULL or "".
Definition: ai_string.h:76
Arnold String allows for fast string comparisons.
Definition: ai_string.h:46
size_t length() const
Computing the length is a fast constant time operation.
Definition: ai_string.h:70
AI_DEVICE size_t hash() const
Returns a hashed version of the string.
Definition: ai_string.h:110
Functor class to use as a hasher when you want to make a hash map or hash set using AtString as a key...
Definition: ai_string.h:134
DLL export prefix for API functions (necessary for multi-platform development)
AI_DEVICE bool operator==(const AtString &rhs) const
Comparing two AtString objects is an extremely fast pointer comparison.
Definition: ai_string.h:65
AI_DEVICE const char * c_str() const
Returns the string as a const char*.
Definition: ai_string.h:101
AtString(const char *str)
Creating an AtString from a char* is an expensive operation.
Definition: ai_string.h:60

© 2020 Autodesk, Inc. · All rights reserved · www.arnoldrenderer.com