HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ZString.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: UT_ZString.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_ZString__
12 #define __UT_ZString__
13 
14 #include "UT_StringHolder.h"
15 #include <SYS/SYS_Hash.h>
16 
17 // Signals UT_ZString that its ingesting an already compressed string
18 namespace UT
19 {
21 }
22 
23 /// Storage to hold compressed data.
24 ///
25 /// This class will take a buffer of data and compress it. You can access the
26 /// raw compressed data using @c compressedString(), or easily get a copy of
27 /// the uncompressed data by calling @c uncompress().
28 ///
29 /// Since @c UT_StringRef can hold binary data, this is a convenient way of
30 /// compressing data that has infrequent access (or possibly never accessed).
31 /// For example: @code
32 /// void *binary_data;
33 /// UT_ZString z(UT_StringRef((const char *)binary_data, binary_size));
34 /// @endcode
35 ///
36 /// @note BLOSC compression typically works best on larger buffers with numeric
37 /// data. It's faster than GZIP compression, but has worse compression.
39 {
40 public:
41  enum Style
42  {
45  BLOSC
46  };
47 
48  /// Map the enum to a string
49  static const char *style(Style s);
50  /// Map the string to an enum value
51  static Style style(const char *s);
52 
53  UT_ZString() = default;
54 
55  /// Take an uncompressed string and store the compressed result.
56  UT_ZString(const UT_StringRef &src, Style style=GZIP, int level=9)
57  {
58  compress(src, style, level);
59  }
60  /// Set the compressed data given the original style.
61  /// If the <tt>original_size == src.length()</tt> then we assume the string
62  /// is uncompressed (unless the @c force_compressed flag is true).
63  ///
64  /// This matches the semantics of @c compress() (which stores the
65  /// uncompressed string if the compression results in a larger buffer).
68  exint original_size,
69  bool force_compressed=false)
70  {
71  setCompressedData(src, style, original_size, force_compressed);
72  }
73 
74  /// Clear storage and reset values
75  void clear()
76  {
77  myString.clear();
78  mySize = 0;
79  myStyle = GZIP;
80  }
81 
82  /// Store a string as compressed data, returning the compressed style. If
83  /// no compression is done, the returned style will be NONE.
84  ///
85  /// For GZIP compression, level can be between 1 and 9. 1 is faster, 9 is
86  /// better compression (but can be significantly slower).
87  Style compress(const UT_StringRef &s,
88  Style style=GZIP,
89  int level=1);
90 
91  /// Return the uncompressed contents
92  UT_StringHolder uncompress() const;
93 
94  /// Set the class to hold an already compressed string.
95  void setCompressedData(const UT_StringHolder &z, Style s,
96  exint original_size,
97  bool force_compressed=false);
98 
99  /// @{
100  /// Member data access
101  const UT_StringHolder &compressedString() const { return myString; }
102  Style compressedStyle() const { return myStyle; }
103  exint originalSize() const { return mySize; }
104  /// @}
105 
106  /// The z-string has a string if the original size is non-zero
108  bool isstring() const { return mySize > 0; }
109 
110  /// Test whether the string is defined or not
112  SYS_SAFE_BOOL operator bool() const { return isstring(); }
113 
114  /// @{
115  /// Standard "string" type operations.
116  int64 getMemoryUsage(bool inclusive) const
117  {
118  int64 mem = inclusive ? sizeof(*this) : 0;
119  return mem += myString.getMemoryUsage(false);
120  }
122  {
123  SYS_HashType h = myString.hash();
124  SYShashCombine(h, mySize);
125  SYShashCombine(h, myStyle);
126  return h;
127  }
128  friend std::size_t hash_value(const UT_ZString &s) { return s.hash(); }
129  bool operator==(const UT_ZString &s) const
130  {
131  return mySize == s.mySize
132  && myStyle == s.myStyle
133  && myString == s.myString;
134  }
135  /// @}
136 
137 private:
138  UT_StringHolder myString;
139  exint mySize = 0;
140  Style myStyle = NONE;
141 };
142 
143 #endif
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
int64 exint
Definition: SYS_Types.h:125
GLint level
Definition: glcorearb.h:108
GLdouble s
Definition: glad.h:3009
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
Style compressedStyle() const
Definition: UT_ZString.h:102
#define UT_API
Definition: UT_API.h:14
SYS_HashType hash() const
Definition: UT_ZString.h:121
void clear()
Clear storage and reset values.
Definition: UT_ZString.h:75
#define SYS_SAFE_BOOL
Definition: SYS_Compiler.h:55
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
long long int64
Definition: SYS_Types.h:116
const UT_StringHolder & compressedString() const
Definition: UT_ZString.h:101
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
bool operator==(const UT_ZString &s) const
Definition: UT_ZString.h:129
UT_ZString(const UT_StringRef &src, Style style=GZIP, int level=9)
Take an uncompressed string and store the compressed result.
Definition: UT_ZString.h:56
UT_ZString(const UT_StringRef &src, Style style, const UT::source_is_compressed &, exint original_size, bool force_compressed=false)
Definition: UT_ZString.h:66
exint originalSize() const
Definition: UT_ZString.h:103
friend std::size_t hash_value(const UT_ZString &s)
Definition: UT_ZString.h:128
SYS_FORCE_INLINE bool isstring() const
The z-string has a string if the original size is non-zero.
Definition: UT_ZString.h:108
GLenum src
Definition: glcorearb.h:1793
int64 getMemoryUsage(bool inclusive) const
Definition: UT_ZString.h:116