ai_string.h
Go to the documentation of this file.
1// Copyright 2021 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
26// Do not use these functions directly. Instead use the AtString class
28AI_API const char* AiCreateAtStringData_private(const char*);
29AI_API AI_PURE size_t AiAtStringLength(const char*);
30AI_API AI_PURE AI_DEVICE size_t AiAtStringHash(const char*);
33
54class AtString {
55public:
56 AI_DEVICE AtString() : data(NULL) { }
57
64 explicit AtString(const char* str) { data = AiCreateAtStringData_private(str); }
65
69 AI_DEVICE bool operator== (const AtString &rhs) const { return data == rhs.data; }
70 AI_DEVICE bool operator!= (const AtString &rhs) const { return data != rhs.data; }
74 size_t length() const { return AiAtStringLength(data); }
75
79#ifdef AI_CPU_COMPILER
80 bool empty() const { return length() == 0; }
81#else
82 AI_DEVICE bool empty() const;
83#endif
84
99 operator const char*() const { return c_str(); }
105 AI_DEVICE const char* c_str() const { return data; }
106
107 AI_DEVICE void clear() { data = NULL; }
108
114 AI_DEVICE size_t hash() const { return AiAtStringHash(data); }
115
116private:
117 const char* data;
119 friend void setAtStringInternalData(AtString &str, const char* data);
120
121 // Comparing a char* with an AtString is not allowed since this would
122 // require using a strcmp or casting the char* to AtString. Both of these
123 // are slow and users expect comparisons with AtString to be fast. Since it
124 // is too easy to accidentally do this slow comparison without realizing
125 // it's slow, we make this a compiler error. Note these are not defined on
126 // purpose so that using these functions will cause a linker error.
127 bool operator== (const char* rhs) const;
128 bool operator!= (const char* rhs) const;
129 AI_UNAVAILABLE friend inline bool operator==(const char* lhs, const AtString& rhs);
130 AI_UNAVAILABLE friend inline bool operator!=(const char* lhs, const AtString& rhs);
132};
133
139{
140public:
141 size_t operator() (const AtString &s) const { return s.hash(); }
142};
143/*\}*/
DLL export prefix for API functions (necessary for multi-platform development)
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:139
Arnold String allows for fast string comparisons.
Definition: ai_string.h:54
AI_DEVICE size_t hash() const
Returns a hashed version of the string.
Definition: ai_string.h:114
AI_DEVICE const char * c_str() const
Returns the string as a const char*.
Definition: ai_string.h:105
AI_DEVICE bool operator==(const AtString &rhs) const
Comparing two AtString objects is an extremely fast pointer comparison.
Definition: ai_string.h:69
AtString(const char *str)
Creating an AtString from a char* is an expensive operation.
Definition: ai_string.h:64
size_t length() const
Computing the length is a fast constant time operation.
Definition: ai_string.h:74
bool empty() const
Returns true if underlying char* is NULL or "".
Definition: ai_string.h:80

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