DecodeSignal: Fix typo
[pulseview.git] / pv / metadata_obj.cpp
blob3788dbc75907427dd69458b87475cc54f5274be6
1 /*
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"
23 namespace pv {
25 const char* MetadataObjectNames[MetadataObjectTypeCount] = {
26 "main_view_range",
27 "selection",
28 "time_marker",
29 "mouse_pos"
32 const char* MetadataValueNames[MetadataValueTypeCount] = {
33 "start_sample",
34 "end_sample"
35 "text"
39 void MetadataObjObserverInterface::on_metadata_object_created(MetadataObject* obj)
41 (void)obj;
44 void MetadataObjObserverInterface::on_metadata_object_deleted(MetadataObject* obj)
46 (void)obj;
49 void MetadataObjObserverInterface::on_metadata_object_changed(MetadataObject* obj,
50 MetadataValueType value_type)
52 (void)obj;
53 (void)value_type;
57 MetadataObject::MetadataObject(MetadataObjManager* obj_manager, uint32_t obj_id,
58 MetadataObjectType obj_type) :
59 obj_manager_(obj_manager),
60 id_(obj_id),
61 type_(obj_type)
63 // Make sure we accept all value type indices
64 values_.resize(MetadataValueTypeCount);
67 uint32_t MetadataObject::id() const
69 return id_;
72 MetadataObjectType MetadataObject::type() const
74 return type_;
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);
100 return 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; }),
110 objects_.end());
113 MetadataObject* MetadataObjManager::find_object_by_type(MetadataObjectType obj_type)
115 for (MetadataObject& obj : objects_)
116 if (obj.type() == obj_type)
117 return &obj;
119 return nullptr;
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++)
135 if (*cb_it == cb) {
136 callbacks_.erase(cb_it);
137 break;
141 void MetadataObjManager::save_objects(QSettings &settings) const
143 (void)settings;
146 void MetadataObjManager::restore_objects(QSettings &settings)
148 (void)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);
158 } // namespace pv