2 * This file is part of the PulseView project.
4 * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "metadata_obj.hpp"
25 const char* MetadataObjectNames
[MetadataObjectTypeCount
] = {
32 const char* MetadataValueNames
[MetadataValueTypeCount
] = {
39 void MetadataObjObserverInterface::on_metadata_object_created(MetadataObject
* obj
)
44 void MetadataObjObserverInterface::on_metadata_object_deleted(MetadataObject
* obj
)
49 void MetadataObjObserverInterface::on_metadata_object_changed(MetadataObject
* obj
,
50 MetadataValueType value_type
)
57 MetadataObject::MetadataObject(MetadataObjManager
* obj_manager
, uint32_t obj_id
,
58 MetadataObjectType obj_type
) :
59 obj_manager_(obj_manager
),
63 // Make sure we accept all value type indices
64 values_
.resize(MetadataValueTypeCount
);
67 uint32_t MetadataObject::id() const
72 MetadataObjectType
MetadataObject::type() const
77 void MetadataObject::set_value(MetadataValueType value_type
, const QVariant
& value
)
79 values_
.at((uint8_t)value_type
) = value
;
80 obj_manager_
->notify_observers(this, value_type
);
83 QVariant
MetadataObject::value(MetadataValueType value_type
) const
85 return values_
.at((uint8_t)value_type
);
89 MetadataObject
* MetadataObjManager::create_object(MetadataObjectType obj_type
)
91 // Note: This function is not reentrant as race conditions between
92 // emplace_back() and back() may occur
94 objects_
.emplace_back(this, objects_
.size(), obj_type
);
95 MetadataObject
* obj
= &(objects_
.back());
97 for (MetadataObjObserverInterface
*cb
: callbacks_
)
98 cb
->on_metadata_object_created(obj
);
103 void MetadataObjManager::delete_object(uint32_t obj_id
)
105 for (MetadataObjObserverInterface
*cb
: callbacks_
)
106 cb
->on_metadata_object_deleted(&(objects_
.at(obj_id
)));
108 objects_
.erase(std::remove_if(objects_
.begin(), objects_
.end(),
109 [&](MetadataObject obj
) { return obj
.id() == obj_id
; }),
113 MetadataObject
* MetadataObjManager::find_object_by_type(MetadataObjectType obj_type
)
115 for (MetadataObject
& obj
: objects_
)
116 if (obj
.type() == obj_type
)
122 MetadataObject
* MetadataObjManager::object(uint32_t obj_id
)
124 return &(objects_
.at(obj_id
));
127 void MetadataObjManager::add_observer(MetadataObjObserverInterface
*cb
)
129 callbacks_
.emplace_back(cb
);
132 void MetadataObjManager::remove_observer(MetadataObjObserverInterface
*cb
)
134 for (auto cb_it
= callbacks_
.begin(); cb_it
!= callbacks_
.end(); cb_it
++)
136 callbacks_
.erase(cb_it
);
141 void MetadataObjManager::save_objects(QSettings
&settings
) const
146 void MetadataObjManager::restore_objects(QSettings
&settings
)
151 void MetadataObjManager::notify_observers(MetadataObject
* obj
,
152 MetadataValueType changed_value
)
154 for (MetadataObjObserverInterface
*cb
: callbacks_
)
155 cb
->on_metadata_object_changed(obj
, changed_value
);