libyui  3.9.3
YProperty.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YProperty.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #include "YProperty.h"
26 #include "YUIException.h"
27 
28 using std::string;
29 
30 
31 string
32 YProperty::typeAsStr( YPropertyType type )
33 {
34  switch ( type )
35  {
36  case YUnknownPropertyType: return "<Unknown>";
37  case YOtherProperty: return "<Other>";
38  case YStringProperty: return "String";
39  case YBoolProperty: return "Bool";
40  case YIntegerProperty: return "Integer";
41 
42  // Intentionally omitting default branch
43  // so the compiler catches unhandled enum values
44  }
45 
46  return "<Undefined property type>";
47 }
48 
49 
51 {
52 }
53 
54 bool YPropertyValue::operator==( const YPropertyValue &other ) const
55 {
56  // compare the type first
57  if (_type != other.type()) return false;
58 
59  // then compare the values
60  switch ( _type )
61  {
62  case YStringProperty: return _stringVal == other.stringVal();
63  case YBoolProperty: return _boolVal == other.boolVal();
64  case YIntegerProperty: return _integerVal == other.integerVal();
65 
66  case YUnknownPropertyType:
67  case YOtherProperty:
68  YUI_THROW( YUIException( "Cannot compare " + typeAsStr() + " properties") );
69  }
70 
71  // mark this part as unreachable to avoid "end of non-void function" error,
72  // YUI_THROW is a macro for a function template and cannot be marked as "noreturn"
73  __builtin_unreachable();
74 }
75 
76 bool YPropertyValue::operator!=( const YPropertyValue &other ) const
77 {
78  return !(*this == other);
79 }
80 
82 {
83  // NOP
84 }
85 
86 
87 void
88 YPropertySet::check( const string & propertyName ) const
89 {
90  if ( ! contains( propertyName ) )
91  YUI_THROW( YUIUnknownPropertyException( propertyName ) );
92 }
93 
94 
95 void
96 YPropertySet::check( const string & propertyName, YPropertyType type ) const
97 {
98  if ( ! contains( propertyName, type ) )
99  YUI_THROW( YUIUnknownPropertyException( propertyName ) );
100 
101  // YPropertySet::contains( const string &, YPropertyType ) will throw
102  // a YUIPropertyTypeMismatchException, if applicable
103 }
104 
105 
106 bool
107 YPropertySet::contains( const string & propertyName ) const throw()
108 {
109  for ( YPropertySet::const_iterator it = _properties.begin();
110  it != _properties.end();
111  ++it )
112  {
113  if ( it->name() == propertyName )
114  return true;
115  }
116 
117  return false;
118 }
119 
120 
121 bool
122 YPropertySet::contains( const string & propertyName, YPropertyType type ) const
123 {
124  for ( YPropertySet::const_iterator it = _properties.begin();
125  it != _properties.end();
126  ++it )
127  {
128  if ( it->name() == propertyName )
129  {
130  if ( it->isReadOnly() )
131  YUI_THROW( YUISetReadOnlyPropertyException( *it ) );
132 
133  if ( it->type() == type ||
134  it->type() == YOtherProperty ) // "Other" could be anything
135  return true;
136 
137  YUI_THROW( YUIPropertyTypeMismatchException( *it, type ) );
138  }
139  }
140 
141  return false;
142 }
143 
144 
145 void
147 {
148  _properties.push_back( prop );
149 }
150 
151 
152 void
153 YPropertySet::add( const YPropertySet & otherSet )
154 {
155  for ( YPropertySet::const_iterator it = otherSet.propertiesBegin();
156  it != otherSet.propertiesEnd();
157  ++it )
158  {
159  add( *it );
160  }
161 }
162 
163 
164 YPropertySet::const_iterator
166 {
167  return _properties.begin();
168 }
169 
170 YPropertySet::const_iterator
172 {
173  return _properties.end();
174 }
Exception class for attempt to set a read-only property.
Definition: YUIException.h:613
Transport class for the value of simple properties.
Definition: YProperty.h:104
const_iterator propertiesEnd() const
Returns an iterator that points after the last property in this set.
Definition: YProperty.cc:171
~YPropertyValue()
Destructor.
Definition: YProperty.cc:50
bool contains(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:107
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:146
YPropertyType type() const
Returns the type of this property.
Definition: YProperty.h:72
A set of properties to check names and types against.
Definition: YProperty.h:197
bool operator!=(const YPropertyValue &other) const
Inequality operator.
Definition: YProperty.cc:76
bool operator==(const YPropertyValue &other) const
Equality operator, can compare with another YPropertyValue.
Definition: YProperty.cc:54
std::string typeAsStr() const
Returns the type of this property as string.
Definition: YProperty.h:82
const_iterator propertiesBegin() const
Returns an iterator that points to the first property in this set.
Definition: YProperty.cc:165
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
Exception class for "unknown property name": The application tried to set (or query) a property that ...
Definition: YUIException.h:553
YPropertySet()
Constructor.
Definition: YProperty.cc:81
Class for widget properties.
Definition: YProperty.h:51
std::string typeAsStr() const
Returns the type of this property value as string.
Definition: YProperty.h:174
Exception class for "property type mismatch": The application tried to set a property with a wrong ty...
Definition: YUIException.h:578
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:88
Base class for UI Exceptions.
Definition: YUIException.h:297
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169