Debian Codegraph @

3rdparty/xspublic/xstypes/datapacket_p.cpp

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #include "datapacket_p.h"
34
35 /*! \cond XS_INTERNAL */
36
37 /*! \class DataPacketPrivate
38 \brief Internal administration for contained data of XsDataPacket class.
39 \details This is only the part that can be stored in an XsMessage, so no TOA and computed packet IDs
40 */
41
42 volatile std::atomic_int DataPacketPrivate::m_created(0);
43 volatile std::atomic_int DataPacketPrivate::m_destroyed(0);
44
45 /*! \brief Copy constructor */
46 DataPacketPrivate::DataPacketPrivate(DataPacketPrivate const& p)
47 : MapType() // start with clean map
48 , m_refCount(1) // this is a new object so the ref count is 1
49 {
50 ++m_created;
51 *this = p; // does NOT manipulate the ref count
52 }
53
54 /*! \brief Destructor */
55 DataPacketPrivate::~DataPacketPrivate()
56 {
57 ++m_destroyed;
58 try
59 {
60 clear();
61 }
62 catch(...)
63 {
64 }
65 }
66
67 /*! \brief Assignment operator
68 \param p The item to copy from
69 \note This does NOT manipulate the ref count and it shouldn't.
70 \return A reference to this
71 */
72 DataPacketPrivate& DataPacketPrivate::operator = (const DataPacketPrivate& p)
73 {
74 if (this != &p)
75 {
76 clear();
77 for (auto i : p)
78 insert(i.first, i.second->clone());
79 }
80 return *this;
81 }
82
83 /*! \brief Clear the contents */
84 void DataPacketPrivate::clear()
85 {
86 for (auto it : *this)
87 delete it.second;
88 MapType::clear();
89 }
90
91 /*! \brief Find the item matching \a id
92 \details This function will return an iterator to the item that matches \a id or end() if it could not find it.
93 The function uses a loose comparison to ensure entry ambiguity. As a result, the returned iterator may have a
94 slightly different id than the supplied one.
95 \param id The item to search for.
96 \return An iterator to the requested item or end()
97 */
98 MapType::const_iterator DataPacketPrivate::find(XsDataIdentifier id) const
99 {
100 return MapType::find(id & XDI_FullTypeMask);
101 }
102
103 /*! \brief Add or overwrite the item with \a id
104 \details The function will create a new item or overwite the current item, properly cleaning up existing data
105 if necessary.
106 \param id The id to attach to the data.
107 \param var The data to store.
108 \return An internal iterator pointing to the inserted or updated item.
109 */
110 MapType::iterator DataPacketPrivate::insert(XsDataIdentifier id, XsDataPacket_Private::Variant* var)
111 {
112 id = id & XDI_FullTypeMask;
113 auto it = MapType::lower_bound(id);
114 if (it != end() && it->first == id)
115 {
116 delete it->second;
117 it->second = var;
118 return it;
119 }
120 else
121 return MapType::insert(it, std::make_pair(id & XDI_FullTypeMask, var));
122 }
123
124 /*! \brief Remove the item with \a id if it exists, cleaning up associated data */
125 void DataPacketPrivate::erase(XsDataIdentifier id)
126 {
127 auto it = find(id);
128 if (it != end())
129 erase(it);
130 }
131
132 /*! \brief Remove the item at \a it, cleaning up associated data */
133 void DataPacketPrivate::erase(MapType::const_iterator const& it)
134 {
135 delete it->second;
136 MapType::erase(it);
137 }
138
139 /*! \brief Merge \a other into this
140 \details The function will copy all items from \a other into this. Existing items will only be overwritten if
141 \a overwrite is set to true. This function does not detect conflicts between closely related but IDs (such as
142 frame range and packet counter). This is left to the caller.
143 \param other The other data to merge into this packet
144 \param overwrite When set to true, the contents of \a other will overwrite existing data. When set to false, only new items will be added.
145 */
146 void DataPacketPrivate::merge(DataPacketPrivate const& other, bool overwrite)
147 {
148 if (overwrite)
149 for (auto i : other)
150 insert(i.first, i.second->clone());
151 else
152 {
153 for (auto i : other)
154 {
155 auto j = find(i.first);
156 if (j == end())
157 insert(i.first, i.second->clone());
158 }
159 }
160 }
161
162 /*! \brief Returns the difference between created and destroyed DataPacketPrivate objects, for debugging purposes only */
163 int DataPacketPrivate::creationDiff()
164 {
165 return m_created.load() - m_destroyed.load();
166 }
167
168 /*! \endcond XS_INTERNAL */