Essentia  2.1-beta6-dev
roguevector.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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(__clang__) || 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 // Linux implementation
76 #elif defined(OS_LINUX) || defined(OS_MINGW)
77 
78 template <typename T>
79 void RogueVector<T>::setData(T* data) { this->_M_impl._M_start = data; }
80 
81 template <typename T>
82 void RogueVector<T>::setSize(size_t size) {
83  this->_M_impl._M_finish = this->_M_impl._M_start + size;
84  this->_M_impl._M_end_of_storage = this->_M_impl._M_start + size;
85 }
86 
87 // Windows implementation
88 #elif defined(OS_WIN32)
89 
90 template <typename T>
91 void RogueVector<T>::setData(T* data) {
92  this->_Myfirst() = data;
93 }
94 
95 template <typename T>
96 void RogueVector<T>::setSize(size_t size) {
97  this->_Mylast() = this->_Myfirst() + size;
98  this->_Myend() = this->_Myfirst() + size;
99 }
100 
101 #endif
102 
103 } // namespace essentia
104 
105 #endif // ESSENTIA_ROGUEVECTOR_H
Definition: roguevector.h:30
bool _ownsMemory
Definition: roguevector.h:32
~RogueVector()
Definition: roguevector.h:47
void setSize(size_t size)
Definition: roguevector.h:82
RogueVector(const RogueVector< T > &v)
Definition: roguevector.h:42
RogueVector(uint size, T value)
Definition: roguevector.h:40
void setData(T *data)
Definition: roguevector.h:79
RogueVector(T *tab=0, size_t size=0)
Definition: roguevector.h:35
Definition: algorithm.h:28
unsigned int uint
Definition: types.h:49