libyui  3.4.2
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 
29 
30 std::string
31 YProperty::typeAsStr( YPropertyType type )
32 {
33  switch ( type )
34  {
35  case YUnknownPropertyType: return "<Unknown>";
36  case YOtherProperty: return "<Other>";
37  case YStringProperty: return "String";
38  case YBoolProperty: return "Bool";
39  case YIntegerProperty: return "Integer";
40 
41  // Intentionally omitting default branch
42  // so the compiler catches unhandled enum values
43  }
44 
45  return "<Undefined property type>";
46 }
47 
48 
50 {
51 }
52 
53 bool YPropertyValue::operator==( const YPropertyValue &other ) const
54 {
55  // compare the type first
56  if (_type != other.type()) return false;
57 
58  // then compare the values
59  switch ( _type )
60  {
61  case YStringProperty: return _stringVal == other.stringVal();
62  case YBoolProperty: return _boolVal == other.boolVal();
63  case YIntegerProperty: return _integerVal == other.integerVal();
64 
65  case YUnknownPropertyType:
66  case YOtherProperty:
67  YUI_THROW( YUIException( "Cannot compare " + typeAsStr() + " properties") );
68  }
69 
70  // mark this part as unreachable to avoid "end of non-void function" error,
71  // YUI_THROW is a macro for a function template and cannot be marked as "noreturn"
72  __builtin_unreachable();
73 }
74 
75 bool YPropertyValue::operator!=( const YPropertyValue &other ) const
76 {
77  return !(*this == other);
78 }
79 
81 {
82  // NOP
83 }
84 
85 
86 void
87 YPropertySet::check( const std::string & propertyName ) const
88 {
89  if ( ! contains( propertyName ) )
90  YUI_THROW( YUIUnknownPropertyException( propertyName ) );
91 }
92 
93 
94 void
95 YPropertySet::check( const std::string & propertyName, YPropertyType type ) const
96 {
97  if ( ! contains( propertyName, type ) )
98  YUI_THROW( YUIUnknownPropertyException( propertyName ) );
99 
100  // YPropertySet::contains( const std::string &, YPropertyType ) will throw
101  // a YUIPropertyTypeMismatchException, if applicable
102 }
103 
104 
105 bool
106 YPropertySet::contains( const std::string & propertyName ) const throw()
107 {
108  for ( YPropertySet::const_iterator it = _properties.begin();
109  it != _properties.end();
110  ++it )
111  {
112  if ( it->name() == propertyName )
113  return true;
114  }
115 
116  return false;
117 }
118 
119 
120 bool
121 YPropertySet::contains( const std::string & propertyName, YPropertyType type ) const
122 {
123  for ( YPropertySet::const_iterator it = _properties.begin();
124  it != _properties.end();
125  ++it )
126  {
127  if ( it->name() == propertyName )
128  {
129  if ( it->isReadOnly() )
130  YUI_THROW( YUISetReadOnlyPropertyException( *it ) );
131 
132  if ( it->type() == type ||
133  it->type() == YOtherProperty ) // "Other" could be anything
134  return true;
135 
136  YUI_THROW( YUIPropertyTypeMismatchException( *it, type ) );
137  }
138  }
139 
140  return false;
141 }
142 
143 
144 void
146 {
147  _properties.push_back( prop );
148 }
149 
150 
151 void
152 YPropertySet::add( const YPropertySet & otherSet )
153 {
154  for ( YPropertySet::const_iterator it = otherSet.propertiesBegin();
155  it != otherSet.propertiesEnd();
156  ++it )
157  {
158  add( *it );
159  }
160 }
161 
162 
163 YPropertySet::const_iterator
165 {
166  return _properties.begin();
167 }
168 
169 YPropertySet::const_iterator
171 {
172  return _properties.end();
173 }
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:170
~YPropertyValue()
Destructor.
Definition: YProperty.cc:49
bool contains(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:106
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:145
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:75
bool operator==(const YPropertyValue &other) const
Equality operator, can compare with another YPropertyValue.
Definition: YProperty.cc:53
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:164
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:80
Class for widget properties.
Definition: YProperty.h:51
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:87
Base class for UI Exceptions.
Definition: YUIException.h:297
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169