Blue Brain BioExplorer
BaseObject.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  *
4  * Responsible Author: Daniel.Nachbaur@epfl.ch
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #pragma once
23 
24 #include <atomic>
25 #include <cmath>
26 #include <functional>
27 #include <type_traits>
28 
29 namespace core
30 {
32 {
33 public:
34  BaseObject() = default;
35  virtual ~BaseObject() = default;
36 
42  : _modified(true)
43  {
44  }
45 
48  {
49  if (this == &rhs)
50  return *this;
51 
52  _modified = true;
53  return *this;
54  }
55 
60  bool isModified() const { return _modified; }
64  void resetModified() { _modified = false; }
65  void markModified(const bool triggerCallback = true)
66  {
67  _modified = true;
68  if (_modifiedCallback && triggerCallback)
69  _modifiedCallback(*this);
70  }
71 
72  using ModifiedCallback = std::function<void(const BaseObject&)>;
73 
77  void onModified(const ModifiedCallback& callback) { _modifiedCallback = callback; }
78 
79  void clearModifiedCallback() { _modifiedCallback = ModifiedCallback(); }
80 
81 protected:
86  template <typename T>
87  void _updateValue(T& member, const T& newValue, const bool triggerCallback = true)
88  {
89  if (!_isEqual(member, newValue))
90  {
91  member = newValue;
92  markModified(triggerCallback);
93  }
94  }
95 
96  template <class T>
97  bool _isEqual(const T& a, const T& b, typename std::enable_if<std::is_floating_point<T>::value>::type* = 0)
98  {
99  return std::fabs(a - b) < 0.000001;
100  }
101 
102  template <class T>
103  bool _isEqual(const T& a, const T& b, typename std::enable_if<!std::is_floating_point<T>::value>::type* = 0)
104  {
105  return a == b;
106  }
107 
108 private:
109  std::atomic_bool _modified{true};
110  ModifiedCallback _modifiedCallback;
111 };
112 } // namespace core
void _updateValue(T &member, const T &newValue, const bool triggerCallback=true)
Definition: BaseObject.h:87
void resetModified()
Definition: BaseObject.h:64
BaseObject()=default
void markModified(const bool triggerCallback=true)
Definition: BaseObject.h:65
bool _isEqual(const T &a, const T &b, typename std::enable_if< std::is_floating_point< T >::value >::type *=0)
Definition: BaseObject.h:97
virtual ~BaseObject()=default
BaseObject(const BaseObject &)
Definition: BaseObject.h:41
void clearModifiedCallback()
Definition: BaseObject.h:79
bool isModified() const
Definition: BaseObject.h:60
bool _isEqual(const T &a, const T &b, typename std::enable_if<!std::is_floating_point< T >::value >::type *=0)
Definition: BaseObject.h:103
void onModified(const ModifiedCallback &callback)
Definition: BaseObject.h:77
std::function< void(const BaseObject &)> ModifiedCallback
Definition: BaseObject.h:72
BaseObject & operator=(const BaseObject &rhs)
Definition: BaseObject.h:47