FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fixed_array.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (1 Mar. 2011)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #pragma once
17 
18 #include <cstddef>
19 #include <algorithm>
20 #include <iterator>
21 
22 namespace cpp {
23 
24 template<typename T, unsigned S>
26 {
27 private:
28  typedef FixedArray<T, S> Self_t;
29 
30 public:
31  // STL compatibility
32  typedef T* pointer;
33  typedef T& reference;
34  typedef T value_type;
35  typedef unsigned size_type;
36  typedef const T* const_pointer;
37  typedef const T& const_reference;
38  typedef std::ptrdiff_t difference_type;
39  typedef std::random_access_iterator_tag iterator_category;
40  typedef T* iterator;
41  typedef const T* const_iterator;
42 
43  // Ctor
45  FixedArray<T, S>(const Self_t& other);
46 
47  // Iteration
48  const_iterator begin() const;
49  iterator begin();
50  const_iterator end() const;
51  iterator end();
52 
53  // Measurement (STL compatibility)
54  bool empty() const;
55  size_type max_size() const;
56  size_type size() const;
57 
58  // Access
59  T& get(unsigned i);
60  const T& get(unsigned i) const;
61  T& operator[](unsigned i);
62  const T& operator[](unsigned i) const;
63  const T* cArray() const;
64  T* cArray();
65 
66  // Element change
67  void set(unsigned i, const T& coord);
68  Self_t& operator=(const Self_t& other);
69 
70 protected:
71  // Attributes
72  T m_vector[S];
73 };
74 
76 template<typename TEXT_STREAM, typename T, unsigned S>
77 TEXT_STREAM& operator<<(TEXT_STREAM& os, const FixedArray<T, S>& coords);
78 
79 //
80 // Implementation
81 //
82 
91 template<typename T, unsigned S>
93 {
94 }
95 
96 template<typename T, unsigned S>
98 {
99  std::copy(other.begin(), other.end(), this->begin());
100 }
101 
102 template<typename T, unsigned S>
105 {
106  return m_vector;
107 }
108 
109 template<typename T, unsigned S>
112 {
113  return m_vector;
114 }
115 
116 template<typename T, unsigned S>
119 {
120  return m_vector + S;
121 }
122 
123 template<typename T, unsigned S>
126 {
127  return m_vector + S;
128 }
129 
130 template<typename T, unsigned S>
132 {
133  return false;
134 }
135 
136 template<typename T, unsigned S>
139 {
140  return S;
141 }
142 
143 template<typename T, unsigned S>
146 {
147  return this->max_size();;
148 }
149 
150 template<typename T, unsigned S>
151 T& FixedArray<T, S>::get(unsigned i)
152 {
153  return m_vector[i];
154 }
155 
156 template<typename T, unsigned S>
157 const T& FixedArray<T, S>::get(unsigned i) const
158 {
159  return m_vector[i];
160 }
161 
162 template<typename T, unsigned S>
164 {
165  return this->get(i);
166 }
167 
168 template<typename T, unsigned S>
169 const T& FixedArray<T, S>::operator[](unsigned i) const
170 {
171  return this->get(i);
172 }
173 
174 template<typename T, unsigned S>
175 const T* FixedArray<T, S>::cArray() const
176 {
177  return &(m_vector[0]);
178 }
179 
180 template<typename T, unsigned S>
182 {
183  return const_cast<T*>(static_cast<const FixedArray<T, S>*>(this)->cArray());
184 }
185 
186 template<typename T, unsigned S>
187 void FixedArray<T, S>::set(unsigned i, const T& coord)
188 {
189  m_vector[i] = coord;
190 }
191 
192 template<typename T, unsigned S>
194 {
195  if (this != &other)
196  std::copy(other.begin(), other.end(), this->begin());
197  return *this;
198 }
199 
200 template<typename TEXT_STREAM, typename T, unsigned S>
201 TEXT_STREAM& operator<<(TEXT_STREAM& os, const FixedArray<T, S>& coords)
202 {
203  os << "(";
204  for (unsigned i = 0; i < S; i++) {
205  os << coords.get(i);
206  if (i < S - 1)
207  os << ", ";
208  }
209  return os << ")";
210 }
211 
212 } // namespace cpp
std::random_access_iterator_tag iterator_category
Definition: fixed_array.h:39
T value_type
Definition: fixed_array.h:34
T * pointer
Definition: fixed_array.h:32
const T * cArray() const
Definition: fixed_array.h:175
Definition: basic_shared_pointer.h:20
T & reference
Definition: fixed_array.h:33
size_type max_size() const
Definition: fixed_array.h:138
std::ptrdiff_t difference_type
Definition: fixed_array.h:38
FixedArray()
Definition: fixed_array.h:92
const T * const_pointer
Definition: fixed_array.h:36
Self_t & operator=(const Self_t &other)
Definition: fixed_array.h:193
const T * const_iterator
Definition: fixed_array.h:41
const T & const_reference
Definition: fixed_array.h:37
T & get(unsigned i)
Definition: fixed_array.h:151
const_iterator end() const
Definition: fixed_array.h:118
bool empty() const
Definition: fixed_array.h:131
unsigned size_type
Definition: fixed_array.h:35
Provides a generic fixed-size array of items.
Definition: fixed_array.h:25
void set(unsigned i, const T &coord)
Definition: fixed_array.h:187
T m_vector[S]
Definition: fixed_array.h:72
size_type size() const
Definition: fixed_array.h:145
T & operator[](unsigned i)
Definition: fixed_array.h:163
const_iterator begin() const
Definition: fixed_array.h:104
T * iterator
Definition: fixed_array.h:40