Commit 50fe41b3 authored by Laurent Belcour's avatar Laurent Belcour

Updating the interface of ALTA to use shared_pointers. This is not a stable...

Updating the interface of ALTA to use shared_pointers. This is not a stable REV. Do not update to it if you plan to compile ALTA.
parent e3eaa038
......@@ -98,8 +98,8 @@ env.AppendUnique(CPPPATH = ['#sources'])
Export('env')
external = env.SConscript('external/SConscript')
core = env.SConscript('sources/core/SConscript')
#plugins = env.SConscript('sources/plugins/SConscript')
softs = env.SConscript('sources/softs/SConscript')
#sources = env.SConscript('sources/SConscript')
#env.Depends(sources, external)
core = env.SConscript('sources/core/SConscript')
plugins = env.SConscript('sources/plugins/SConscript')
softs = env.SConscript('sources/softs/SConscript')
env.NoClean(external)
\ No newline at end of file
......@@ -12,7 +12,7 @@ class clustering : public data
//! \brief constructor loading a full dimension data and clustering
//! it into a low dimension one.
clustering::clustering(const data* d, const arguments& args);
clustering::clustering(const ptr<data> d, const arguments& args);
//! \brief the clustering class can save a clustering to a file.
virtual void save(const std::string& filename);
......
......@@ -10,6 +10,7 @@
#include "args.h"
#include "params.h"
#include "clustering.h"
#include "ptr.h"
/*! \brief A data object. Allows to load data from files.
* \ingroup core
......@@ -18,6 +19,9 @@ class data : public parametrized
{
public: // methods
// Virtual destructor
virtual ~data() {}
// Load data from a file
virtual void load(const std::string& filename) = 0 ;
virtual void load(const std::string& filename, const arguments& args) = 0 ;
......@@ -98,7 +102,7 @@ class data_params : public data
//! \brief contructor requires the definition of a base class that
//! has a parametrization, and a new parametrization.
data_params(const data* d, params::input new_param,
data_params(const ptr<data> d, params::input new_param,
data_params::clustrering method = data_params::NONE) :
_clustering_method(method)
{
......@@ -109,7 +113,8 @@ class data_params : public data
_nY = d->dimY();
std::cout << "<<INFO>> Reparametrization of the data" << std::endl;
clustering<data>(d, _nY, d->parametrization(), new_param, _data);
//TODO
//clustering<data>(d, _nY, d->parametrization(), new_param, _data);
std::cout << "<<INFO>> clustering left " << _data.size() << "/" << d->size() << " elements" << std::endl;
save(std::string("cluster.gnuplot"));
......
......@@ -14,10 +14,13 @@ class fitter
{
public:
// Virtual destructor
virtual ~fitter() {}
//! \brief static function to fit a data set d with the underling
//! function class. Return the best fit (along with fitting
//! information ?)
virtual bool fit_data(const data* d, function* f, const arguments& args) = 0 ;
virtual bool fit_data(const ptr<data> d, function* f, const arguments& args) = 0 ;
//! \brief parse the command line arguments to setup some general
//! options before any fit. Those options should be resilient to
......
......@@ -6,7 +6,7 @@
/*--- Functions implementation ----*/
void function::bootstrap(const data*, const arguments& args)
void function::bootstrap(const ptr<data>, const arguments& args)
{
// If the bootstrap option contains a filename, load it
if(args.is_defined("bootstrap") && !args["bootstrap"].empty())
......@@ -138,7 +138,7 @@ void function::save_call(std::ostream&, const arguments&) const
}
//! \brief L2 norm to data.
double function::L2_distance(const data* d) const
double function::L2_distance(const ptr<data> d) const
{
double l2_dist = 0.0;
for(int i=0; i<d->size(); ++i)
......@@ -163,7 +163,7 @@ double function::L2_distance(const data* d) const
}
//! \brief Linf norm to data.
double function::Linf_distance(const data* d) const
double function::Linf_distance(const ptr<data> d) const
{
vec mean = vec::Zero(dimY());
vec var = vec::Zero(dimY());
......@@ -305,7 +305,7 @@ void nonlinear_function::save_call(std::ostream& out, const arguments& args) con
function::save_call(out, args);
}
void nonlinear_function::bootstrap(const data* d, const arguments& args)
void nonlinear_function::bootstrap(const ptr<data> d, const arguments& args)
{
if(args.is_vec("bootstrap"))
{
......@@ -487,7 +487,7 @@ void compound_function::setParametrization(params::output new_param)
}
}
void compound_function::bootstrap(const ::data* d, const arguments& args)
void compound_function::bootstrap(const ::ptr<data> d, const arguments& args)
{
const bool global_bootstrap = args.is_defined("bootstrap");
......@@ -904,7 +904,7 @@ void product_function::save_call(std::ostream& out, const arguments& args) const
function::save_call(out, args);
}
void product_function::bootstrap(const data* d, const arguments& args)
void product_function::bootstrap(const ptr<data> d, const arguments& args)
{
const bool global_bootstrap = args.is_defined("bootstrap");
......
......@@ -40,7 +40,7 @@ class function : public parametrized
//! \details
//! Can be used to set the diffuse component of the function for
//! example. The default behaviour is to load a function file.
virtual void bootstrap(const data*, const arguments& args);
virtual void bootstrap(const ptr<data>, const arguments& args);
/* IMPORT/EXPORT FUNCTIONS */
......@@ -77,14 +77,14 @@ class function : public parametrized
//! If the measurement points are not uniformly distributed, then the
//! metric does not represent the real difference integrated over the
//! hemisphere.
double L2_distance(const data* d) const ;
double L2_distance(const ptr<data> d) const ;
//! \brief Linf norm to data.
//! \note This distance is only valid with respect to the data sampling.
//! If the measurement points are not uniformly distributed, then the
//! metric does not represent the real difference integrated over the
//! hemisphere.
double Linf_distance(const data* d) const ;
double Linf_distance(const ptr<data> d) const ;
};
......@@ -111,7 +111,7 @@ class nonlinear_function: public function
//! The nonllinear_function overload the function bootstrap call to
//! allows for loading the parameters vector directly from the command
//! line: --bootstrap [p1, p2, ..., pn]
virtual void bootstrap(const data*, const arguments& args);
virtual void bootstrap(const ptr<data>, const arguments& args);
//! Number of parameters to this non-linear function
virtual int nbParameters() const = 0;
......@@ -199,7 +199,7 @@ class compound_function: public nonlinear_function
//!
//! <u>Local/Global policy:</u></br />
//! Local bootstrap can not overload the global bootstrap.
virtual void bootstrap(const ::data* d, const arguments& args);
virtual void bootstrap(const ::ptr<data> d, const arguments& args);
//! Set the dimension of the input space of the function
virtual void setDimX(int nX);
......@@ -316,7 +316,7 @@ class product_function : public nonlinear_function
//! \brief Provide a first rough fit of the function.
virtual void bootstrap(const data* d, const arguments& args);
virtual void bootstrap(const ptr<data> d, const arguments& args);
// Set the dimension of the input/output space of the function
virtual void setDimX(int nX);
......
......@@ -271,7 +271,7 @@ function* plugins_manager::get_function(const arguments& args)
*/
return func;
}
data* plugins_manager::get_data(const std::string& n)
ptr<data> plugins_manager::get_data(const std::string& n)
{
if(n.empty())
{
......@@ -295,14 +295,14 @@ data* plugins_manager::get_data(const std::string& n)
return new vertical_segment() ;
}
}
fitter* plugins_manager::get_fitter(const std::string& n)
ptr<fitter> plugins_manager::get_fitter(const std::string& n)
{
if(n.empty())
{
#ifdef DEBUG
std::cout << "<<DEBUG>> no fitter plugin specified, returning null" << std::endl;
#endif
return NULL;
return ptr<NULL>;
}
FitterPrototype myFitter = open_library<FitterPrototype>(n, "provide_fitter");
......@@ -311,16 +311,16 @@ fitter* plugins_manager::get_fitter(const std::string& n)
#ifdef DEBUG
std::cout << "<<DEBUG>> using function provider in file \"" << n << "\"" << std::endl;
#endif
return myFitter();
return ptr<myFitter()>;
}
else
{
std::cerr << "<<ERROR>> no fitter provider found in file \"" << n << "\"" << std::endl;
return NULL ;
return ptr<NULL> ;
}
}
void plugins_manager::check_compatibility(data*& d, function*& f,
void plugins_manager::check_compatibility(ptr<data>& d, function*& f,
const arguments& args)
{
if(d->input_parametrization() == params::UNKNOWN_INPUT &&
......
......@@ -9,6 +9,7 @@
#include "fitter.h"
#include "args.h"
#include "clustering.h"
#include "ptr.h"
/*! \brief This class handles the loading of plugins and insure that they can
* talk to each others through coordinates transforms.
......@@ -28,18 +29,18 @@ class plugins_manager
//! \brief get an instance of the data that is defined in the plugin with
//! filename n. Return null if no one exist.
static data* get_data(const std::string& n) ;
static ptr<data> get_data(const std::string& n) ;
//! \brief get an instance of the fitter that is defined in the plugin with
//! filename n. Return null if no one exist.
static fitter* get_fitter(const std::string& n) ;
static ptr<fitter> get_fitter(const std::string& n) ;
//! \brief check if a data object and a function object are compatibles.
//! this has to be done before fitting to ensure that the
//! parametrizations spaces are the same.
//! \todo specify an output parametrization for the function ?
static void check_compatibility(data*& d, function*& f,
static void check_compatibility(ptr<data>& d, function*& f,
const arguments& args) ;
......@@ -54,10 +55,4 @@ class plugins_manager
typedef function* (*FunctionPrototype)();
typedef fitter* (*FitterPrototype)();
typedef data* (*DataPrototype)();
// Map associating a filename to a provider. This is used to store a list of
// accessible provider to make automatic type testing.
std::map<std::string, function*> _functions ;
std::map<std::string, data*> _datas ;
std::map<std::string, fitter*> _fitters ;
} ;
......@@ -59,6 +59,12 @@ template<class T> class ptr
_counter = new ptr_counter();
}
//! Counter copy constructor
ptr(T* ptr, ptr_counter* counter) : _ptr(ptr), _counter(counter)
{
_counter->increment();
}
//! Copy constructor
ptr(const ptr<T>& p) : _ptr(p._ptr), _counter(p._counter)
{
......@@ -90,14 +96,33 @@ template<class T> class ptr
return _ptr;
}
//! Is the underlying pointer NULL.
inline bool is_null() const
//! Is the underlying pointer not NULL.
inline operator bool() const
{
return _ptr == NULL;
return _ptr != NULL;
}
template<class U>
friend ptr<U> dynamic_pointer_cast(const ptr<T>& ptr_t)
{
U* u = dynamic_cast<U*>(ptr_t._ptr);
if(u == NULL)
{
return ptr<U>(NULL);
}
else
{
ptr<U> ptr_u = ptr<U>(u, ptr_t._counter);
return ptr_u;
}
}
private:
T* _ptr;
ptr_counter* _counter;
};
#endif
......@@ -78,8 +78,8 @@ int main(int argc, char** argv)
return 0 ;
}
fitter* fit = plugins_manager::get_fitter(args["fitter"]) ;
if(fit == NULL)
ptr<fitter> fit = plugins_manager::get_fitter(args["fitter"]) ;
if(!fit)
{
std::cerr << "<<ERROR>> unable to load the fitter plugin \"" << args["fitter"] << "\"" << std::endl;
return 1;
......@@ -100,13 +100,10 @@ int main(int argc, char** argv)
return 1 ;
}
// if(fitters.size() > 0 && datas.size() > 0 && functions.size() > 0)
if(fit != NULL)
{
fit->set_parameters(args) ;
function* f = plugins_manager::get_function(args);
data* d = plugins_manager::get_data(args["data"]);
ptr<data> d = plugins_manager::get_data(args["data"]);
d->load(args["input"], args);
if(f == NULL || d == NULL)
......@@ -160,11 +157,6 @@ int main(int argc, char** argv)
return 1;
}
}
else
{
std::cout << "<<ERROR>> no fitter loaded, please check your command line arguments" << std::endl ;
}
return 0 ;
}
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