Commit 43347abb authored by schurade's avatar schurade
Browse files

Merge with a617e7d5d4024deb653de38834457614fd3d9b29

parents 8fc57f1a 08ccff2a
//---------------------------------------------------------------------------
//
// 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 <map>
#include <string>
#include <vector>
#include "WLogger.h"
#include "exceptions/WPropertyUnknown.h"
#include "WProperties2.h"
WProperties2::WProperties2():
m_iterationLock( boost::shared_lock< boost::shared_mutex >( m_updateLock ) )
{
m_iterationLock.unlock();
}
WProperties2::~WProperties2()
{
}
void WProperties2::addProperty( boost::shared_ptr< WPropertyBase > prop )
{
boost::unique_lock< boost::shared_mutex > lock = boost::unique_lock< boost::shared_mutex >( m_updateLock );
m_properties.insert( prop );
lock.unlock();
}
bool WProperties2::existsProperty( std::string name )
{
return ( findProperty( name ) != boost::shared_ptr< WPropertyBase >() );
}
boost::shared_ptr< WPropertyBase > WProperties2::getProperty( std::string name )
{
boost::shared_ptr< WPropertyBase > p = findProperty( name );
if ( findProperty( name ) == boost::shared_ptr< WPropertyBase >() )
{
throw WPropertyUnknown( "Property \"" + name + "\" can't be found." );
}
return p;
}
boost::shared_ptr< WPropertyBase > WProperties2::findProperty( std::string name )
{
boost::shared_ptr< WPropertyBase > result = boost::shared_ptr< WPropertyBase >();
// iterate over the items
for ( PropertyIterator it = beginIteration(); it != getPropertyIteratorEnd(); ++it )
{
if ( ( *it )->getName() == name )
{
result = ( *it );
break;
}
}
endIteration();
return result;
}
const WProperties2::PropertyIterator WProperties2::beginIteration()
{
m_iterationLock.lock();
return m_properties.begin();
}
void WProperties2::endIteration()
{
m_iterationLock.unlock();
}
const WProperties2::PropertyIterator WProperties2::getPropertyIteratorEnd() const
{
return m_properties.end();
}
//---------------------------------------------------------------------------
//
// 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 WPROPERTIES2_H
#define WPROPERTIES2_H
#include <map>
#include <string>
#include <set>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread.hpp>
#include "WPropertyBase.h"
#include "WPropertyVariable.h"
/**
* class to manage properties of an object and to provide convinience methods for easy access and manipulation. It also allows
* thread safe iteration on its elements.
*/
class WProperties2
{
public:
/**
* The iterator used to iterate over the property set
*/
typedef std::set< boost::shared_ptr< WPropertyBase > >::iterator PropertyIterator;
/**
* standard constructor
*/
WProperties2();
/**
* destructor
*/
virtual ~WProperties2();
/**
* Simply insert the specified property to the list.
*
* \param prop the property to add
*/
void addProperty( boost::shared_ptr< WPropertyBase > prop );
/**
* Helper function that finds a property by its name. Use this method to find out whether the property exists or not, since
* findProperty throws an exception.
*
* \param name name of searched property.
*/
bool existsProperty( std::string name );
/**
* Function searches the property. If it does not exists, it throws an exception.
*
* \param name the name of the property
*
* \return a WProperty object
*/
boost::shared_ptr< WPropertyBase > getProperty( std::string name );
/**
* Iterator over all property elements. This locks the property set for writing. endIteration() frees the lock.
*
* \return the list of properties.
*/
const PropertyIterator beginIteration();
/**
* To iterate over all set elements. Use this method to denote the end of iteration. This allows others to write to the set again.
*/
void endIteration();
/**
* Iterator denoting the end of the property set.
*
* \return the list of properties.
*/
const PropertyIterator getPropertyIteratorEnd() const;
/**
* Create and add a new property of the template type. For more details see appropriate constructor ow WPropertyVariable.
*
* \see WPropertyVariable
*
* \param name the property name
* \param description the property description
* \param initial the initial value
*/
template< typename T>
boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial );
/**
* Create and add a new property of the template type. For more details see appropriate constructor ow WPropertyVariable.
*
* \see WPropertyVariable
*
* \param name the property name
* \param description the property description
* \param initial the initial value
* \param condition use this external condition for notification.
*/
template< typename T>
boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
boost::shared_ptr< WCondition > condition );
/**
* Create and add a new property of the template type. For more details see appropriate constructor ow WPropertyVariable.
*
* \see WPropertyVariable
*
* \param name the property name
* \param description the property description
* \param initial the initial value
* \param notifier use this notifier for change callbacks.
*/
template< typename T>
boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
WCondition::t_ConditionNotifierType notifier );
/**
* Create and add a new property of the template type. For more details see appropriate constructor ow WPropertyVariable.
*
* \see WPropertyVariable
*
* \param name the property name
* \param description the property description
* \param initial the initial value
* \param notifier use this notifier for change callbacks.
* \param condition use this external condition for notification
*/
template< typename T>
boost::shared_ptr< WPropertyVariable< T > > addProperty( std::string name, std::string description, const T& initial,
boost::shared_ptr< WCondition > condition,
WCondition::t_ConditionNotifierType notifier );
private:
/**
* Searches the property with a given name. It does not throw any exception. It simply returns NULL if it can't be found.
*
* \param name the name of the property to search
*
* \return the property or NULL if not found.
*/
boost::shared_ptr< WPropertyBase > findProperty( std::string name );
/**
* The set of proerties. This uses the operators ==,<,> WProperty to determine equalnes.
*/
std::set< boost::shared_ptr< WPropertyBase > > m_properties;
/**
* boost mutex object for thread safety of updating of properties
*/
boost::shared_mutex m_updateLock;
/**
* The lock for a thread safe iteration.
*/
boost::shared_lock< boost::shared_mutex > m_iterationLock;
};
template< typename T>
boost::shared_ptr< WPropertyVariable< T > > WProperties2::addProperty( std::string name, std::string description, const T& initial )
{
return boost::shared_ptr< WPropertyVariable< T > >( new WPropertyVariable< T >( name, description, initial ) );
}
template< typename T>
boost::shared_ptr< WPropertyVariable< T > > WProperties2::addProperty( std::string name, std::string description, const T& initial,
boost::shared_ptr< WCondition > condition )
{
return boost::shared_ptr< WPropertyVariable< T > >( new WPropertyVariable< T >( name, description, initial, condition ) );
}
template< typename T>
boost::shared_ptr< WPropertyVariable< T > > WProperties2::addProperty( std::string name, std::string description, const T& initial,
WCondition::t_ConditionNotifierType notifier )
{
return boost::shared_ptr< WPropertyVariable< T > >( new WPropertyVariable< T >( name, description, initial, notifier ) );
}
template< typename T>
boost::shared_ptr< WPropertyVariable< T > > WProperties2::addProperty( std::string name, std::string description, const T& initial,
boost::shared_ptr< WCondition > condition,
WCondition::t_ConditionNotifierType notifier )
{
return boost::shared_ptr< WPropertyVariable< T > >( new WPropertyVariable< T >( name, description, initial, condition,
notifier ) );
}
#endif // WPROPERTIES2_H
......@@ -25,6 +25,13 @@
#ifndef WPROPERTYTYPES_H
#define WPROPERTYTYPES_H
#include <stdint.h>
#include <string>
#include <list>
#include <boost/filesystem.hpp>
/**
* Enum of all possible types, that can be used with WProperty.
*/
......@@ -40,4 +47,137 @@ typedef enum
}
PROPERTY_TYPE;
/**
* This namespace contains several helper classes which translate their template type to an enum.
*/
namespace PROPERTY_TYPE_HELPER
{
/**
* Class helping to adapt types specified as template parameter into an enum.
*/
template< typename T >
class WTypeIdentifier
{
public:
/**
* Get type identifier of the template type T.
*
* \return type identifier-
*/
PROPERTY_TYPE getType()
{
return UNKNOWN;
}
};
/**
* Class helping to adapt types specified as template parameter into an enum.
*/
template<>
class WTypeIdentifier< bool >
{
public:
/**
* Get type identifier of the template type T.
*
* \return type identifier-
*/
PROPERTY_TYPE getType()
{
return BOOL;
}
};
/**
* Class helping to adapt types specified as template parameter into an enum.
*/
template<>
class WTypeIdentifier< int32_t >
{
public:
/**
* Get type identifier of the template type T.
*
* \return type identifier-
*/
PROPERTY_TYPE getType()
{
return INT;
}
};
/**
* Class helping to adapt types specified as template parameter into an enum.
*/
template<>
class WTypeIdentifier< double >
{
public:
/**
* Get type identifier of the template type T.
*
* \return type identifier-
*/
PROPERTY_TYPE getType()
{
return DOUBLE;
}
};
/**
* Class helping to adapt types specified as template parameter into an enum.
*/
template<>
class WTypeIdentifier< std::string >
{
public:
/**
* Get type identifier of the template type T.
*
* \return type identifier-
*/
PROPERTY_TYPE getType()
{
return STRING;
}
};
/**
* Class helping to adapt types specified as template parameter into an enum.
*/
template<>
class WTypeIdentifier< boost::filesystem::path >
{
public:
/**
* Get type identifier of the template type T.
*
* \return type identifier-
*/
PROPERTY_TYPE getType()
{
return PATH;
}
};
/**
* Class helping to adapt types specified as template parameter into an enum.
*/
template<>
class WTypeIdentifier< std::list< std::string > >
{
public:
/**
* Get type identifier of the template type T.
*
* \return type identifier-
*/
PROPERTY_TYPE getType()
{
return LIST;
}
};
}
#endif // WPROPERTYTYPES_H
......@@ -170,41 +170,22 @@ WPropertyVariable< T >::~WPropertyVariable()
}
template < typename T >
bool WPropertyVariable< T >::accept( T newValue )
bool WPropertyVariable< T >::accept( T /* newValue */ )
{
// this currently is a dummy. Later this can be implemented to determine whether a value is valid.
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Implement the type function for each known type
// This is useful since the user does not need to specify some kind of type identifier
///////////////////////////////////////////////////////////////////////////////////////////////////
template < typename T >
void WPropertyVariable< T >::updateType()
{
m_type = UNKNOWN;
if ( typeid( int32_t ) == typeid( T ) )
{
m_type = INT;
}
else if ( typeid( double ) == typeid( T ) )
{
m_type = DOUBLE;
}
else if ( typeid( bool ) == typeid( T ) )
{
m_type = BOOL;
}
else if ( typeid( std::string ) == typeid( T ) )
{
m_type = STRING;
}
else if ( typeid( boost::filesystem::path ) == typeid( T ) )
{
m_type = PATH;
}
else if ( typeid( boost::filesystem::path ) == typeid( T ) )
{
m_type = LIST;
}
PROPERTY_TYPE_HELPER::WTypeIdentifier< T > tid;
m_type = tid.getType();
}
/**
......@@ -213,32 +194,32 @@ void WPropertyVariable< T >::updateType()
/**
* Alias for int32_t property variables.
*/
typedef WPropertyVariable< int32_t > WPropInt;
typedef boost::shared_ptr< WPropertyVariable< int32_t > > WPropInt;
/**
* Alias for int32_t property variables.
*/
typedef WPropertyVariable< double > WPropDouble;
typedef boost::shared_ptr< WPropertyVariable< double > > WPropDouble;
/**
* Alias for bool property variables.
*/
typedef WPropertyVariable< bool > WPropBool;
typedef boost::shared_ptr< WPropertyVariable< bool > > WPropBool;
/**
* Alias for string property variables.
*/
typedef WPropertyVariable< std::string > WPropString;
typedef boost::shared_ptr< WPropertyVariable< std::string > > WPropString;
/**
* Alias for filename property variables.
*/
typedef WPropertyVariable< boost::filesystem::path > WPropFilename;
typedef boost::shared_ptr< WPropertyVariable< boost::filesystem::path > > WPropFilename;
/**
* Alias for string list property variables.
*/
typedef WPropertyVariable< std::list< std::string > > WPropList;
typedef boost::shared_ptr< WPropertyVariable< std::list< std::string > > > WPropList;
#endif // WPROPERTYVARIABLE_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 "WPropertyUnknown.h"
WPropertyUnknown::WPropertyUnknown( const std::string& msg )
: WException( msg )
{
// init members
}
WPropertyUnknown::~WPropertyUnknown() throw()
{
// clean up
}
//---------------------------------------------------------------------------
//
// 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 WPROPERTYUNKNOWN_H