From d61ae5af2881b3a80d9a76691104fe31b47b384e Mon Sep 17 00:00:00 2001 From: Zimnx Date: Fri, 8 May 2015 03:17:34 +0200 Subject: [PATCH 1/2] Added variadic functions adding setting values and names of parameters --- hist/hist/inc/TFormula.h | 4 +++ hist/hist/src/TFormula.cxx | 65 +++++++++++++++++++++++++------------- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/hist/hist/inc/TFormula.h b/hist/hist/inc/TFormula.h index 5c1fcc27217fd..16a858e3d7e40 100644 --- a/hist/hist/inc/TFormula.h +++ b/hist/hist/inc/TFormula.h @@ -190,10 +190,14 @@ class TFormula : public TNamed void SetParameter(Int_t param, Double_t value); void SetParameters(const Double_t *params); //void SetParameters(const pair *params, const Int_t size); + template + void SetMultipleParameters(Args... args); void SetParameters(Double_t p0,Double_t p1,Double_t p2=0,Double_t p3=0,Double_t p4=0, Double_t p5=0,Double_t p6=0,Double_t p7=0,Double_t p8=0, Double_t p9=0,Double_t p10=0); // *MENU* void SetParName(Int_t ipar, const char *name); + template + void SetMultipleParNames(Args... args); void SetParNames(const char *name0="p0",const char *name1="p1",const char *name2="p2",const char *name3="p3",const char *name4="p4", const char *name5="p5",const char *name6="p6",const char *name7="p7",const char diff --git a/hist/hist/src/TFormula.cxx b/hist/hist/src/TFormula.cxx index 9912654b7fbfb..ba08911fd442b 100644 --- a/hist/hist/src/TFormula.cxx +++ b/hist/hist/src/TFormula.cxx @@ -136,6 +136,22 @@ End_Html //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* +namespace detail +{ +template +struct all_true; + +template<> +struct all_true<> : std::true_type{}; + +template +struct all_true : std::false_type{}; + +template +struct all_true : all_true{}; +} + + // prefix used for function name passed to Cling static const TString gNamePrefix = "T__"; // function index number used to append in cling name to avoid a clash @@ -2170,6 +2186,18 @@ void TFormula::SetParameters(const Double_t *params) // apart if the users has defined explicitly the parameter names DoSetParameters(params,fNpar); } + +template +void TFormula::SetMultipleParameters(Args... args) +{ + static_assert(detail::all_true::value...>::value, "All parameters should be of type Double_t"); + std::vector params = { args... }; + for(int i = 0; i < (int)params.size(); ++i) + { + if(fNpar >= i+1) SetParameter(i, params[i]); + } +} + void TFormula::SetParameters(Double_t p0,Double_t p1,Double_t p2,Double_t p3,Double_t p4, Double_t p5,Double_t p6,Double_t p7,Double_t p8, Double_t p9,Double_t p10) @@ -2177,17 +2205,7 @@ void TFormula::SetParameters(Double_t p0,Double_t p1,Double_t p2,Double_t p3,Dou // Set a list of parameters. // The order is by default the aphabetic order given to the parameters // apart if the users has defined explicitly the parameter names - if(fNpar >= 1) SetParameter(0,p0); - if(fNpar >= 2) SetParameter(1,p1); - if(fNpar >= 3) SetParameter(2,p2); - if(fNpar >= 4) SetParameter(3,p3); - if(fNpar >= 5) SetParameter(4,p4); - if(fNpar >= 6) SetParameter(5,p5); - if(fNpar >= 7) SetParameter(6,p6); - if(fNpar >= 8) SetParameter(7,p7); - if(fNpar >= 9) SetParameter(8,p8); - if(fNpar >= 10) SetParameter(9,p9); - if(fNpar >= 11) SetParameter(10,p10); + SetMultipleParameters(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); } void TFormula::SetParameter(Int_t param, Double_t value) { @@ -2200,21 +2218,24 @@ void TFormula::SetParameter(Int_t param, Double_t value) // TString name = TString::Format("%d",param); // SetParameter(name,value); } + +template +void TFormula::SetMultipleParNames(Args... args) +{ + static_assert(detail::all_true::value...>::value, + "All parameters should be of type const char*"); + std::vector params = { args... }; + for(int i = 0; i < (int)params.size(); ++i) + { + if(fNpar >= i+1) SetParName(i, params[i]); + } +} + void TFormula::SetParNames(const char *name0,const char *name1,const char *name2,const char *name3, const char *name4, const char *name5,const char *name6,const char *name7, const char *name8,const char *name9,const char *name10) { - if(fNpar >= 1) SetParName(0,name0); - if(fNpar >= 2) SetParName(1,name1); - if(fNpar >= 3) SetParName(2,name2); - if(fNpar >= 4) SetParName(3,name3); - if(fNpar >= 5) SetParName(4,name4); - if(fNpar >= 6) SetParName(5,name5); - if(fNpar >= 7) SetParName(6,name6); - if(fNpar >= 8) SetParName(7,name7); - if(fNpar >= 9) SetParName(8,name8); - if(fNpar >= 10) SetParName(9,name9); - if(fNpar >= 11) SetParName(10,name10); + SetMultipleParNames(name0, name1, name2, name3, name4, name5, name6, name7, name8, name9, name10); } void TFormula::SetParName(Int_t ipar, const char * name) { From ce40f593c44b28fca397e1a7fd62c183011c6e4d Mon Sep 17 00:00:00 2001 From: Zimnx Date: Fri, 8 May 2015 04:26:03 +0200 Subject: [PATCH 2/2] Added rvalue parameters and forwarding to TFormula SetMultiple* --- hist/hist/inc/TFormula.h | 4 ++-- hist/hist/src/TFormula.cxx | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/hist/hist/inc/TFormula.h b/hist/hist/inc/TFormula.h index 16a858e3d7e40..b2dd47bcdea2e 100644 --- a/hist/hist/inc/TFormula.h +++ b/hist/hist/inc/TFormula.h @@ -191,13 +191,13 @@ class TFormula : public TNamed void SetParameters(const Double_t *params); //void SetParameters(const pair *params, const Int_t size); template - void SetMultipleParameters(Args... args); + void SetMultipleParameters(Args&&... args); void SetParameters(Double_t p0,Double_t p1,Double_t p2=0,Double_t p3=0,Double_t p4=0, Double_t p5=0,Double_t p6=0,Double_t p7=0,Double_t p8=0, Double_t p9=0,Double_t p10=0); // *MENU* void SetParName(Int_t ipar, const char *name); template - void SetMultipleParNames(Args... args); + void SetMultipleParNames(Args&&... args); void SetParNames(const char *name0="p0",const char *name1="p1",const char *name2="p2",const char *name3="p3",const char *name4="p4", const char *name5="p5",const char *name6="p6",const char *name7="p7",const char diff --git a/hist/hist/src/TFormula.cxx b/hist/hist/src/TFormula.cxx index ba08911fd442b..7c8f3a38db28d 100644 --- a/hist/hist/src/TFormula.cxx +++ b/hist/hist/src/TFormula.cxx @@ -2188,10 +2188,11 @@ void TFormula::SetParameters(const Double_t *params) } template -void TFormula::SetMultipleParameters(Args... args) +void TFormula::SetMultipleParameters(Args&&... args) { - static_assert(detail::all_true::value...>::value, "All parameters should be of type Double_t"); - std::vector params = { args... }; + static_assert(detail::all_true::type,Double_t>::value...>::value, + "All parameters should be of type Double_t"); + std::vector params = { std::forward(args)... }; for(int i = 0; i < (int)params.size(); ++i) { if(fNpar >= i+1) SetParameter(i, params[i]); @@ -2220,11 +2221,11 @@ void TFormula::SetParameter(Int_t param, Double_t value) } template -void TFormula::SetMultipleParNames(Args... args) +void TFormula::SetMultipleParNames(Args&&... args) { - static_assert(detail::all_true::value...>::value, + static_assert(detail::all_true::type,const char*>::value...>::value, "All parameters should be of type const char*"); - std::vector params = { args... }; + std::vector params = { std::forward(args)... }; for(int i = 0; i < (int)params.size(); ++i) { if(fNpar >= i+1) SetParName(i, params[i]);