├── LICENSE ├── maya_templates └── maya_stl.h ├── README.md ├── maya_iteration └── maya_array_range.h └── maya_array └── maya_array.h /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 scottenglert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /maya_templates/maya_stl.h: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2019 Scott Englert 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | #ifndef MAYATEMPLATES_MAYA_STL_H_ 25 | #define MAYATEMPLATES_MAYA_STL_H_ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | namespace std { 32 | /* 33 | Standard Template Library hash specialization for MStrings 34 | */ 35 | template<> 36 | class hash 37 | { 38 | public: 39 | size_t operator() (const MString& value) const 40 | { 41 | MUniqueString uniqueString = MUniqueString::intern(value); 42 | return uniqueString.hash(); 43 | } 44 | }; 45 | 46 | /* 47 | Standard Template Library equal_to specialization for MStrings 48 | */ 49 | template <> 50 | class equal_to 51 | { 52 | public: 53 | bool operator() (const MString& valueA, const MString& valueB) const 54 | { 55 | return valueA == valueB; 56 | } 57 | }; 58 | } // std namespace 59 | 60 | #endif // MAYATEMPLATES_MAYA_STL_H_ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Maya API Utils 2 | This repo contains useful utilties for Autodesk's Maya API in C++. More common tools will be added over time. 3 | 4 | ## Maya Array 5 | A class template that uses a Maya M***Array as the underlying container but has a more standard library compatible interface. It provides the best of both worlds by still being able to use it in Maya's API but also with other libraries and algorithms without copying the data from one container to the other. More improvements and support will be when possible. 6 | 7 | ### Usage Examples 8 | 9 | ``` 10 | // Create an empty MPointArray that is wrapped around this generic interface 11 | mayaarray::MayaArray meshPoints; 12 | 13 | // get all the points from a MFnMesh object 14 | myMeshFn.getPoints(meshPoints.array()); 15 | 16 | // copy points from an existing MPointArray 17 | MPointArray otherPoints(5); 18 | mayaarray::MayaArray pointsCopy(otherPoints); 19 | 20 | // create iterators for the beginning and end of the array 21 | mayaarray::MayaArray::iterator beginIt = meshPoints.begin(); 22 | mayaarray::MayaArray::iterator endIt = meshPoints.end(); 23 | 24 | // Assign a new point to the first point in the array 25 | *beginIt = MPoint(1.0, 1.0, 1.0); 26 | 27 | // dereferencing to get the x value of the point 28 | std::cout << "X= " << beginIt->x << std::endl; 29 | 30 | // using regular for loops 31 | for(auto it = beginIt; it != endIt; ++it) 32 | std::cout << *it << std::endl; 33 | 34 | // using new range based loop in C++11 35 | for(auto& pnt : meshPoints) 36 | std::cout << pnt << std::endl; 37 | 38 | // sort the array using a comparsion function 39 | std::sort(beginIt, endIt, myPointCompareFunc); 40 | 41 | // getting the number of points between two iterators 42 | auto pointCount = std::distance(beginIt, endIt); 43 | std::cout << pointCount << std::endl; 44 | 45 | // append 3 default MPoints to the array using standard library algorithm fill_n 46 | std::fill_n(std::back_inserter(meshPoints), 3, MPoint()); 47 | 48 | // erase points from beginIt to endIt 49 | meshPoints.erase(beginIt, endIt); 50 | ``` 51 | 52 | ## Maya Iteration 53 | Contains tools for iteration. Currently available is class template that wraps an existing Maya M***Array object and provides a standard library iterator. This makes it easy to pass our Maya array objects to other libraries and algorithms that work with iterators without having to copy your data to another compatible container. 54 | 55 | ### Usage Examples 56 | ``` 57 | MPointArray myPointArray(5); // array of 5 points 58 | mayaiteration::MayaArrayRange myPointArrayRange(myPointArray); 59 | 60 | // create iterators for the beginning and end of the array 61 | mayaiteration::MayaArrayRange::iterator beginIt = myPointArrayRange.begin(); 62 | mayaiteration::MayaArrayRange::iterator endIt = myPointArrayRange.end(); 63 | 64 | // Assign a new point to the first point in the array 65 | *beginIt = MPoint(1.0, 1.0, 1.0); 66 | 67 | // dereferencing to get the x value of the point 68 | std::cout << "X= " << beginIt->x << std::endl; 69 | 70 | // using regular loops 71 | for(auto it = beginIt; it != endIt; ++it) 72 | std::cout << *it << std::endl; 73 | 74 | // using new range based loop in C++11 75 | for(auto pnt : myPointArrayRange) 76 | std::cout << pnt << std::endl; 77 | 78 | // sort the array using a comparsion function 79 | std::sort(beginIt, endIt, myPointCompareFunc); 80 | 81 | // getting the number of points between two iterators 82 | mayaiteration::MayaArrayRange::iterator::difference_type dist = std::distance(beginIt, endIt); 83 | std::cout << dist << std::endl; 84 | ``` 85 | -------------------------------------------------------------------------------- /maya_iteration/maya_array_range.h: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Scott Englert 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef MAYAITERATION_MAYA_ARRAY_RANGE_H_ 26 | #define MAYAITERATION_MAYA_ARRAY_RANGE_H_ 27 | 28 | #include 29 | #include 30 | 31 | // forward declaration 32 | namespace mayaarray { 33 | template 34 | class MayaArray; 35 | } 36 | 37 | namespace mayaiteration { 38 | 39 | /** 40 | Maya Array Range Class Template 41 | 42 | DESCRIPTION: 43 | This template class is intended to provide Maya array classes a std iterator 44 | interface. This makes working with Maya's arrays more friendly and consistent 45 | with the C++ standard library. This allows use of iterator features with out 46 | sacrificing performance of copying the array to and from other containers. This 47 | template class should work for all of Maya's internal *Array classes that have 48 | an "[]"" operator and a "length" member function. 49 | 50 | A MayaArrayRange instance provides the commonly used "begin()" and "end()" 51 | functions. These return either an iterator or a const iterator. See the 52 | std iterator documentation for more information on using the iterator instance. 53 | 54 | USAGE: 55 | MPointArray myPointArray(5); // array of 5 points 56 | mayaiteration::MayaArrayRange myPointArrayRange(myPointArray); 57 | 58 | // create iterators for the beginning and end of the array 59 | mayaiteration::MayaArrayRange::iterator beginIt = myPointArrayRange.begin(); 60 | mayaiteration::MayaArrayRange::iterator endIt = myPointArrayRange.end(); 61 | 62 | // Assign a new point to the first point in the array 63 | *beginIt = MPoint(1.0, 1.0, 1.0); 64 | 65 | // dereferencing to get the x value of the point 66 | std::cout << "X= " << beginIt->x << std::endl; 67 | 68 | // using regular loops 69 | for(auto it = beginIt; it != endIt; ++it) 70 | std::cout << *it << std::endl; 71 | 72 | // using new range based loop in C++11 73 | for(auto pnt : myPointArrayRange) 74 | std::cout << pnt << std::endl; 75 | 76 | // sort the array using a comparsion function 77 | std::sort(beginIt, endIt, myPointCompareFunc); 78 | 79 | // getting the number of points between two iterators 80 | mayaiteration::MayaArrayRange::iterator::difference_type dist = std::distance(beginIt, endIt); 81 | std::cout << dist << std::endl; 82 | */ 83 | template 84 | class MayaArrayRange { 85 | protected: 86 | // there are different types returned based if the array is const or not 87 | typedef typename decltype(std::declval()[0]) ref_type; 88 | typedef typename decltype(std::declval()[0]) const_ref_type; 89 | typedef typename std::remove_reference::type item_type; 90 | 91 | public: 92 | template 93 | class MayaArrayIter : public std::iterator { 94 | 95 | friend class MayaArrayRange; 96 | template 97 | friend class mayaarray::MayaArray; 98 | 99 | protected: 100 | C* c; 101 | unsigned int i; 102 | 103 | MayaArrayIter(C& c) : c(&c), i(0) {} 104 | MayaArrayIter(C& c, unsigned int i) : c(&c), i(i) {} 105 | 106 | public: 107 | template 108 | MayaArrayIter(const MayaArrayIter& other) : c(other.c), i(other.i) {} 109 | 110 | template 111 | MayaArrayIter& operator=(const MayaArrayIter& other) { 112 | c = other.c; 113 | i = other.i; 114 | return *this; 115 | } 116 | 117 | reference operator*() const { 118 | assert(i < c->length()); 119 | return (*c)[i]; 120 | } 121 | 122 | pointer operator->() const { 123 | assert(i < c->length()); 124 | return &(*c)[i]; 125 | } 126 | 127 | MayaArrayIter& operator++() { 128 | ++i; 129 | return *this; 130 | } 131 | 132 | MayaArrayIter& operator--() { 133 | --i; 134 | return *this; 135 | } 136 | 137 | MayaArrayIter operator++(int) { 138 | return MayaArrayIter(*c, i++); 139 | } 140 | 141 | MayaArrayIter operator--(int) { 142 | return MayaArrayIter(*c, i--); 143 | } 144 | 145 | MayaArrayIter operator+(const difference_type& n) const { 146 | return MayaArrayIter(*c, (i + n)); 147 | } 148 | 149 | MayaArrayIter& operator+=(const difference_type& n) { 150 | i += n; 151 | return *this; 152 | } 153 | 154 | MayaArrayIter operator-(const difference_type& n) const { 155 | return MayaArrayIter(*c, (i - n)); 156 | } 157 | 158 | MayaArrayIter& operator-=(const difference_type& n) { 159 | i -= n; 160 | return *this; 161 | } 162 | 163 | reference operator[](const difference_type& n) const { 164 | return (*c)[i + n]; 165 | } 166 | 167 | bool operator==(const MayaArrayIter& other) const { 168 | return (i == other.i && c == other.c); 169 | } 170 | 171 | bool operator!=(const MayaArrayIter& other) const { 172 | return !(*this == other); 173 | } 174 | 175 | bool operator<(const MayaArrayIter& other) const { 176 | return (i < other.i && c == other.c); 177 | } 178 | 179 | bool operator>(const MayaArrayIter& other) const { 180 | return !(*this < other || *this == other); 181 | } 182 | 183 | bool operator<=(const MayaArrayIter& other) const { 184 | return !(*this > other); 185 | } 186 | 187 | bool operator>=(const MayaArrayIter& other) const { 188 | return !(*this < other); 189 | } 190 | 191 | difference_type operator+(const MayaArrayIter& other) const { 192 | return i + other.i; 193 | } 194 | 195 | difference_type operator-(const MayaArrayIter& other) const { 196 | return i - other.i; 197 | } 198 | }; 199 | 200 | typedef MayaArrayIter iterator; 201 | typedef MayaArrayIter const_iterator; 202 | typedef std::reverse_iterator reverse_iterator; 203 | typedef std::reverse_iterator const_reverse_iterator; 204 | 205 | MayaArrayRange(T* mayaArray) : mArray(*mayaArray) {} 206 | MayaArrayRange(T& mayaArray) : mArray(mayaArray) {} 207 | 208 | iterator begin() { 209 | return iterator(mArray); 210 | } 211 | 212 | const_iterator begin() const { 213 | return const_iterator(mArray); 214 | } 215 | 216 | const_iterator cbegin() const { 217 | return const_iterator(mArray); 218 | } 219 | 220 | reverse_iterator rbegin() { 221 | return reverse_iterator(end()); 222 | } 223 | 224 | const_reverse_iterator rbegin() const { 225 | return const_reverse_iterator(end()); 226 | } 227 | 228 | const_reverse_iterator crbegin() const { 229 | return const_reverse_iterator(cend()); 230 | } 231 | 232 | iterator end() { 233 | return iterator(mArray, mArray.length()); 234 | } 235 | 236 | const_iterator end() const { 237 | return const_iterator(mArray, mArray.length()); 238 | } 239 | 240 | const_iterator cend() const { 241 | return const_iterator(mArray, mArray.length()); 242 | } 243 | 244 | reverse_iterator rend() { 245 | return reverse_iterator(begin()); 246 | } 247 | 248 | const_reverse_iterator rend() const { 249 | return const_reverse_iterator(begin()); 250 | } 251 | 252 | const_reverse_iterator crend() const { 253 | return const_reverse_iterator(cbegin()); 254 | } 255 | 256 | protected: 257 | T& mArray; 258 | }; 259 | 260 | } // namespace mayaiteration 261 | 262 | #endif // MAYAITERATION_MAYA_ARRAY_RANGE_H_ -------------------------------------------------------------------------------- /maya_array/maya_array.h: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2016 Scott Englert 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | #ifndef MAYAARRAY_MAYA_ARRAY_H_ 25 | #define MAYAARRAY_MAYA_ARRAY_H_ 26 | 27 | #include 28 | #include 29 | 30 | namespace mayaarray { 31 | 32 | /** 33 | Generic Maya Array Class Template 34 | 35 | DESCRIPTION: 36 | This class template container that uses a M***Array for the underlying container. 37 | It is intended to be more generic and compatible with the C++ standard library 38 | and algorithms, but able to be used in Maya's API to get benefits of both worlds 39 | without copying data from one type to another. One example is to use this class 40 | for creating iterators and using more generic programming features. This class should 41 | work for all of Maya's internal ***Array classes. This class is mostly modeled after 42 | the standard library vector class. More featues will be added to take advantage 43 | of C++ features as they become available. 44 | 45 | A MayaArray instance provides the commonly used "begin()" and "end()" 46 | functions. This returns a random access compliant iterator. See the 47 | standard library iterator documentation for more information on using 48 | the iterator instance. More functionality will be added for more support. 49 | 50 | USAGE: 51 | // Create an empty MPointArray that is wrapped around this generic interface 52 | mayaarray::MayaArray meshPoints; 53 | 54 | // get all the points from a MFnMesh object 55 | myMeshFn.getPoints(meshPoints.array()); 56 | 57 | // copy points from an existing MPointArray 58 | MPointArray otherPoints(5); 59 | mayaarray::MayaArray pointsCopy(otherPoints); 60 | 61 | // create iterators for the beginning and end of the array 62 | mayaarray::MayaArray::iterator beginIt = meshPoints.begin(); 63 | mayaarray::MayaArray::iterator endIt = meshPoints.end(); 64 | 65 | // Assign a new point to the first point in the array 66 | *beginIt = MPoint(1.0, 1.0, 1.0); 67 | 68 | // dereferencing to get the x value of the point 69 | std::cout << "X= " << beginIt->x << std::endl; 70 | 71 | // using regular for loops 72 | for(auto it = beginIt; it != endIt; ++it) 73 | std::cout << *it << std::endl; 74 | 75 | // using new range based loop in C++11 76 | for(auto pnt : meshPoints) 77 | std::cout << pnt << std::endl; 78 | 79 | // sort the array using a comparsion function 80 | std::sort(beginIt, endIt, myPointCompareFunc); 81 | 82 | // getting the number of points between two iterators 83 | auto pointCount = std::distance(beginIt, endIt); 84 | std::cout << pointCount << std::endl; 85 | 86 | // append 3 default MPoints to the array using standard library algorithm fill_n 87 | std::fill_n(std::back_inserter(meshPoints), 3, MPoint()); 88 | 89 | // erase points from beginIt to endIt 90 | meshPoints.erase(beginIt, endIt); 91 | */ 92 | template 93 | class MayaArray { 94 | protected: 95 | T mArray; 96 | 97 | public: 98 | // there are different types returned based if the array is const or not 99 | typedef typename decltype(std::declval()[0]) reference; 100 | typedef typename decltype(std::declval()[0]) const_reference; 101 | typedef typename std::remove_reference::type value_type; 102 | typedef unsigned int size_type; 103 | 104 | typedef typename mayaiteration::MayaArrayRange::iterator iterator; 105 | typedef typename mayaiteration::MayaArrayRange::const_iterator const_iterator; 106 | typedef typename mayaiteration::MayaArrayRange::reverse_iterator reverse_iterator; 107 | typedef typename mayaiteration::MayaArrayRange::const_reverse_iterator const_reverse_iterator; 108 | 109 | /** 110 | Creates an empty array 111 | */ 112 | MayaArray() {} 113 | 114 | /** 115 | Creates an array of "count" elements with the given value. 116 | 117 | \param[in] count number of elements in the array 118 | \param[in] value the initial value of the elements 119 | */ 120 | MayaArray(size_type count, const value_type& value=value_type()) : mArray(count, value) {} 121 | 122 | /** 123 | Creates an array with a copy of the given M***Array instance 124 | 125 | \param[in] maya_array the Maya array to copy 126 | */ 127 | MayaArray(const T& maya_array) : mArray(maya_array) {} 128 | 129 | /** 130 | Creates a copy of another MayaArray instance, copying elements 131 | from the other array to this array. 132 | 133 | \param[in] other the other MayaArray instance to copy 134 | */ 135 | MayaArray(const MayaArray& other) : mArray(other.mArray) {} 136 | 137 | /** 138 | Assigns all values from a M***Array instance to this array 139 | 140 | \param[in] other the Maya array to assign values from 141 | */ 142 | MayaArray& operator=(const T& other) { 143 | mArray = other; 144 | return *this; 145 | } 146 | 147 | /** 148 | Assigns all values from another MayaArray instance to this array 149 | 150 | \param[in] other the Maya array to assign values from 151 | */ 152 | MayaArray& operator=(const MayaArray& other) { 153 | mArray = other.mArray; 154 | return *this; 155 | } 156 | 157 | /** 158 | Returns a reference to underlying Maya array instance 159 | */ 160 | inline T& array() { 161 | return mArray; 162 | } 163 | 164 | /** 165 | Returns a const reference to the underlying Maya array instance 166 | */ 167 | inline const T& array() const { 168 | return mArray; 169 | } 170 | 171 | /** 172 | Returns an iterator for the first element in the array 173 | */ 174 | inline iterator begin() { 175 | return iterator(mArray); 176 | } 177 | 178 | /** 179 | Returns a const iterator for the first element in the array 180 | */ 181 | inline const_iterator begin() const { 182 | return const_iterator(mArray); 183 | } 184 | 185 | /** 186 | Returns a const iterator for the first element in the array 187 | */ 188 | inline const_iterator cbegin() const { 189 | return const_iterator(mArray); 190 | } 191 | 192 | /** 193 | Returns a reverse iterator for the last element in the array 194 | */ 195 | reverse_iterator rbegin() { 196 | return reverse_iterator(end()); 197 | } 198 | 199 | /** 200 | Returns a const reverse iterator for the last element in the array 201 | */ 202 | const_reverse_iterator rbegin() const { 203 | return const_reverse_iterator(end()); 204 | } 205 | 206 | /** 207 | Returns a const reverse iterator for the last element in the array 208 | */ 209 | const_reverse_iterator crbegin() const { 210 | return const_reverse_iterator(cend()); 211 | } 212 | 213 | /** 214 | Returns an iterator for one past the last element in the array 215 | */ 216 | inline iterator end() { 217 | return iterator(mArray, mArray.length()); 218 | } 219 | 220 | /** 221 | Returns a const iterator for one past the last element in the array 222 | */ 223 | inline const_iterator end() const { 224 | return const_iterator(mArray, mArray.length()); 225 | } 226 | 227 | /** 228 | Returns a const iterator for one past the last element in the array 229 | */ 230 | inline const_iterator cend() const { 231 | return const_iterator(mArray, mArray.length()); 232 | } 233 | 234 | /** 235 | Returns a reverse iterator for one before the first element in the array 236 | */ 237 | reverse_iterator rend() { 238 | return reverse_iterator(begin()); 239 | } 240 | 241 | /** 242 | Returns a const reverse iterator for one before the first element in the array 243 | */ 244 | const_reverse_iterator rend() const { 245 | return const_reverse_iterator(begin()); 246 | } 247 | 248 | /** 249 | Returns a const reverse iterator for one before the first element in the array 250 | */ 251 | const_reverse_iterator crend() const { 252 | return const_reverse_iterator(cbegin()); 253 | } 254 | 255 | /** 256 | Appends the value to the end of the array 257 | 258 | \param[in] value value to append 259 | */ 260 | inline void push_back(const value_type& value) { 261 | mArray.append(value); 262 | } 263 | 264 | /** 265 | Inserts the value in the front of the array 266 | 267 | \param[in] value value to insert 268 | */ 269 | inline void push_front(const value_type& value) { 270 | mArray.insert(value, 0); 271 | } 272 | 273 | /** 274 | Clear the array of all elements 275 | */ 276 | inline void clear() { 277 | mArray.clear(); 278 | } 279 | 280 | /** 281 | Inserts a value at the given iterator position 282 | 283 | \param[in] pos const iterator to insert position 284 | \param[in] value value to insert at position 285 | 286 | \return 287 | iterator at the inserted value 288 | */ 289 | iterator insert(const_iterator pos, const value_type& value) { 290 | size_type i = pos - begin(); 291 | mArray.insert(value, i); 292 | return iterator(mArray, i); 293 | } 294 | 295 | /** 296 | Inserts a value before the given iterator position "count" number of times 297 | 298 | \param[in] pos const iterator to insert position 299 | \param[in] count number of times the value will be inserted 300 | \param[in] value value to insert at position 301 | 302 | \return 303 | iterator at the first inserted value 304 | */ 305 | iterator insert(const_iterator pos, size_type count, const value_type& value) { 306 | size_type i = pos - begin(); 307 | while (count--) 308 | mArray.insert(value, i); 309 | return iterator(mArray, i); 310 | } 311 | 312 | /** 313 | Inserts a range of values before the given iterator position 314 | 315 | \param[in] pos const interator to the element to insert before 316 | \param[in] first iterator to first value to insert 317 | \param[in] last iterator to one past the last value to insert 318 | 319 | \return 320 | iterator at the first inserted value 321 | */ 322 | template 323 | iterator insert(const_iterator pos, InputIt first, InputIt last) { 324 | size_type i = pos - begin(); 325 | for (size_type p=i; first != last; ++first, ++p) 326 | mArray.insert(*first, p); 327 | return iterator(mArray, i); 328 | } 329 | 330 | /** 331 | Erases a value from the array at the given iterator position 332 | 333 | \param[in] pos const iterator to the element to erase 334 | 335 | \return 336 | iterator at the erase position 337 | */ 338 | iterator erase(const_iterator pos) { 339 | size_type i = pos - begin(); 340 | mArray.remove(i); 341 | return iterator(mArray, i); 342 | } 343 | 344 | /** 345 | Erases a range from the array from the first iterator position up to, 346 | but not including, the last. 347 | 348 | \param[in] first const iterator to first element to erase 349 | \param[in] last const iterator to the one past last element to erase 350 | 351 | \return 352 | iterator at the first erased position 353 | */ 354 | iterator erase(const_iterator first, const_iterator last) { 355 | size_type i = first - begin(); 356 | for (; first != last; ++first) 357 | mArray.remove(i); 358 | return iterator(mArray, i); 359 | } 360 | 361 | /** 362 | Resizes the array to have the given number of elements. If the array 363 | size increases, it will be filled with unitialized values. If the 364 | size decreases, the values at the end will be lost. 365 | 366 | \param[in] count set the size of the array to this number of elements 367 | */ 368 | inline void resize(size_type count) { 369 | mArray.setLength(count); 370 | } 371 | 372 | /** 373 | Resizes the array to have the given number of elements. If the array 374 | size increases, it will be filled with the given value. If the 375 | size decreases, the values at the end will be lost. 376 | 377 | \param[in] count set the size of the array to this number of elements 378 | \param[in] value the to set for new elements if the array increases size 379 | */ 380 | void resize(size_type count, const value_type& value) { 381 | size_type oldSize = size(); 382 | if (count < oldSize) { 383 | mArray.setLength(count); 384 | } 385 | else if (oldSize < count) { 386 | mArray.setLength(count); 387 | for (size_type i=oldSize; i < count; ++i) 388 | mArray[i] = value; 389 | } 390 | } 391 | 392 | /** 393 | Returns a reference to the value at the given position. Similar to the 394 | [] operator, but this will throw a out_of_range exception if the position 395 | is not within bounds of the array. 396 | 397 | \param[in] pos position in the array 398 | 399 | \return 400 | reference to element at pos 401 | */ 402 | reference at(size_type pos) { 403 | if (mArray.length() <= pos) 404 | throw std::out_of_range("MayaArray out of bounds"); 405 | return mArray[pos]; 406 | } 407 | 408 | /** 409 | Returns a const reference to the value at the given position. Similar to the 410 | [] operator, but this will throw a out_of_range exception if the position 411 | is not within bounds of the array. 412 | 413 | \param[in] pos position in the array 414 | 415 | \return 416 | const_reference to element at pos 417 | */ 418 | const_reference at(size_type pos) const { 419 | if (mArray.length() <= pos) 420 | throw std::out_of_range("MayaArray out of bounds"); 421 | return mArray[pos]; 422 | } 423 | 424 | /** 425 | Returns a reference to the value at the given position. Similar to the 426 | "at" function, but there is no bounds checking before getting the value. 427 | 428 | \param[in] pos position in the array 429 | 430 | \return 431 | reference to value at pos 432 | */ 433 | inline reference operator[](size_type pos) { 434 | return mArray[pos]; 435 | } 436 | 437 | /** 438 | Returns a const reference to the value at the given position. Similar to the 439 | "at" function, but there is no bounds checking before getting the value. 440 | 441 | \param[in] pos position in the array 442 | 443 | \return 444 | const_reference to value at pos 445 | */ 446 | inline const_reference operator[](size_type pos) const { 447 | return mArray[pos]; 448 | } 449 | 450 | /** 451 | Returns a reference to the first value in the array 452 | 453 | \return 454 | reference to value 455 | */ 456 | inline reference front() { 457 | return (*begin()); 458 | } 459 | 460 | /** 461 | Returns a const reference to the first value in the array 462 | 463 | \return 464 | const_reference to value 465 | */ 466 | inline const_reference front() const { 467 | return (*begin()); 468 | } 469 | 470 | /** 471 | Returns a reference to the last value in the array 472 | 473 | \return 474 | reference to value 475 | */ 476 | inline reference back() { 477 | return (*(end() - 1)); 478 | } 479 | 480 | /** 481 | Returns a const reference to the last value in the array 482 | 483 | \return 484 | const_reference to value 485 | */ 486 | inline const_reference back() const { 487 | return (*(end() - 1)); 488 | } 489 | 490 | /** 491 | Returns a size of the array 492 | 493 | \return 494 | number of elements in array 495 | */ 496 | inline size_type size() const { 497 | return mArray.length(); 498 | } 499 | }; 500 | 501 | } // namespace mayaarray 502 | 503 | #endif // MAYAARRAY_MAYA_ARRAY_H_ --------------------------------------------------------------------------------