File: object.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (186 lines) | stat: -rw-r--r-- 5,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/************************************************************************
 *
 * Copyright (C) 2009-2025 IRCAD France
 * Copyright (C) 2012-2020 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "data/object.hpp"

#include "data/factory/new.hpp"

#include <core/com/signal.hxx>

#include <functional>

namespace sight::data
{

//------------------------------------------------------------------------------

object::object()
{
    new_signal<modified_signal_t>(signals::MODIFIED);
    new_signal<added_fields_signal_t>(signals::ADDED_FIELDS);
    new_signal<changed_fields_signal_t>(signals::CHANGED_FIELDS);
    new_signal<removed_fields_signal_t>(signals::REMOVED_FIELDS);
}

//------------------------------------------------------------------------------

data::object::sptr object::get_field(const field_name_t& _name, data::object::sptr _default_value) const
{
    data::object::sptr object = _default_value;
    auto iter                 = m_fields.find(_name);
    if(iter != m_fields.end())
    {
        object = iter->second;
    }

    return object;
}

//------------------------------------------------------------------------------

const object::field_map_t& object::get_fields() const
{
    return m_fields;
}

//------------------------------------------------------------------------------

object::field_name_vector_t object::get_field_names() const
{
    field_name_vector_t names;
    std::transform(
        m_fields.begin(),
        m_fields.end(),
        std::back_inserter(names),
        [](const auto& _e){return _e.first;});
    return names;
}

//------------------------------------------------------------------------------

void object::set_field(const field_name_t& _name, data::object::sptr _obj)
{
    std::pair<field_map_t::iterator, bool> res = m_fields.insert(field_map_t::value_type(_name, _obj));
    if(!res.second)
    {
        res.first->second = _obj;
    }
}

//------------------------------------------------------------------------------

void object::set_fields(const field_map_t& _field_map)
{
    m_fields = _field_map;
}

//------------------------------------------------------------------------------

void object::remove_field(const field_name_t& _name)
{
    auto iter = m_fields.find(_name);
    SIGHT_ASSERT("field " << _name << " not found.", iter != m_fields.end());
    if(iter != m_fields.end())
    {
        m_fields.erase(iter);
    }
}

//------------------------------------------------------------------------------

void object::deep_copy(const object::csptr& _source, const std::unique_ptr<deep_copy_cache_t>& _cache)
{
    SIGHT_ASSERT("Cache shall not be null !", _cache);

    // Copy members
    m_description = _source->m_description;

    // Deep copy fields
    m_fields.clear();

    for(const auto& [key, value] : _source->m_fields)
    {
        m_fields.insert_or_assign(key, object::copy(value, _cache));
    }
}

//-----------------------------------------------------------------------------

void object::shallow_copy(const object::csptr& _source)
{
    // Copy members
    m_description = _source->m_description;

    // Shallow copy fields
    m_fields = _source->m_fields;
}

//-----------------------------------------------------------------------------

data::object::sptr object::copy(const data::object::csptr& _source, const std::unique_ptr<deep_copy_cache_t>& _cache)
{
    SIGHT_ASSERT("Cache shall not be null !", _cache);

    if(!_source)
    {
        return nullptr;
    }

    // Check if the object is already in the cache
    if(const auto& it = _cache->find(_source); it != _cache->cend())
    {
        return it->second;
    }

    // Not found in cache, we crete a new copy
    auto object_copy = data::factory::make(_source->get_classname());
    SIGHT_ASSERT("Could not create object of type : " + _source->get_classname(), object_copy);

    // Add the object to the cache
    _cache->insert_or_assign(_source, object_copy);

    // Copy the object
    object_copy->deep_copy(_source, _cache);

    return object_copy;
}

//-----------------------------------------------------------------------------

bool object::operator==(const object& _other) const noexcept
{
    if(m_description != _other.m_description)
    {
        return false;
    }

    return core::is_equal(m_fields, _other.m_fields);
}

//------------------------------------------------------------------------------

bool object::operator!=(const object& _other) const noexcept
{
    return !(*this == _other);
}

} // namespace sight::data