OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreRawPtr.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 __RawPtr_H__
30 #define __RawPtr_H__
31 
32 #include "OgrePlatform.h"
33 
34 //#define OGRE_RAW_PTR_PROFILE
35 
36 #ifdef OGRE_RAW_PTR_PROFILE
37  #include "OgreException.h"
38 #endif
39 
40 namespace Ogre
41 {
51  template <typename T, MemoryCategory M_CATEGORY> class RawSimdUniquePtr
52  {
54  size_t mNumElements;
55 
56  public:
58 
59  RawSimdUniquePtr( size_t numElements ) :
60  mPtr( static_cast<T * RESTRICT_ALIAS>(
61  OGRE_MALLOC_SIMD( numElements * sizeof( T ), M_CATEGORY ) ) ),
62  mNumElements( numElements )
63  {
64  }
65 
67  mPtr( 0 ),
69  {
70  if( copy.mPtr )
71  {
72 #ifdef OGRE_RAW_PTR_PROFILE
74  "Called copy constructor (performance warning)! Consider using swap "
75  "or undefine OGRE_RAW_PTR_PROFILE", "RawSimdUniquePtr::RawSimdUniquePtr" );
76 #endif
77  mPtr = static_cast<T * RESTRICT_ALIAS>(
78  OGRE_MALLOC_SIMD( copy.mNumElements * sizeof( T ), M_CATEGORY ) );
79  memcpy( mPtr, copy.mPtr, copy.mNumElements * sizeof( T ) );
80  }
81  }
82 
84  {
85  OGRE_FREE_SIMD( mPtr, M_CATEGORY );
86  mPtr = 0;
87  }
88 
89  void swap( RawSimdUniquePtr &copy )
90  {
91  std::swap( mPtr, copy.mPtr );
93  }
94 
95  void operator = ( const RawSimdUniquePtr &copy )
96  {
97  if( this != &copy )
98  {
99  OGRE_FREE_SIMD( mPtr, M_CATEGORY );
100  mPtr = 0;
101  mNumElements = copy.mNumElements;
102 
103  if( copy.mPtr )
104  {
105 #ifdef OGRE_RAW_PTR_PROFILE
107  "Called assignment operator (performance warning)! Consider using swap "
108  "or undefine OGRE_RAW_PTR_PROFILE", "RawSimdUniquePtr::RawSimdUniquePtr" );
109 #endif
110  mPtr = static_cast<T * RESTRICT_ALIAS>(
111  OGRE_MALLOC_SIMD( copy.mNumElements * sizeof( T ), M_CATEGORY ) );
112  memcpy( mPtr, copy.mPtr, copy.mNumElements * sizeof( T ) );
113  }
114  }
115  }
116 
117  T * RESTRICT_ALIAS_RETURN get() { return mPtr; }
118  const T * RESTRICT_ALIAS_RETURN get() const { return mPtr; }
119  size_t size() const { return mNumElements; }
120  };
121 }
122 
123 #endif
RawSimdUniquePtr(size_t numElements)
Definition: OgreRawPtr.h:59
#define RESTRICT_ALIAS
Definition: OgrePlatform.h:448
#define OGRE_MALLOC_SIMD(bytes, category)
Allocate a block of raw memory aligned to SIMD boundaries, and indicate the category of usage...
size_t size() const
Definition: OgreRawPtr.h:119
RawSimdUniquePtr(const RawSimdUniquePtr &copy)
Definition: OgreRawPtr.h:66
Similar to std::unique_ptr, but: Uses a custom allocator (OGRE_MALLOC_SIMD) Pointers must be really u...
Definition: OgreRawPtr.h:51
#define OGRE_EXCEPT(num, desc, src)
void operator=(const RawSimdUniquePtr &copy)
Definition: OgreRawPtr.h:95
T *RESTRICT_ALIAS mPtr
Definition: OgreRawPtr.h:53
#define OGRE_FREE_SIMD(ptr, category)
Free the memory allocated with either OGRE_MALLOC_SIMD or OGRE_ALLOC_T_SIMD. Category is required to ...
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
#define RESTRICT_ALIAS_RETURN
Definition: OgrePlatform.h:449
void swap(RawSimdUniquePtr &copy)
Definition: OgreRawPtr.h:89