OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreIdString.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #ifndef __OgreIdString__
30 #define __OgreIdString__
31 
32 #include "Hash/MurmurHash3.h"
33 #include <stdio.h> // sprintf
34 #include <string.h> // strlen
35 #include <string>
36 
37 #define OGRE_HASH_FUNC MurmurHash3_x86_32
38 #define OGRE_HASH_BITS 32
39 
40 #ifdef NDEBUG
41  #define OGRE_COPY_DEBUG_STRING( _Expression ) ((void)0)
42  #define OGRE_APPEND_DEBUG_STRING( _Expression ) ((void)0)
43 #else
44  #include "assert.h"
45 #endif
46 
47 namespace Ogre
48 {
94  struct IdString
95  {
96  static const uint32_t Seed = 0x3A8EFA67; //It's a prime number :)
97 
99 #ifndef NDEBUG
100  #define OGRE_DEBUG_STR_SIZE 32
102 #endif
103 
104  IdString() : mHash( 0 )
105  {
106 #ifndef NDEBUG
107  mDebugString[0] = '\0';
108 #endif
109  }
110 
111  IdString( const char *string ) : mHash( 0 )
112  {
113  OGRE_HASH_FUNC( string, static_cast<int>(strlen( string )), Seed, &mHash );
114  OGRE_COPY_DEBUG_STRING( string );
115  }
116 
117  IdString( const std::string &string ) : mHash( 0 )
118  {
119  OGRE_HASH_FUNC( string.c_str(), static_cast<int>(string.size()), Seed, &mHash );
120  OGRE_COPY_DEBUG_STRING( string );
121  }
122 
123  IdString( uint32 value ) : mHash( 0 )
124  {
125  OGRE_HASH_FUNC( &value, sizeof( value ), Seed, &mHash );
126  OGRE_COPY_DEBUG_STRING( value );
127  }
128 
129 #ifndef NDEBUG
130  #if OGRE_COMPILER == OGRE_COMPILER_MSVC
131  #pragma warning( push )
132  #pragma warning( disable: 4996 ) //Unsecure CRT deprecation warning
133  #endif
134 
135  void OGRE_COPY_DEBUG_STRING( const char *string )
136  {
137  size_t strLength = strlen( string );
138  if( strLength > OGRE_DEBUG_STR_SIZE-1 )
139  {
140  //Copy the last characters, not the first ones!
141  strncpy( mDebugString, string + strLength - (OGRE_DEBUG_STR_SIZE-1),
143  }
144  else
145  {
146  strncpy( mDebugString, string, OGRE_DEBUG_STR_SIZE );
147  }
149  }
150 
151  void OGRE_COPY_DEBUG_STRING( const std::string &string )
152  {
153  size_t strLength = string.size();
154  if( strLength > OGRE_DEBUG_STR_SIZE-1 )
155  {
156  //Copy the last characters, not the first ones!
157  strncpy( mDebugString, string.c_str() + strLength - (OGRE_DEBUG_STR_SIZE-1),
159  }
160  else
161  {
162  strncpy( mDebugString, string.c_str(), OGRE_DEBUG_STR_SIZE );
163  }
165  }
166 
168  {
169  sprintf( mDebugString, "[Value 0x%.8x]", value );
171  }
172 
173  void OGRE_APPEND_DEBUG_STRING( const char *string )
174  {
175  size_t strLen0 = strlen( mDebugString );
176  size_t strLen1 = strlen( string );
177 
178  if( strLen0 + strLen1 < OGRE_DEBUG_STR_SIZE )
179  {
180  strcat( mDebugString, string );
182  }
183  else
184  {
185  size_t newStart0 = (strLen0 >> 1);
186  size_t newLen0 = strLen0 - newStart0;
187  memmove( mDebugString, mDebugString + newStart0, newLen0 );
188 
189  size_t newStart1 = 0;
190  size_t newLen1 = strLen1;
191  if( newLen0 + strLen1 >= OGRE_DEBUG_STR_SIZE )
192  {
193  newLen1 = OGRE_DEBUG_STR_SIZE - newLen0 - 1;
194  newStart1 = strLen1 - newLen1;
195  }
196 
197  memcpy( mDebugString + newLen0, string + newStart1, newLen1 );
199  }
200  }
201 
202  #if OGRE_COMPILER == OGRE_COMPILER_MSVC
203  #pragma warning( pop )
204  #endif
205 #endif
206 
207  void operator += ( IdString idString )
208  {
209  uint32 doubleHash[2];
210  doubleHash[0] = mHash;
211  doubleHash[1] = idString.mHash;
212 
213  OGRE_HASH_FUNC( &doubleHash, sizeof( doubleHash ), Seed, &mHash );
215  }
216 
217  IdString operator + ( IdString idString ) const
218  {
219  IdString retVal( *this );
220  retVal += idString;
221  return retVal;
222  }
223 
224  bool operator < ( IdString idString ) const
225  {
226 #if OGRE_DEBUG_MODE
227  //On highly debug builds, check for collisions
228  assert( !(mHash == idString.mHash &&
229  strcmp( mDebugString, idString.mDebugString ) != 0) &&
230  "Collision detected!" );
231 #endif
232  return mHash < idString.mHash;
233  }
234 
235  bool operator == ( IdString idString ) const
236  {
237 #ifndef NDEBUG
238  assert( !(mHash == idString.mHash &&
239  strcmp( mDebugString, idString.mDebugString ) != 0) &&
240  "Collision detected!" );
241 #endif
242  return mHash == idString.mHash;
243  }
244 
245  bool operator != ( IdString idString ) const
246  {
247 #ifndef NDEBUG
248  assert( !(mHash == idString.mHash &&
249  strcmp( mDebugString, idString.mDebugString ) != 0) &&
250  "Collision detected!" );
251 #endif
252  return mHash != idString.mHash;
253  }
254 
256  std::string getFriendlyText() const
257  {
258 #ifndef NDEBUG
259  return std::string( mDebugString );
260 #else
261  #if OGRE_COMPILER == OGRE_COMPILER_MSVC
262  #pragma warning( push )
263  #pragma warning( disable: 4996 ) //Unsecure CRT deprecation warning
264  #endif
265 
266  char tmp[(OGRE_HASH_BITS >> 2)+10];
267  sprintf( tmp, "[Hash 0x%.8x]", mHash );
268  tmp[(OGRE_HASH_BITS >> 2)+10-1] = '\0';
269  return std::string( tmp );
270 
271  #if OGRE_COMPILER == OGRE_COMPILER_MSVC
272  #pragma warning( pop )
273  #endif
274 #endif
275  }
276 
278  std::string getReleaseText() const
279  {
280  #if OGRE_COMPILER == OGRE_COMPILER_MSVC
281  #pragma warning( push )
282  #pragma warning( disable: 4996 ) //Unsecure CRT deprecation warning
283  #endif
284 
285  char tmp[(OGRE_HASH_BITS >> 2)+10];
286  sprintf( tmp, "[Hash 0x%.8x]", mHash );
287  tmp[(OGRE_HASH_BITS >> 2)+10-1] = '\0';
288  return std::string( tmp );
289 
290  #if OGRE_COMPILER == OGRE_COMPILER_MSVC
291  #pragma warning( pop )
292  #endif
293  }
294  };
295 }
296 
297 #endif
unsigned int uint32
Definition: OgrePlatform.h:420
void OGRE_COPY_DEBUG_STRING(uint32 value)
Definition: OgreIdString.h:167
#define OGRE_DEBUG_STR_SIZE
Definition: OgreIdString.h:100
std::string getFriendlyText() const
Returns "[Hash 0x0a0100ef]" strings in Release mode, readable string in debug.
Definition: OgreIdString.h:256
IdString(uint32 value)
Definition: OgreIdString.h:123
bool operator!=(IdString idString) const
Definition: OgreIdString.h:245
static const uint32_t Seed
Definition: OgreIdString.h:96
bool operator==(IdString idString) const
Definition: OgreIdString.h:235
#define OGRE_HASH_FUNC
Definition: OgreIdString.h:37
bool operator<(IdString idString) const
Definition: OgreIdString.h:224
Hashed string.
Definition: OgreIdString.h:94
char mDebugString[OGRE_DEBUG_STR_SIZE]
Definition: OgreIdString.h:101
void OGRE_COPY_DEBUG_STRING(const char *string)
Definition: OgreIdString.h:135
IdString operator+(IdString idString) const
Definition: OgreIdString.h:217
void OGRE_APPEND_DEBUG_STRING(const char *string)
Definition: OgreIdString.h:173
void OGRE_COPY_DEBUG_STRING(const std::string &string)
Definition: OgreIdString.h:151
IdString(const char *string)
Definition: OgreIdString.h:111
std::string getReleaseText() const
Always returns "[Hash 0x0a0100ef]" strings in any mode.
Definition: OgreIdString.h:278
#define OGRE_HASH_BITS
Definition: OgreIdString.h:38
IdString(const std::string &string)
Definition: OgreIdString.h:117
void operator+=(IdString idString)
Definition: OgreIdString.h:207