source: OGRE/trunk/ogre_dependencies/Dependencies/include/Cg/cgTemplates.h @ 692

Revision 692, 8.3 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2 *
3 * Copyright (c) 2002, NVIDIA Corporation.
4 *
5 * 
6 *
7 * NVIDIA Corporation("NVIDIA") supplies this software to you in consideration
8 * of your agreement to the following terms, and your use, installation,
9 * modification or redistribution of this NVIDIA software constitutes
10 * acceptance of these terms.  If you do not agree with these terms, please do
11 * not use, install, modify or redistribute this NVIDIA software.
12 *
13 * 
14 *
15 * In consideration of your agreement to abide by the following terms, and
16 * subject to these terms, NVIDIA grants you a personal, non-exclusive license,
17 * under NVIDIA’s copyrights in this original NVIDIA software (the "NVIDIA
18 * Software"), to use, reproduce, modify and redistribute the NVIDIA
19 * Software, with or without modifications, in source and/or binary forms;
20 * provided that if you redistribute the NVIDIA Software, you must retain the
21 * copyright notice of NVIDIA, this notice and the following text and
22 * disclaimers in all such redistributions of the NVIDIA Software. Neither the
23 * name, trademarks, service marks nor logos of NVIDIA Corporation may be used
24 * to endorse or promote products derived from the NVIDIA Software without
25 * specific prior written permission from NVIDIA.  Except as expressly stated
26 * in this notice, no other rights or licenses express or implied, are granted
27 * by NVIDIA herein, including but not limited to any patent rights that may be
28 * infringed by your derivative works or by other works in which the NVIDIA
29 * Software may be incorporated. No hardware is licensed hereunder.
30 *
31 * 
32 *
33 * THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
34 * WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
35 * WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
36 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR ITS USE AND OPERATION
37 * EITHER ALONE OR IN COMBINATION WITH OTHER PRODUCTS.
38 *
39 * 
40 *
41 * IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL,
42 * EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOST
43 * PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44 * PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY OUT OF THE USE,
45 * REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE NVIDIA SOFTWARE,
46 * HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING
47 * NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF NVIDIA HAS BEEN ADVISED
48 * OF THE POSSIBILITY OF SUCH DAMAGE.
49 *
50 */
51
52/*********************************************************************NVMH2****
53File:  cgTemplates.h
54
55Copyright (C) 1999, 2000 NVIDIA Corporation
56This file is provided without support, instruction, or implied warranty of any
57kind.  NVIDIA makes no guarantee of its fitness for a particular purpose and is
58not liable under any circumstances for any damages or loss whatsoever arising
59from the use or inability to use this file or items derived from it.
60
61Comments:
62
63cgTemplates - cg utility templates
64
65
66******************************************************************************/
67
68#ifndef CG_TEMPLATE_H
69#define CG_TEMPLATE_H
70
71#include <stdlib.h>
72
73
74//////////////////////////////////////////
75//
76//       templated cg_vector class
77//
78//////////////////////////////////////////
79
80
81template <class T>
82class cg_vector{
83public:
84        typedef int size_type;
85
86        cg_vector();
87    cg_vector( cg_vector<T>& v );
88        ~cg_vector();
89
90        void clear();
91        int size() const;
92        T& operator [] ( int i );
93        void push_back( const T& t );
94
95private:
96        T* data;
97        int num;
98        int capacity;
99        const static int initSize;
100};
101
102
103//definitions of cg_vector methods
104
105template <class T>
106const int cg_vector<T>::initSize = 16;
107
108template <class T>
109cg_vector<T>::cg_vector(){
110        data = NULL;
111        clear();
112}
113
114template <class T>
115cg_vector<T>::cg_vector( cg_vector<T>& v ){
116        data = NULL;
117        clear();
118    for( int i=0; i<v.size(); i++ ){
119        push_back( v[i] );
120    }
121}
122
123template <class T>
124cg_vector<T>::~cg_vector(){
125        delete[] data;
126}
127
128template <class T>
129void cg_vector<T>::clear(){
130        if( data ){
131                delete[] data;
132        }
133        data = (T*)new T[initSize];
134        num = 0;
135        capacity = initSize;
136}
137
138template <class T>
139int cg_vector<T>::size() const{
140        return num;
141}
142
143template <class T>
144void cg_vector<T>::push_back( const T& t ){
145        if( num >= capacity ){
146                T* temp = new T[ 2 * capacity ];
147        for( int i = 0; i < num; i++ ){
148            temp[i] = data[i];
149        }
150                delete[] data;
151                data = temp;
152                capacity *= 2;
153        }
154        data[num] = t;
155        num++;
156}
157
158template <class T>
159T& cg_vector<T>::operator [] ( int i ){
160        if( i>=0 && i < num ){
161                return data[i];
162        }else{
163                return data[0];
164        }
165}
166
167
168
169
170//////////////////////////////////////////
171//
172//       templated cg_list class
173//
174//////////////////////////////////////////
175
176
177template <class T>
178class cg_list{
179public:
180        class Node{
181        public:
182                Node( const T& _data, Node* _next, Node* _prev ){
183                        data = _data;
184                        next = _next;
185                        prev = _prev;
186                }
187
188                T data;
189                Node* next;
190                Node* prev;
191        };
192
193        class iterator{
194        public:
195                iterator( Node* _node ){
196                        node = _node;
197                }
198                iterator(){
199                        node = NULL;
200                }
201
202                bool operator == ( const iterator& it ) const{
203                        return node == it.node;
204                }
205                bool operator != ( const iterator& it ) const{
206                        return !( *this == it );
207                }
208                T& operator * (){
209                        return node -> data;
210                }
211                void operator ++ (){
212                        node = node -> next;
213                }
214                void operator ++ (int){
215                        node = node -> next;
216                }
217
218                Node* node;
219        };
220
221        cg_list();
222    cg_list( const cg_list<T>& l );
223        ~cg_list();
224        iterator begin();
225        iterator end();
226        void push_back( const T& t );
227        void clear();
228        void remove( const T& value );
229
230private:
231        Node* head;
232        Node* tail;
233
234        void insert( const T& t, Node* node );
235        void remove( Node* node );
236};
237
238
239// definitions of cg_list methods
240
241template <class T>
242cg_list<T>::cg_list(){
243        head = tail = new Node( T(), NULL, NULL );
244}
245
246template <class T>
247cg_list<T>::~cg_list(){
248        clear();
249        delete head;
250}
251
252template <class T>
253cg_list<T>::cg_list( const cg_list<T>& l ){
254    head = tail = new Node( T(), NULL, NULL );
255    cg_list<T>::iterator it = l.begin();
256    while( it != l.end() ){
257        push_back( *it );
258        it++;
259    }
260}
261
262template <class T>
263cg_list<T>::iterator cg_list<T>::begin(){
264        return head;
265}
266
267template <class T>
268cg_list<T>::iterator cg_list<T>::end(){
269        return tail;
270}
271
272template <class T>
273void cg_list<T>::push_back( const T& t ){
274        insert( t, tail );
275}
276
277template <class T>
278void cg_list<T>::clear(){
279        while( head != tail ){
280                remove( head );
281        }
282}
283
284template <class T>
285void cg_list<T>::remove( const T& value ){
286        Node* curr = head;
287        Node* temp;
288        while( curr != tail ){
289                temp = curr;
290                curr = curr -> next;
291                if( temp -> data == value ){
292                        remove( temp );
293                }
294        }
295}
296
297template <class T>
298void cg_list<T>::insert( const T& t, cg_list<T>::Node* node ){
299        Node* newNode = new Node( t, node, node -> prev );
300        if( node -> prev == NULL ){
301                head = newNode;
302        }else{
303                node -> prev -> next = newNode;
304        }
305        node -> prev = newNode;
306}
307
308template <class T>
309void cg_list<T>::remove( cg_list<T>::Node* node ){
310        if( node == tail ){
311                return;
312        }
313        node -> next -> prev = node -> prev;
314        if( node -> prev == NULL ){
315                head = node -> next;
316        }else{
317                node -> prev -> next = node -> next;
318        }
319        delete node;
320}
321
322
323
324//////////////////////////////////////////
325//
326//             cg_string class
327//
328//////////////////////////////////////////
329
330
331class cg_string{
332public:
333        typedef int size_type;
334        cg_string();
335        cg_string( const char* s);
336        cg_string( const cg_string& s );
337        ~cg_string();
338
339        int size() const;
340        const char* c_str() const;
341        void resize( int n, char c = '\0' );
342        cg_string& erase( int pos = 0, int n = npos );
343        int find_last_of( const cg_string& s, int pos = npos ) const;
344        cg_string substr( int pos = 0, int len = npos ) const;
345
346        char& operator [] ( int i );
347        const char& operator [] ( int i ) const;
348        cg_string& operator = ( const cg_string& s );
349        cg_string& operator += ( const cg_string& s );
350        cg_string operator + ( const cg_string& s ) const;
351
352        const static int npos;
353
354private:
355        char* data;
356        int num;                                        //length of the string, not counting the trailing '\0'.
357        int capacity;
358        const static int initSize;
359
360        void set( const char* s, int n );
361};
362
363#endif
Note: See TracBrowser for help on using the repository browser.