libyui  3.1.5
 All Classes Files Functions Variables Typedefs Enumerations Friends Pages
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 
54 
56 {
57  // NOP
58 }
59 
60 
61 void
62 YPropertySet::check( const std::string & propertyName ) const
63 {
64  if ( ! contains( propertyName ) )
65  YUI_THROW( YUIUnknownPropertyException( propertyName ) );
66 }
67 
68 
69 void
70 YPropertySet::check( const std::string & propertyName, YPropertyType type ) const
71 {
72  if ( ! contains( propertyName, type ) )
73  YUI_THROW( YUIUnknownPropertyException( propertyName ) );
74 
75  // YPropertySet::contains( const std::string &, YPropertyType ) will throw
76  // a YUIPropertyTypeMismatchException, if applicable
77 }
78 
79 
80 bool
81 YPropertySet::contains( const std::string & propertyName ) const throw()
82 {
83  for ( YPropertySet::const_iterator it = _properties.begin();
84  it != _properties.end();
85  ++it )
86  {
87  if ( it->name() == propertyName )
88  return true;
89  }
90 
91  return false;
92 }
93 
94 
95 bool
96 YPropertySet::contains( const std::string & propertyName, YPropertyType type ) const
97 {
98  for ( YPropertySet::const_iterator it = _properties.begin();
99  it != _properties.end();
100  ++it )
101  {
102  if ( it->name() == propertyName )
103  {
104  if ( it->isReadOnly() )
105  YUI_THROW( YUISetReadOnlyPropertyException( *it ) );
106 
107  if ( it->type() == type ||
108  it->type() == YOtherProperty ) // "Other" could be anything
109  return true;
110 
111  YUI_THROW( YUIPropertyTypeMismatchException( *it, type ) );
112  }
113  }
114 
115  return false;
116 }
117 
118 
119 void
121 {
122  _properties.push_back( prop );
123 }
124 
125 
126 void
127 YPropertySet::add( const YPropertySet & otherSet )
128 {
129  for ( YPropertySet::const_iterator it = otherSet.propertiesBegin();
130  it != otherSet.propertiesEnd();
131  ++it )
132  {
133  add( *it );
134  }
135 }
136 
137 
138 YPropertySet::const_iterator
140 {
141  return _properties.begin();
142 }
143 
144 YPropertySet::const_iterator
146 {
147  return _properties.end();
148 }
Exception class for attempt to set a read-only property.
Definition: YUIException.h:597
void check(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:62
~YPropertyValue()
Destructor.
Definition: YProperty.cc:49
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:120
A set of properties to check names and types against.
Definition: YProperty.h:184
const_iterator propertiesBegin() const
Returns an iterator that points to the first property in this set.
Definition: YProperty.cc:139
bool contains(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:81
Exception class for "unknown property name": The application tried to set (or query) a property that ...
Definition: YUIException.h:537
YPropertySet()
Constructor.
Definition: YProperty.cc:55
Class for widget properties.
Definition: YProperty.h:51
const_iterator propertiesEnd() const
Returns an iterator that points after the last property in this set.
Definition: YProperty.cc:145
Exception class for "property type mismatch": The application tried to set a property with a wrong ty...
Definition: YUIException.h:562
std::string typeAsStr() const
Returns the type of this property as string.
Definition: YProperty.h:82