Commit c9664d69 authored by schurade's avatar schurade
Browse files

[ADD] the property classes for the last commit, this stupid system hadn't checked them

parent 8bd0398d
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include "../common/WLogger.h"
#include "WProperties.h"
WProperties::WProperties()
{
}
WProperties::~WProperties()
{
}
void WProperties::addBool( std::string name, bool value, std::string shortDesc, std::string longDesc )
{
WProperty* prop = new WProperty( name, value, shortDesc, longDesc );
m_propertyList.push_back( prop );
}
void WProperties::addChar( std::string name, char value, std::string shortDesc, std::string longDesc )
{
WProperty* prop = new WProperty( name, value, shortDesc, longDesc );
m_propertyList.push_back( prop );
}
void WProperties::addInt( std::string name, int value, std::string shortDesc, std::string longDesc )
{
WProperty* prop = new WProperty( name, value, shortDesc, longDesc );
m_propertyList.push_back( prop );
}
void WProperties::addFloat( std::string name, float value, std::string shortDesc, std::string longDesc )
{
WProperty* prop = new WProperty( name, value, shortDesc, longDesc );
m_propertyList.push_back( prop );
}
void WProperties::addDouble( std::string name, double value, std::string shortDesc, std::string longDesc )
{
WProperty* prop = new WProperty( name, value, shortDesc, longDesc );
m_propertyList.push_back( prop );
}
void WProperties::addString( std::string name, std::string value, std::string shortDesc, std::string longDesc )
{
WProperty* prop = new WProperty( name, value, shortDesc, longDesc );
m_propertyList.push_back( prop );
}
WProperty* WProperties::findProp( std::string name )
{
for ( size_t i = 0 ; i < m_propertyList.size(); ++i)
{
if ( m_propertyList[i]->getName() == name )
{
return m_propertyList[i];
}
}
WLogger::getLogger()->addLogMessage( "Property doesn't exist", "Properties", LL_ERROR );
std::cout << name << std::endl;
return 0;
}
std::string WProperties::getValueString( std::string prop )
{
if ( findProp( prop ) )
{
return findProp( prop )->getValueString();
}
return "";
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPROPERTIES_H
#define WPROPERTIES_H
#include <string>
#include <vector>
#include "WProperty.h"
/**
* class to manage properties of an object and to provide convinience methods for easy access and manipulation
*/
class WProperties
{
public:
/**
* standard constructor
*/
explicit WProperties();
/**
* destructor
*/
virtual ~WProperties();
void addBool( std::string name, bool value = false, std::string shortDesc = "", std::string longDesc = "" );
void addChar( std::string name, char value = 0, std::string shortDesc = "", std::string longDesc = "" );
void addInt( std::string name, int value = 0, std::string shortDesc = "", std::string longDesc = "" );
void addFloat( std::string name, float value = 0.0, std::string shortDesc = "", std::string longDesc = "" );
void addDouble( std::string name, double value = 0.0, std::string shortDesc = "", std::string longDesc = "" );
void addString( std::string name, std::string value = "", std::string shortDesc = "", std::string longDesc = "" );
std::string getValueString( std::string prop );
template < typename T > void setValue( std::string prop, const T& arg )
{
if ( findProp( prop ) )
{
findProp( prop )->setValue( arg );
}
}
template < typename T > T getValue( std::string prop )
{
if ( findProp( prop ) )
{
return findProp( prop )->getValue< T >();
}
return 0;
}
private:
WProperty* findProp( std::string name );
std::vector < WProperty* >m_propertyList;
};
#endif // WPROPERTIES_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "WProperty.h"
WProperty::WProperty( std::string name, std::string value, std::string shortDesc, std::string longDesc )
: m_name( name ),
m_value( value ),
m_shortDesc( shortDesc ),
m_longDesc( longDesc )
{
m_type = P_STRING;
}
WProperty::WProperty( std::string name, bool value, std::string shortDesc, std::string longDesc )
: m_name( name ),
m_shortDesc( shortDesc ),
m_longDesc( longDesc )
{
m_type = P_BOOL;
setValue( value );
}
WProperty::WProperty( std::string name, char value, std::string shortDesc, std::string longDesc )
: m_name( name ),
m_shortDesc( shortDesc ),
m_longDesc( longDesc )
{
m_type = P_CHAR;
setValue( value );
}
WProperty::WProperty( std::string name, int value, std::string shortDesc, std::string longDesc )
: m_name( name ),
m_shortDesc( shortDesc ),
m_longDesc( longDesc )
{
m_type = P_INT;
setValue( value );
}
WProperty::WProperty( std::string name, float value, std::string shortDesc, std::string longDesc )
: m_name( name ),
m_shortDesc( shortDesc ),
m_longDesc( longDesc )
{
m_type = P_FLOAT;
setValue( value );
}
WProperty::WProperty( std::string name, double value, std::string shortDesc, std::string longDesc )
: m_name( name ),
m_shortDesc( shortDesc ),
m_longDesc( longDesc )
{
m_type = P_DOUBLE;
setValue( value );
}
WProperty::~WProperty()
{
}
PropertyType WProperty::getType()
{
return m_type;
}
std::string WProperty::getName()
{
return m_name;
}
void WProperty::setShortDesc( const std::string desc )
{
m_shortDesc = desc;
}
void WProperty::setLongDesc( const std::string desc )
{
m_longDesc = desc;
}
std::string WProperty::getShortDesc()
{
return m_shortDesc;
}
std::string WProperty::getLongDesc()
{
return m_longDesc;
}
std::string WProperty::getValueString()
{
return m_value;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPROPERTY_H
#define WPROPERTY_H
#include <string>
#include <boost/lexical_cast.hpp>
typedef enum
{
P_BOOL = 0,
P_CHAR,
P_INT,
P_FLOAT,
P_DOUBLE,
P_STRING
}
PropertyType;
/**
* class to encapsulate one property
*/
class WProperty
{
public:
/**
* constructors
*/
WProperty( std::string name, std::string value, std::string shortDesc = "", std::string longDesc = "" );
WProperty( std::string name, bool value, std::string shortDesc = "", std::string longDesc = "" );
WProperty( std::string name, char value, std::string shortDesc = "", std::string longDesc = "" );
WProperty( std::string name, int value, std::string shortDesc = "", std::string longDesc = "" );
WProperty( std::string name, float value, std::string shortDesc = "", std::string longDesc = "" );
WProperty( std::string name, double value, std::string shortDesc = "", std::string longDesc = "" );
/**
* destructor
*/
virtual ~WProperty();
PropertyType getType();
std::string getName();
void setShortDesc( const std::string desc );
void setLongDesc( const std::string desc );
std::string getShortDesc();
std::string getLongDesc();
template < typename T > void setValue( const T& arg )
{
try
{
m_value = boost::lexical_cast< std::string >( arg );
}
catch( const boost::bad_lexical_cast& e )
{
m_value = "";
}
}
template < typename T > T getValue()
{
return boost::lexical_cast< T >( m_value );
}
std::string getValueString();
private:
PropertyType m_type;
std::string m_name;
std::string m_value;
std::string m_shortDesc;
std::string m_longDesc;
};
#endif // WPROPERTY_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPROPERTIES_TEST_H
#define WPROPERTIES_TEST_H
#include <cxxtest/TestSuite.h>
#include "../WProperties.h"
/**
* unit tests the properties class
*/
class WPropertiesTest : public CxxTest::TestSuite
{
public:
/**
* tests creating an integer property
*/
void testAddProp( void )
{
WProperties props;
props.addInt( "test", 57, "test57", "longDesc57" );
TS_ASSERT_EQUALS( props.getValue<int>( "test" ), 57 );
}
/**
* tests creating an integer property and changing its value
*/
void testChangeProp( void )
{
WProperties props;
props.addInt( "test", 57, "test57", "longDesc57" );
props.setValue( "test", 61 );
TS_ASSERT_EQUALS( props.getValue<int>( "test" ), 61 );
}
};
#endif // WPROPERTIES_TEST_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPROPERTY_TEST_H
#define WPROPERTY_TEST_H
#include <cxxtest/TestSuite.h>
#include "../WProperty.h"
/**
* unit tests the property class
*/
class WPropertyTest : public CxxTest::TestSuite
{
public:
/**
* Tests the property class by creating a boolean property
*/
void testBool( void )
{
WProperty prop( "testProp", true );
TS_ASSERT_EQUALS( prop.getValue<bool>(), true );
}
void testBoolSetFalse( void )
{
WProperty prop( "testProp", true );
prop.setValue( false );
TS_ASSERT_EQUALS( prop.getValue<bool>(), false );
}
void testBoolGetString( void )
{
WProperty prop( "testProp", true );
TS_ASSERT_EQUALS( prop.getValueString(), "1" );
}
};
#endif // WPROPERTY_TEST_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment