ROOT_Application  2.0
C++ Core modules and GUIStock
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ListDataStock.h
Go to the documentation of this file.
1 
10 // playing with swap and move semantic
11 // http://stackoverflow.com/questions/6380862/how-to-provide-a-swap-function-for-my-class?lq=1
12 // if use noexcept, swap with move ? only for swap( A, B) make sense..
13 // http://stackoverflow.com/questions/6416385/move-semantics-custom-swap-function-obsolete
14 
15 #ifndef LISTDATASTOCK_H_
16 #define LISTDATASTOCK_H_
17 
18 // pass on asus ?? but not on home ??
19 #ifndef __CINT__
20 #include <initializer_list>
21 #endif
22 
23 #include "DataStock.h"
24 #include "../utils/TimeScale.h"
25 #include "../utils/FullName.h"
26 #include "../utils/Indicator.h"
27 #include "../utils/Utils.h"
28 
29 // test for having polymorphic template PutCArray/GetCArray
30 #ifndef __CINT__
31 #include <boost/any.hpp>
32 #include <boost/variant.hpp>
33 #include <typeinfo>
34 #endif
35 
36 // includes PolicyChronologic
37 #include "typedef.h"
38 
51 
52  public:
57 
58  // check if timescale necessary
59  // should be replaced with longname. TimeScale not needed any more
60  // maybe very convenient this two constructor, one internal, the other not
61  // problem here need a TimeScale to make difference with the other,
62  // problem ?? not really, more clear internally to have TimeScale
63  // but need to setup the fullname as well
67  ListDataStockBase(std::string iname, ETime itmscl,
69  name(iname),tmscl(itmscl),dates(idates) {}
71  virtual ~ListDataStockBase() {}
73 
76  // really need virtual here ? no only apply to base...
77  // GetName cannot be called with an default object ??
78  std::string GetName() const {return name;}
79  virtual std::string GetFullName() const {return fullname;}
80  virtual ETime GetTimeScale() const {return tmscl;}
81  virtual void SetTimeScale( const ETime new_tmscl ) { tmscl = new_tmscl; }
82  void SetName(const std::string iname) {name=iname;}
83  virtual void SetFullName( std::string fname ) {fullname=fname;}
84 
85  // confusing here both dates / time
87  virtual Utils::firstlast_dates GetDates() const {return dates;}
89  time_t GetFirstDate() const {return dates.first;}
91  time_t GetLastDate() const {return dates.last;}
93  virtual void SetDates( const time_t first, const time_t last ) {
94  dates.first = first; dates.last = last; }
96  virtual void SetDates( const Utils::firstlast_dates & new_date) { dates = new_date; }
99  virtual void SetDates() =0;
101 
104  // change nothing pure virtual or not
105  // Wrappers around the internal std::vector
108  virtual unsigned int GetSize() const =0;
109  //virtual unsigned int GetSize() const {return 0;}
111  virtual unsigned int Capacity() const =0;
116  virtual void Reserve(const unsigned int new_size) =0;
123  virtual void Clear() =0;
125  virtual void Swap() =0;
127 
128  // need argument, otherwise same declaration, as for AddData
129  // std::vector<DataStock> &, cannot work..., or only in Derived ? really used from base ?
130  // cannot set protected in base class ?
133  virtual void GetVectorData( std::vector<DataCSV>&, const EPChrono ) const {}
134  virtual void GetVectorData( std::vector<SimpleData>&, const EPChrono ) const {}
135  virtual void GetVectorData( std::vector<MultiData>&, const EPChrono ) const {}
137 
138  // should be able to call the policy class here, but call is ambiguous
142  virtual std::string policy() const =0;
144  virtual EPChrono bpolicy() const =0;
145 
158  virtual void Order( const EPChrono chrono ) = 0; //{};
160  virtual time_t LastUpdate() const =0;
162  virtual time_t GetFirstTime() const =0;
164 
168  virtual DataStock& operator[] (const unsigned int offset) =0;
184  virtual const DataStock& ChronologicAt(const unsigned int offset) const =0;
196  virtual const DataStock& NoChronologicAt(const unsigned int offset) const =0;
198 
200 
214  virtual void AddData( DataStock & ) =0;
215 
230  // internally call Policy::AddChronoToList ( with param first_to_skip )
231  virtual int AddToList ( const ListDataStockBase * ) =0;
232 
248  // could be protected ? at this point used only by AddToList ??
249  virtual void Replace ( const ListDataStockBase * ) =0;
251 
252 
255 #ifndef __CINT__
256  typedef boost::variant<float**, double**, int**> var_types;
257  typedef boost::variant<float***, double***, int***> var_types2;
258 #endif
259 
260 
262  // to use double for more generality ?? extended by template
264 
306  // better version, can use EDS::ECSV
307 #ifndef __CINT__
308  virtual unsigned int GetCArray( var_types arraydata_c, const EPChrono b_order, const unsigned int no = 0 ) const =0;
309 #endif
310 
336  // could return the number of indic allocated
337  // float** array_c const compiles
338 #ifndef __CINT__
339  virtual unsigned int GetCArray( var_types2 arraydata_c, const EPChrono b_order, std::initializer_list<unsigned int> list_no ) const =0;
340 #endif
341 
377 #ifndef __CINT__
378  virtual int PutCArray ( const ListDataStockBase & ldata_date, const unsigned int nb_data,
379  const unsigned size_indic, var_types arraydata_c, const unsigned int offset_begin,
380  const unsigned int offset_end = 0, const EPChrono b_chrono = EPChrono::CHRONO, bool full_array = false ) =0;
381 #endif
382 
383 
392  virtual void PrintData ( const unsigned int verbose = 0) const =0;
393 
394  protected:
396  std::string name;
397  //5/2/11
398  // Need timescale for drawing label, cannot be extracted from name of listdata or THStock
399  // present in canvas stock but will be dependent of CanvasStock, cannot be draw alone...! not really used ??
400  // jan 2013 to check if needed, in fullname is the timescale !
401  // can be deleted, if fullname is always setup !
402  // necessary for Transform_TimeScale ? not really done from Stock !
408  // selecting a range..., not really because List is created in ReadCSV and use range as parameter, makes sense
409  // keep for now, used a lot in unit-test, but if do not find really a use, to delete
410  //
411  // Case useful, if only part of the data is in memory (by some range), then call to any update will be wrong !
412  // In fact larger problem in this case, update by server, saving to file... cannot use the in-memory List
413  //
414  // => So check only with header ? but wrong also if INST has been updated normally.
415  // So dates as data member could maintain a correct value:
416  // - set by default to Header not to Last data.(in case of range)
417  // - updated only after a real update. AddToList for instance
418  // - when asked for LastCSVUpdate, return dm value
419  // - When asking to save data in file, same, check with this value. Cannot write the file if cannot
420  // - When data received from ServerSM, same, do not apply the update.
421  // Too complex case..., not sure to solve, but make some checks/warning before update (in LastCSVUpdate)
424  // put last to use initializer constructors
425  std::string fullname;
426 
428  // why cannot move GetVecotrData here ?
429 
436 #ifndef __CINT__
438  : public boost::static_visitor<type_value_data>
439  {
440  public:
441  // need default constructor
442  visitor_type_array(): i(0),j(0) {}
443  visitor_type_array(int i1,int j1): i(i1),j(j1) {}
444 
445  template <typename T>
446  type_value_data operator() (T **val) const {
447  //std::cout << t << std::endl;
448  return val[i][j];
449  }
450  unsigned int i, j;
451  };
452 
453 
455  : public boost::static_visitor<>
456  {
457  public:
458  // need default constructor
460  visitor_get_array_allocate(int new_size): size(new_size) {}
461 
462  template <typename T>
463  void operator() (T **array) const {
464  //std::cout << "Allocate with size: " << size << std::endl;
465  //std::cout << " size of T: " << sizeof(T) << std::endl;
466  *array = new T[size];
467  }
468 
469  template <typename T>
470  void operator() (T ***array) const {
471  //std::cout << "Allocate T*** with size: " << size << std::endl;
472  //std::cout << " size of T: " << sizeof(T) << std::endl;
473  *array = new T*[size];
474  }
475  unsigned int size;
476  };
477 
479  : public boost::static_visitor<>
480  {
481  public:
482  // need default constructor
484  visitor_get_array_set_value(unsigned int nbi, double valuei): nb(nbi), value(valuei) {}
485 
486  template <typename T>
487  void operator() (T **array) const {
488  //std::cout << "nb " << nb << std::endl;
489  //std::cout << "value " << value << std::endl;
490  (*array)[nb] = value;
491  }
492  unsigned int nb;
493  double value;
494  };
495 #endif
496 
497 
498  private:
499  // rootcint doesnot understand delete
501  ListDataStockBase ( const ListDataStockBase & src ); // {}; //=delete;
503  ListDataStockBase& operator=( const ListDataStockBase & rhs); // {} //=delete;
504 };
506 
524 template
525 <
526  class T,
527  template <class> class PolicyChronologic //= VecNoChronologic by default, put in a forward declaration
528 >
530  public PolicyChronologic<T>
531 {
532 
533 public :
534 
537  /* Default constructor, timescale set to ETime::not_a_time, dates to 0 and empty name */
539  /* Set default dates (0) if not provided */
540  ListDataStock (std::string iname, ETime itmscl,
542  ListDataStockBase(iname,itmscl,idates) {}
544  ListDataStock (std::string iname, ETime itmscl, unsigned int size):ListDataStockBase(iname,itmscl) {ListData.reserve(size);}
546  // SetDates to include ? not clear if all default DataStock */
547  ListDataStock (std::string iname, ETime itmscl, const std::vector<T> & vec_data):
548  ListDataStockBase(iname,itmscl) {
549  ListData = vec_data;
550  //SetDates once the vector associated (?)
551  //SetDates();
552  }
556 
557 
560  unsigned int GetSize() const { return ListData.size(); }
561  unsigned int Capacity() const { return ListData.capacity(); }
562  void Reserve(const unsigned int new_size) { ListData.reserve(new_size); }
563  void Clear();
564  void Swap () { std::vector<T>(ListData).swap(ListData); }
566 
569  // maybe function non const to set data
570  const T & ChronologicAt (const unsigned int offset) const {
572  }
573  const T & NoChronologicAt(const unsigned int offset) const {
575  }
576  // see book, 2 version one const because copy operator
577  // usually reference is preferred in examples and STL
578  T& operator[] (const unsigned int offset) {
579  if ( offset > ListData.size() )
580  return ListData[0];
581  else
582  return ListData[offset];
583  }
585 
588  void AddData ( DataStock & data ) {
589  // call the correct derived constructor
590  ListData.push_back( static_cast<T &>(data) ); }
591  int AddToList ( const ListDataStockBase * new_list );
592  void Replace ( const ListDataStockBase * ldata_in );
594 
603  void PrintData ( const unsigned int verbose = 0) const;
604 
607  unsigned int GetCArray( var_types arraydata_c, const EPChrono b_order, const unsigned int no = 0 ) const ;
608  unsigned int GetCArray( var_types2 arraydata_c, const EPChrono b_order, std::initializer_list<unsigned int> list_no ) const ;
609 
610  int PutCArray ( const ListDataStockBase & ldata_date,
611  const unsigned int nb_data,
612  const unsigned size_indic,
613  var_types arraydata_c,
614  const unsigned int offset_begin,
615  const unsigned int offset_end = 0,
616  const EPChrono b_chrono = EPChrono::CHRONO,
617  bool full_array = false );
619 
622  // not sure if must be public or protected
623  std::string policy() const { return PolicyChronologic<T>::policy(); }
625  // Return the date of the last (chronologic) data
627  // Return the date of the first (chronologic) data
629  void Order( const EPChrono b_chronologic = EPChrono::CHRONO ) {
630  PolicyChronologic<T>::order_impl( ListData, b_chronologic );
631  }
632  // should check it exists ! valid != 0 ?
633  void SetDates() {
634  if ( GetSize() > 0 ) {
635  dates.first = ChronologicAt(0).GetDate();
636  dates.last = NoChronologicAt(0).GetDate();
637  } //else ?
638  }
639  // Set direcltly the dates, has been overwritten from Base by SetDates()
640  void SetDates( time_t first, time_t last ) {
641  ListDataStockBase::SetDates( first, last );
642  }
644 
645  protected :
646  // Get a copy of the internal vector, in the order given by chrono
647  void GetVectorData(std::vector<T> &res, const EPChrono chrono) const {
648  res = ListData;
649  // if chronology are different, reverse the vector, without modifying this.ListData
650  if ( bpolicy() != chrono )
651  std::reverse( res.begin(), res.end() );
652  }
653 
654  private :
656  std::vector<T> ListData;
658  // rootcint does not try to read derived for the moment, can keep the delete
659  ListDataStock ( const ListDataStock & src ) =delete;
661  ListDataStock & operator=( const ListDataStockBase & rhs) =delete;
662 };
663 
666 namespace ListDataStock_Utils {
667 
694 template <
695  template <class> class Policy1,
696  template <class> class Policy2,
697  template <class> class Policy3,
698  template <class> class Policy4
699 >
700 int TransformTimeScale( const ETime new_tmscl, const ListDataStock<DataCSV,Policy1> & list_csv_in,
701  const ListDataStock<SimpleData,Policy2> & list_vol_in,
702  ListDataStock<DataCSV,Policy3> & list_csv_out, ListDataStock<SimpleData,Policy4> & list_vol_out );
703 
723 template <
724  template <class> class Policy1,
725  template <class> class Policy2,
726  template <class> class Policy3,
727  template <class> class Policy4
728 >
729 int TransformTimeScale( const ETime new_tmscl, const ListDataStock<SimpleData,Policy1> & list_csv_in,
730  const ListDataStock<SimpleData,Policy2> & list_vol_in,
731  ListDataStock<DataCSV,Policy3> & list_csv_out, ListDataStock<SimpleData,Policy4> & list_vol_out );
732 }
734 
735 //simple split, stay in header anyway
736 #ifndef __MAKECINT__
737 #include "ListDataStock_timpl.h"
738 #endif
739 
740 #endif // LISTDATASTOCK_H_
virtual DataStock & operator[](const unsigned int offset)=0
Access nth element, do not check for the policy chronology.
std::string fullname
fullname seems necessary (convenient) for GUI, never set by constructors
virtual void AddData(DataStock &)=0
Add a copy of the DataStock into the ListDataStock.
virtual void SetDates()=0
Set the correct first/last dates from the DataStock in the vector.
EPChrono bpolicy() const
Return policy as an enumeration type EPChrono, CHRONO / NO_CHRONO.
void Replace(const ListDataStockBase *ldata_in)
Replace all DataStock's of the list with ldata_in.data, without checking for update.
ListDataStock(std::string iname, ETime itmscl, Utils::firstlast_dates idates=Utils::get_fldate_default())
void SetName(const std::string iname)
Definition: ListDataStock.h:82
virtual std::string policy() const =0
Return a string of the chronology policy.
void AddData(DataStock &data)
Add a copy of the DataStock into the ListDataStock.
ListDataStock(std::string iname, ETime itmscl, unsigned int size)
Reserve size for the vector, default DataStock constructor is called (dates and values are set to zer...
void PrintData(const unsigned int verbose=0) const
Nice formatting output on console.
virtual int PutCArray(const ListDataStockBase &ldata_date, const unsigned int nb_data, const unsigned size_indic, var_types arraydata_c, const unsigned int offset_begin, const unsigned int offset_end=0, const EPChrono b_chrono=EPChrono::CHRONO, bool full_array=false)=0
Fill a ListDataStock<T> from a 2 dimensional C-array.
virtual ~ListDataStockBase()
Destructor.
Definition: ListDataStock.h:71
T & operator[](const unsigned int offset)
Access nth element, do not check for the policy chronology.
unsigned int GetSize() const
Return the size of the vector.
virtual time_t LastUpdate() const =0
Get the date of the last data (always chronological meaning)
virtual void PrintData(const unsigned int verbose=0) const =0
Nice formatting output on console.
virtual int AddToList(const ListDataStockBase *)=0
Append all data from a new list ( coming from an Update or ReadCSV normally ) to an existing list...
double type_value_data
Type of the floating values to be stored, can be set to float or double before compilation.
Definition: DataStock.h:49
boost::variant< float **, double **, int ** > var_types
time_t GetFirstDate() const
Get first date from the private data member dates.
Definition: ListDataStock.h:89
Implementation of the functions.
void GetVectorData(std::vector< T > &res, const EPChrono chrono) const
const T & NoChronologicAt(const unsigned int offset) const
Get nth element in a non chronological order.
Use a policy PolicyChronologic, default VecNoChronologic.
virtual unsigned int Capacity() const =0
Return the capacity of the vector.
EPChrono
Define convenient name for the chronology, makes algorithms clearer.
virtual void GetVectorData(std::vector< DataCSV > &, const EPChrono) const
time_t GetLastDate() const
Get last date from the private data member dates.
Definition: ListDataStock.h:91
ListDataStockBase(std::string iname, ETime itmscl, Utils::firstlast_dates idates=Utils::get_fldate_default())
Constructor sets name, timescale and optionaly dates.
Definition: ListDataStock.h:67
Define typedef with a default chronology policy for the most common types of ListDataStock.
Classes for data to be stored in ListDataStock.
virtual time_t GetFirstTime() const =0
Get the date of the first data (always chronological meaning)
type_value_data operator()(T **val) const
unsigned int Capacity() const
Return the capacity of the vector.
virtual void Swap()=0
Reduce the capacity of the internal vector to fit its size.
void Reserve(const unsigned int new_size)
Reserve new_size elements.
virtual void Order(const EPChrono chrono)=0
Function to reverse the internal vector in its original policy.
virtual void SetDates(const Utils::firstlast_dates &new_date)
Set dates with a structure Utils::firstlast_dates.
Definition: ListDataStock.h:96
virtual void Replace(const ListDataStockBase *)=0
Replace all DataStock's of the list with ldata_in.data, without checking for update.
boost::variant< float ***, double ***, int *** > var_types2
ListDataStockBase()
Default constructor, set empty names, timescale ETime::not_a_time, dates are zero.
Definition: ListDataStock.h:56
ETime
Enumeration for the different time representation, from instantaneous (INST) to year(YEAR) ...
Definition: TimeScale.h:48
std::string policy() const
Return a string of the chronology policy.
void Order(const EPChrono b_chronologic=EPChrono::CHRONO)
Function to reverse the internal vector in its original policy.
virtual Utils::firstlast_dates GetDates() const
Return the structure Utils::firstlast_dates of the private data member, not from the data...
Definition: ListDataStock.h:87
virtual const DataStock & ChronologicAt(const unsigned int offset) const =0
Get the nth element in a chronological order.
virtual void SetTimeScale(const ETime new_tmscl)
Definition: ListDataStock.h:81
ETime tmscl
TimeScale of the List.
void Swap()
Reduce the capacity of the internal vector to fit its size.
void SetDates()
Set the correct first/last dates from the DataStock in the vector.
ListDataStock & operator=(const ListDataStockBase &rhs)=delete
Deleted assignment operator.
virtual void Reserve(const unsigned int new_size)=0
Reserve new_size elements.
time_t GetFirstTime() const
Get the date of the first data (always chronological meaning)
virtual unsigned int GetSize() const =0
Return the size of the vector.
virtual void SetDates(const time_t first, const time_t last)
Set dates, 2 arguments.
Definition: ListDataStock.h:93
void Clear()
Clear the vector and reinitialize data members to default values.
std::string GetName() const
Definition: ListDataStock.h:78
std::string name
name stores the indicator label with the parameters, it corresponds to its ShortName ...
ListDataStock(std::string iname, ETime itmscl, const std::vector< T > &vec_data)
Constructor accepting a std::vector<DataStock>
virtual std::string GetFullName() const
Definition: ListDataStock.h:79
ListDataStockBase & operator=(const ListDataStockBase &rhs)
Delete assignment operator.
Abstract base class for the ListDataStock, for storing base pointers in vectors.
Definition: ListDataStock.h:50
void SetDates(time_t first, time_t last)
Set dates, 2 arguments.
virtual void SetFullName(std::string fname)
Definition: ListDataStock.h:83
virtual const DataStock & NoChronologicAt(const unsigned int offset) const =0
Get nth element in a non chronological order.
~ListDataStock()
Destructor.
static SUPPRESS_NOT_USED_WARN firstlast_dates get_fldate_default()
need to be static for using default argument in a function
Definition: Utils.h:60
virtual EPChrono bpolicy() const =0
Return policy as an enumeration type EPChrono, CHRONO / NO_CHRONO.
Utils::firstlast_dates dates
Store the first and last dates of the list Redondant with dates from DataStock, added first for readi...
visitor_get_array_set_value(unsigned int nbi, double valuei)
virtual ETime GetTimeScale() const
Definition: ListDataStock.h:80
std::vector< T > ListData
internal vector of objects DataStock
general structure for dates in csv files
Definition: Utils.h:54
int AddToList(const ListDataStockBase *new_list)
Append all data from a new list ( coming from an Update or ReadCSV normally ) to an existing list...
virtual void GetVectorData(std::vector< MultiData > &, const EPChrono) const
time_t LastUpdate() const
Get the date of the last data (always chronological meaning)
virtual unsigned int GetCArray(var_types arraydata_c, const EPChrono b_order, const unsigned int no=0) const =0
Extract the data into a one dimensional C-array.
unsigned int GetCArray(var_types arraydata_c, const EPChrono b_order, const unsigned int no=0) const
Extract the data into a one dimensional C-array.
Abstract base class for all Data to be stored in ListDataStock.
Definition: DataStock.h:90
virtual void Clear()=0
Clear the vector and reinitialize data members to default values.
const T & ChronologicAt(const unsigned int offset) const
Get the nth element in a chronological order.
int PutCArray(const ListDataStockBase &ldata_date, const unsigned int nb_data, const unsigned size_indic, var_types arraydata_c, const unsigned int offset_begin, const unsigned int offset_end=0, const EPChrono b_chrono=EPChrono::CHRONO, bool full_array=false)
Fill a ListDataStock<T> from a 2 dimensional C-array.
virtual void GetVectorData(std::vector< SimpleData > &, const EPChrono) const
int TransformTimeScale(const ETime new_tmscl, const ListDataStock< DataCSV, Policy1 > &list_csv_in, const ListDataStock< SimpleData, Policy2 > &list_vol_in, ListDataStock< DataCSV, Policy3 > &list_csv_out, ListDataStock< SimpleData, Policy4 > &list_vol_out)
Transform StockCSV and Volume from M5 into a StockCSV with larger TimeScale, typical DAY to WEEK...