ArnLib  4.0.x
Active Registry Network
ArnLinkHandle.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #if defined __GNUC__ && __GNUC__ >= 6
33 # pragma GCC diagnostic ignored "-Wnull-dereference"
34 #endif
35 
36 #include "ArnInc/ArnLinkHandle.hpp"
37 #include <QDebug>
38 
39 using namespace Arn;
40 
41 
43 
44 void ArnLinkHandle::init()
45 {
46  _codes = Normal;
47  _data = arnNullptr;
48 }
49 
50 
51 ArnLinkHandle::ArnLinkHandle()
52 {
53  init();
54 }
55 
56 
57 ArnLinkHandle::ArnLinkHandle( const ArnLinkHandle& other)
58 {
59  if ( isNullPtr( &other)) { // other is NULL
60  init();
61  return;
62  }
63 
64  _codes = other._codes;
65  _flags = other._flags;
66  if (other._data)
67  _data = new HandleData( *other._data);
68  else
69  _data = arnNullptr;
70 }
71 
72 
73 ArnLinkHandle::ArnLinkHandle( const ArnLinkHandle::Flags& flags)
74 {
75  init();
76  _flags = flags;
77 }
78 
79 
80 ArnLinkHandle::~ArnLinkHandle()
81 {
82  if (isNullPtr( this)) return; // Shouldn't be called ...
83 
84  if (_data)
85  delete _data;
86 }
87 
88 
89 const ArnLinkHandle& ArnLinkHandle::null()
90 {
91  return *nullPtr();
92 }
93 
94 
95 const ArnLinkHandle* ArnLinkHandle::nullPtr()
96 {
97  return static_cast<ArnLinkHandle*>( arnNullptr);
98 }
99 
100 
101 const ArnLinkHandle::Flags& ArnLinkHandle::flags() const
102 {
103  static Flags nullFlags;
104  if (isNullPtr( this)) return nullFlags; // ArnHandle is NULL
105 
106  return _flags;
107 }
108 
109 
110 ArnLinkHandle::Flags& ArnLinkHandle::flags()
111 {
112  static Flags nullFlags;
113  if (isNullPtr( this)) return nullFlags; // ArnHandle is NULL, should not be used ...
114 
115  return _flags;
116 }
117 
118 
119 ArnLinkHandle& ArnLinkHandle::add( Code code, const QVariant& value)
120 {
121  if (isNullPtr( this)) return *this; // ArnHandle is NULL
122  if (code == Normal) return *this; // Adding nothing
123 
124  if (!_data)
125  _data = new HandleData;
126  _data->insert( code, value);
127  _codes |= code;
128  return *this;
129 }
130 
131 
132 bool ArnLinkHandle::has( Code code) const
133 {
134  if (isNullPtr( this)) return false; // ArnHandle is NULL
135 
136  return _codes.testFlag( code);
137 }
138 
139 
140 bool ArnLinkHandle::isNull() const
141 {
142  if (isNullPtr( this)) return true; // ArnHandle is NULL
143 
144  return (_codes == Normal) && (_flags == Flags());
145 }
146 
147 
148 const QVariant& ArnLinkHandle::valueRef( Code code) const
149 {
150  static QVariant nullValue;
151 
152  if (isNullPtr( this)) return nullValue; // ArnHandle is NULL
153 
154  if (!_data || !has( code)) // Should not be used ...
155  return nullValue;
156 
157  return _data->constFind( code).value();
158 }
bool isNullPtr(const void *ptr)
Definition: Arn.cpp:253
Definition: Arn.cpp:43