Re: Inhomogeneous containers as parameter lists
On 29/10/2010 09:54, d.avitabile@gmail.com wrote:
Hi all,
I am in the process of writing a code with plenty o parameter
dependences. My objects are Firms, that exchange goods in a Market, by
means of a Facilitator.
Now, each firm has a large amount of parameters that need to be
initialised and eventually passed to the facilitator. They are of
mixed type, some of them are bool, some int, some double, some
std::string. For instance, a Firm will have:
bool active
int numberOfContracts
double xCoordinate, yCoordinate
std:string name
What I would like to do is having a container that I can easily pass
to the facilitator. Let us say that the facilitator should be able to
make contact with the firm and get these information via the
container, so that I could do something like
container.get("Name", firmName);
container.get("Number Of contracts", numberOfContracts)
etc. to access the firm parameters. Do you know whether such container
exist in any libraries, or if I should code it from scratch?
Best.
Daniele
You mean something like this? It can probably be optimized in places,
but I found it worked very well in practice. Basic data structure:
std::map<std::string,boost::any>
Regards,
Stu
###
Properties.h:
###
/***
* hesperus: Properties.h
* Copyright Stuart Golodetz, 2009. All rights reserved.
***/
#ifndef H_HESP_PROPERTIES
#define H_HESP_PROPERTIES
#include <map>
#include <string>
#include <boost/any.hpp>
#include <boost/shared_ptr.hpp>
using boost::shared_ptr;
namespace hesp {
class Properties
{
//#################### PRIVATE VARIABLES ####################
private:
std::map<std::string,boost::any> m_properties;
//#################### PUBLIC METHODS ####################
public:
template <typename T> const T& get(const std::string& name) const;
bool has(const std::string& name) const;
template <typename T> void set(const std::string& name, const T& value);
//#################### PRIVATE METHODS ####################
private:
template <typename T> shared_ptr<T> get_ptr(const std::string& name)
const;
};
}
#include "Properties.tpp"
#endif
###
Properties.cpp:
###
/***
* hesperus: Properties.cpp
* Copyright Stuart Golodetz, 2009. All rights reserved.
***/
#include "Properties.h"
namespace hesp {
//#################### PUBLIC METHODS ####################
bool Properties::has(const std::string& name) const
{
return m_properties.find(name) != m_properties.end();
}
}
###
Properties.tpp:
###
/***
* hesperus: Properties.tpp
* Copyright Stuart Golodetz, 2009. All rights reserved.
***/
#include <hesp/exceptions/Exception.h>
namespace hesp {
//#################### PUBLIC METHODS ####################
template <typename T>
const T& Properties::get(const std::string& name) const
{
shared_ptr<T> p = get_ptr<T>(name);
if(p) return *p;
else throw Exception("Missing property: " + name);
}
template <typename T>
void Properties::set(const std::string& name, const T& value)
{
m_properties[name] = shared_ptr<T>(new T(value));
}
//#################### PRIVATE METHODS ####################
template <typename T>
shared_ptr<T> Properties::get_ptr(const std::string& name) const
{
if(!has(name)) return shared_ptr<T>();
try
{
return boost::any_cast<shared_ptr<T>
>(m_properties.find(name)->second);
}
catch(boost::bad_any_cast&)
{
throw Exception("The property " + name + " does not have the
specified type");
}
}
}