Blue Brain BioExplorer
PropertyObject.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 
27 
28 #include <map>
29 
30 namespace core
31 {
36 class PropertyObject : public BaseObject
37 {
38 public:
40  void setCurrentType(const std::string& type)
41  {
43 
44  // add default (empty) property map for new type
45  if (_properties.count(type) == 0)
46  _properties[type];
47  }
48 
50  const std::string& getCurrentType() const { return _currentType; }
52  template <typename T>
53  inline void updateProperty(const std::string& name, const T& value, const bool triggerCallback = true)
54  {
55  auto& propMap = _properties.at(_currentType);
56  const auto oldValue = propMap.getProperty<T>(name, value);
57  if (!_isEqual(oldValue, value))
58  {
59  propMap.updateProperty(name, value);
60  markModified(triggerCallback);
61  }
62  }
63 
68  bool hasProperty(const std::string& name) const { return _properties.at(_currentType).hasProperty(name); }
69 
74  template <typename T>
75  inline T getProperty(const std::string& name) const
76  {
77  return _properties.at(_currentType).getProperty<T>(name);
78  }
79 
84  template <typename T>
85  inline T getPropertyOrValue(const std::string& name, T val) const
86  {
87  return hasProperty(name) ? getProperty<T>(name) : val;
88  }
89 
91  void setProperties(const PropertyMap& properties) { setProperties(_currentType, properties); }
92 
94  void setProperties(const std::string& type, const PropertyMap& properties)
95  {
96  _properties[type] = properties;
97  markModified();
98  }
99 
103  void updateProperties(const PropertyMap& properties)
104  {
105  _properties.at(_currentType).merge(properties);
106  markModified();
107  }
108 
110  const auto& getPropertyMap() const { return _properties.at(_currentType); }
112  const auto& getPropertyMap(const std::string& type) const { return _properties.at(type); }
113 
116  {
117  strings types;
118  for (const auto& i : _properties)
119  types.push_back(i.first);
120  return types;
121  }
122 
125  {
127  _properties.clear();
128  for (const auto& kv : obj._properties)
129  {
130  const auto& key = kv.first;
131  const auto& properties = kv.second.getProperties();
132 
133  PropertyMap propertyMapClone;
134  for (const auto& property : properties)
135  propertyMapClone.setProperty(*property);
136 
137  _properties[key] = propertyMapClone;
138  }
139  }
140 
141 protected:
142  std::string _currentType;
143  std::map<std::string, PropertyMap> _properties;
144 };
145 } // namespace core
void _updateValue(T &member, const T &newValue, const bool triggerCallback=true)
Definition: BaseObject.h:87
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
void setProperty(const Property &newProperty)
Definition: PropertyMap.h:307
void updateProperty(const std::string &name, const T &value, const bool triggerCallback=true)
void setCurrentType(const std::string &type)
const auto & getPropertyMap() const
std::string _currentType
void updateProperties(const PropertyMap &properties)
bool hasProperty(const std::string &name) const
strings getTypes() const
void setProperties(const std::string &type, const PropertyMap &properties)
const std::string & getCurrentType() const
T getProperty(const std::string &name) const
void setProperties(const PropertyMap &properties)
T getPropertyOrValue(const std::string &name, T val) const
void clonePropertiesFrom(const PropertyObject &obj)
const auto & getPropertyMap(const std::string &type) const
std::map< std::string, PropertyMap > _properties
std::vector< std::string > strings
Definition: Types.h:44