libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YProperty.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:         YProperty.cc
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #include "YProperty.h"
00026 #include "YUIException.h"
00027 
00028 
00029 
00030 std::string
00031 YProperty::typeAsStr( YPropertyType type )
00032 {
00033     switch ( type )
00034     {
00035         case YUnknownPropertyType:      return "<Unknown>";
00036         case YOtherProperty:            return "<Other>";
00037         case YStringProperty:           return "String";
00038         case YBoolProperty:             return "Bool";
00039         case YIntegerProperty:          return "Integer";
00040 
00041             // Intentionally omitting default branch
00042             // so the compiler catches unhandled enum values
00043     }
00044 
00045     return "<Undefined property type>";
00046 }
00047 
00048 
00049 YPropertyValue::~YPropertyValue()
00050 {
00051 }
00052 
00053 
00054 
00055 YPropertySet::YPropertySet()
00056 {
00057     // NOP
00058 }
00059 
00060 
00061 void
00062 YPropertySet::check( const std::string & propertyName ) const
00063 {
00064     if ( ! contains( propertyName ) )
00065         YUI_THROW( YUIUnknownPropertyException( propertyName ) );
00066 }
00067 
00068 
00069 void
00070 YPropertySet::check( const std::string & propertyName, YPropertyType type ) const
00071 {
00072     if ( ! contains( propertyName, type ) )
00073         YUI_THROW( YUIUnknownPropertyException( propertyName ) );
00074 
00075     // YPropertySet::contains( const std::string &, YPropertyType ) will throw
00076     // a YUIPropertyTypeMismatchException, if applicable
00077 }
00078 
00079 
00080 bool
00081 YPropertySet::contains( const std::string & propertyName ) const throw()
00082 {
00083     for ( YPropertySet::const_iterator it = _properties.begin();
00084           it != _properties.end();
00085           ++it )
00086     {
00087         if ( it->name() == propertyName )
00088             return true;
00089     }
00090 
00091     return false;
00092 }
00093 
00094 
00095 bool
00096 YPropertySet::contains( const std::string & propertyName, YPropertyType type ) const
00097 {
00098     for ( YPropertySet::const_iterator it = _properties.begin();
00099           it != _properties.end();
00100           ++it )
00101     {
00102         if ( it->name() == propertyName )
00103         {
00104             if ( it->isReadOnly() )
00105                 YUI_THROW( YUISetReadOnlyPropertyException( *it ) );
00106 
00107             if ( it->type() == type ||
00108                  it->type() == YOtherProperty ) // "Other" could be anything
00109                 return true;
00110 
00111             YUI_THROW( YUIPropertyTypeMismatchException( *it, type ) );
00112         }
00113     }
00114 
00115     return false;
00116 }
00117 
00118 
00119 void
00120 YPropertySet::add( const YProperty & prop )
00121 {
00122     _properties.push_back( prop );
00123 }
00124 
00125 
00126 void
00127 YPropertySet::add( const YPropertySet & otherSet )
00128 {
00129     for ( YPropertySet::const_iterator it = otherSet.propertiesBegin();
00130           it != otherSet.propertiesEnd();
00131           ++it )
00132     {
00133         add( *it );
00134     }
00135 }
00136 
00137 
00138 YPropertySet::const_iterator
00139 YPropertySet::propertiesBegin() const
00140 {
00141     return _properties.begin();
00142 }
00143 
00144 YPropertySet::const_iterator
00145 YPropertySet::propertiesEnd() const
00146 {
00147     return _properties.end();
00148 }
 All Classes Functions Variables Enumerations Friends