ODIN
seqdriver.h
1 /***************************************************************************
2  seqdriver.h - description
3  -------------------
4  begin : Sat Apr 3 2004
5  copyright : (C) 2000-2021 by Thies H. Jochimsen
6  email : thies@jochimsen.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef SEQDRIVER_H
19 #define SEQDRIVER_H
20 
21 
22 #include <odinseq/seqclass.h>
23 #include <odinseq/seqplatform.h>
24 
25 class SeqDriverBase : public virtual SeqClass {
26 
27  public:
28  SeqDriverBase() {}
29  virtual ~SeqDriverBase() {}
30  virtual odinPlatform get_driverplatform() const = 0;
31 };
32 
33 
35 
36 
41 template<class D>
42 class SeqDriverInterface : public SeqClass {
43  public:
44  typedef D* (*create_driver_callback)();
45 
46  SeqDriverInterface(const STD_string& driverlabel="unnamedSeqDriverInterface")
47  : current_driver(0) {
48  set_label(driverlabel);
49  }
50 
51  virtual ~SeqDriverInterface(){
52  if(current_driver) delete current_driver;
53  }
54 
55  SeqDriverInterface& operator = (const SeqDriverInterface& di) {
56  SeqClass::operator = (di);
57  if(current_driver) delete current_driver;
58  current_driver=0;
59  if(di.current_driver) current_driver=di.current_driver->clone_driver();
60  return *this;
61  }
62 
63  D& operator * () {return *get_driver();}
64  D* operator -> () {return get_driver();}
65 
66 
67  private:
68 
69  // overwriting virtual functions from SeqClass
70  bool prep() {return bool(get_driver());} // make sure correct driver is loaded
71 
72  D* get_driver() {
73  odinPlatform current_pf=SeqPlatformProxy::get_current_platform();
74  if(!current_driver) allocate_driver();
75  else {
76  odinPlatform driver_platform=current_driver->get_driverplatform();
77  if(driver_platform!=current_pf) {
78  delete current_driver;
79  allocate_driver();
80  }
81  }
82  if(!current_driver) {
83  STD_cerr << "ERROR: " << get_label() << ": Driver missing for platform " << SeqPlatformProxy::get_platform_str(current_pf) << STD_endl;
84  }
85  if(current_driver->get_driverplatform()!=current_pf) {
86  STD_string drvplat(SeqPlatformProxy::get_possible_platforms()[current_driver->get_driverplatform()]);
87  STD_cerr << "ERROR: " << get_label() << ": Driver has wrong platform signature " << drvplat << ", but expected " << SeqPlatformProxy::get_platform_str(current_pf) << STD_endl;
88  }
89  return current_driver;
90  }
91 
92  void allocate_driver() {
93  current_driver=pfinterface->create_driver(current_driver);
94  if(current_driver) current_driver->set_label(get_label());
95  }
96 
97 
98  SeqPlatformProxy pfinterface;
99  D* current_driver;
100 };
101 
102 #endif
Labeled & set_label(const STD_string &label)
Definition: tjlabel.h:43
const STD_string & get_label() const
Definition: tjlabel.h:48
odinPlatform
Definition: system.h:39