Essentia  2.1-beta5-dev
roguevector.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2016 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_ROGUEVECTOR_H
21 #define ESSENTIA_ROGUEVECTOR_H
22 
23 #include <vector>
24 #include "types.h"
25 
26 namespace essentia {
27 
28 
29 template <typename T>
30 class RogueVector : public std::vector<T> {
31  protected:
33 
34  public:
35  RogueVector(T* tab = 0, size_t size = 0) : std::vector<T>(), _ownsMemory(false) {
36  setData(tab);
37  setSize(size);
38  }
39 
40  RogueVector(uint size, T value) : std::vector<T>(size, value), _ownsMemory(true) {}
41 
42  RogueVector(const RogueVector<T>& v) : std::vector<T>(), _ownsMemory(false) {
43  setData(const_cast<T*>(v.data()));
44  setSize(v.size());
45  }
46 
48  if (!_ownsMemory) {
49  setData(0);
50  setSize(0);
51  }
52  }
53 
54  // Those need to be implementation specific
55  void setData(T* data);
56  void setSize(size_t size);
57 };
58 
59 // Clang/LLVM implementation
60 #if defined (OS_MAC) || defined(OS_FREEBSD) || defined(__EMSCRIPTEN__)
61 
62 // TODO: this is a big hack that relies on clang/libcpp not changing the memory
63 // layout of the std::vector (very dangerous, but works for now...)
64 
65 template <typename T>
66 void RogueVector<T>::setData(T* data) { *reinterpret_cast<T**>(this) = data; }
67 
68 template <typename T>
69 void RogueVector<T>::setSize(size_t size) {
70  T** start = reinterpret_cast<T**>(this);
71  *(start+1) = *start + size;
72  *(start+2) = *start + size;
73 }
74 
75 // Windows implementation
76 #elif defined(OS_WIN32)
77 
78 template <typename T>
79 void RogueVector<T>::setData(T* data) { this->_Myfirst() = data; }
80 
81 template <typename T>
82 void RogueVector<T>::setSize(size_t size) {
83  this->_Mylast() = this->_Myfirst() + size;
84  this->_Myend() = this->_Myfirst() + size;
85 }
86 
87 /*
88 // TODO just a copy-paste from OS_LINUX version
89 template <typename T>
90 void RogueVector<T>::setData(T* data) { this->_M_impl._M_start = data; }
91 
92 template <typename T>
93 void RogueVector<T>::setSize(size_t size) {
94  this->_M_impl._M_finish = this->_M_impl._M_start + size;
95  this->_M_impl._M_end_of_storage = this->_M_impl._M_start + size;
96 }
97 */
98 
99 // Linux implementation
100 #elif defined(OS_LINUX)
101 
102 
103 template <typename T>
104 void RogueVector<T>::setData(T* data) { this->_M_impl._M_start = data; }
105 
106 template <typename T>
107 void RogueVector<T>::setSize(size_t size) {
108  this->_M_impl._M_finish = this->_M_impl._M_start + size;
109  this->_M_impl._M_end_of_storage = this->_M_impl._M_start + size;
110 }
111 
112 
113 #endif
114 
115 } // namespace essentia
116 
117 #endif // ESSENTIA_ROGUEVECTOR_H
RogueVector(uint size, T value)
Definition: roguevector.h:40
RogueVector(const RogueVector< T > &v)
Definition: roguevector.h:42
RogueVector(T *tab=0, size_t size=0)
Definition: roguevector.h:35
Definition: roguevector.h:30
~RogueVector()
Definition: roguevector.h:47
void setData(T *data)
Definition: roguevector.h:104
void setSize(size_t size)
Definition: roguevector.h:107
Definition: algorithm.h:28
bool _ownsMemory
Definition: roguevector.h:32
unsigned int uint
Definition: types.h:48