Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
util.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2016 "Omnidome" by cr8tr
2  * Dome Mapping Projection Software (http://omnido.me).
3  * Omnidome was created by Michael Winkelmann aka Wilston Oreo (@WilstonOreo)
4  *
5  * This file is part of Omnidome.
6  *
7  * Omnidome is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Affero General Public License for more details.
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OMNI_UTIL_H_
21 #define OMNI_UTIL_H_
22 
23 #include <type_traits>
24 #include <memory>
25 #include <QDebug>
26 #include <QByteArray>
27 #include <QString>
28 #include <omni/exception.h>
29 
30 namespace omni {
31  namespace util {
32  /// Pointer deleter functor
33  struct QtDeleter
34  {
35  template<typename QOBJECT>
36  void operator()(QOBJECT *_obj)
37  {
38  if (!_obj->parent()) _obj->deleteLater();
39  }
40  };
41 
42  /// QUniquePtr for QObjects
43  template<typename T>
44  using QUniquePtr = std::unique_ptr<T, QtDeleter>;
45 
46  /// Linear interpolation between two values
47  template<typename T, typename A>
48  T mix(const T& _x, const T& _y, A _a)
49  {
50  return _x * (1.0 - _a) + _y * _a;
51  }
52 
53  /// Return content of file from a file name
54  QString fileToStr(const QString& _filename);
55 
56  /// Remove file extension and return string without file extension
57  QString removeFileExt(QString const& _filename);
58 
59  /// Checks if file exists and is actually a file and not a directory
60  bool fileExists(QString const&);
61 
62  /// Test if two instances (which inherit from SerializationInterface) have
63  // equal data
64  template<typename T>
65  bool testPtrEqual(T const *_a, T const *_b)
66  {
67  return _a && _b ?
68 
69  // Call equal() member function from SerializationInterface
70  _a->equal(_b) : // Otherwise
71  // Compare pointer adresses
72  (_a == _b);
73  }
74 
75  /// Test if two vectors which hold unique_ptr's of SerializationInterfaces
76  // are equal
77  template<typename T, typename F>
78  bool testPtrVectorEqual(T const& _a, T const& _b, F f)
79  {
80  // Size of vector must be equal
81  if (_a.size() != _b.size()) return false;
82 
83  // Test each element for equality
84  size_t _size = _a.size();
85 
86  for (size_t i = 0; i < _size; ++i)
87  {
88  // Call functor with pointers as arguments
89  if (!f(_a[i], _b[i])) return false;
90  }
91  return true;
92  }
93 
94  /// Test if two vectors are equal
95  template<typename T>
96  bool testPtrVectorEqual(T const& _a, T const& _b)
97  {
98  typedef typename T::value_type value_type;
99  typedef typename value_type::element_type element_type;
100  return testPtrVectorEqual(_a, _b, testPtrEqual<element_type>);
101  }
102 
103  /// Converts an enum class to integer
104  template<typename ENUM>
105  auto enumToInt(ENUM const & _v)
106  ->typename std::underlying_type<ENUM>::type
107  {
108  return static_cast<typename std::underlying_type<ENUM>::type>(_v);
109  }
110 
111  /// Converts an integer to enum
112  template<typename ENUM, typename INT>
113  ENUM intToEnum(INT _i) {
114  return static_cast<ENUM>(_i);
115  }
116  }
117 
118  using util::mix;
119  using util::QUniquePtr;
120 }
121 
122 #define OMNI_DEBUG \
123  qDebug() << __FILE__ << ":" << __LINE__ << "\t" << __FUNCTION__
124 
125 #define OMNI_TEST_MEMBER_EQUAL(member) \
126  (_lhs.member == _rhs.member)
127 
128 #define OMNI_TEST_PTR_MEMBER_EQUAL(member) \
129  omni::util::testPtrEqual(_lhs.member.get(), _rhs.member.get())
130 
131 
132 #endif /* OMNI_UTIL_H_ */
QString fileToStr(const QString &_filename)
Return content of file from a file name.
Definition: util.cpp:29
std::unique_ptr< T, QtDeleter > QUniquePtr
QUniquePtr for QObjects.
Definition: util.h:44
QString removeFileExt(QString const &_filename)
Remove file extension and return string without file extension.
Definition: util.cpp:37
bool fileExists(QString const &_filename)
Checks if file exists and is actually a file and not a directory.
Definition: util.cpp:49
void operator()(QOBJECT *_obj)
Definition: util.h:36
ENUM intToEnum(INT _i)
Converts an integer to enum.
Definition: util.h:113
bool testPtrVectorEqual(T const &_a, T const &_b, F f)
Test if two vectors which hold unique_ptr's of SerializationInterfaces.
Definition: util.h:78
T mix(const T &_x, const T &_y, A _a)
Linear interpolation between two values.
Definition: util.h:48
bool testPtrEqual(T const *_a, T const *_b)
Test if two instances (which inherit from SerializationInterface) have.
Definition: util.h:65
auto enumToInt(ENUM const &_v) -> typename std::underlying_type< ENUM >::type
Converts an enum class to integer.
Definition: util.h:105
Pointer deleter functor.
Definition: util.h:33