aboutsummaryrefslogtreecommitdiffstats
path: root/models/dl_model.hpp
blob: 7221fd1effb31634e7447aecc06e83cc993d3c57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#ifdef __linux__

#ifndef DL_MODEL_HPP
#define DL_MODEL_HPP
#define OPT_HEADER
#include <core/fitter.hpp>
#include <cmath>
#include <iostream>
#include <string>
#include <sstream>
#include <dlfcn.h>

namespace opt_utilities
{
  template <typename T>
  class dl_model
    :public model<T,T,std::vector<T>,std::string>
  {
  private:
    T (*calc_model)(T x,const T* p);
    int nparams;
    mutable void* handle;
  private:
    model<T,T,std::vector<T> >* do_clone()const
    {
      dl_model<T>* result=new dl_model<T>(*this);
      this->handle=NULL;
      return result;
    }
    
    const char* do_get_type_name()const
    {
      return "shared object wrapping model";
    }

    //  public:
  public:
    dl_model()
      :handle(NULL)
    {}


  public:
    dl_model(const char* file_name)
      :handle(NULL)
    {
      
      handle=dlopen(file_name,RTLD_LAZY);
      
      if(!handle)
	{
	  throw opt_exception("faild loading object");
	}
      
      calc_model=(T (*)(T,const T*))dlsym(handle,"calc_model");
      
      if(!calc_model)
	{
	  throw opt_exception("symble undefined");
	}

      const char* (*get_param_name)(int)
	=(const char* (*)(int))dlsym(handle,"get_param_name");
      
      if(!get_param_name)
	{
	  throw opt_exception("symble undefined");
	}

      int (*get_num_params)()
	=(int (*)())dlsym(handle,"get_num_params");
      
      if(!get_num_params)
	{
	  throw opt_exception("symble undefined");
      if(!get_num_params)
	{
	  throw opt_exception("symble undefined");
	}	}

      T (*get_default_value)(int)
	=(T (*)(int))dlsym(handle,"get_default_value");
      
      if(!get_default_value)
	{
	  throw opt_exception("symble undefined");
	}

      nparams=get_num_params();
      
      for(int i=0;i!=nparams;++i)
	{
	  this->push_param_info(param_info<std::vector<T> >(get_param_name(i),
				get_default_value(i)));
	}
    }

    ~dl_model()
    {
      if(handle)
	{
	  dlclose(handle);
	}
    }

    void bind(const char* file_name)
    {
      if(handle)
	{
	  dlclose(handle);
	}
      this->clear_param_info();
      handle=dlopen(file_name,RTLD_LAZY);

      if(!handle)
	{
	  throw opt_exception("faild loading object");
	}

      calc_model=(T (*)(T,const T*))dlsym(handle,"calc_model");
      
      if(!calc_model)
	{
	  throw opt_exception("symble undefined");
	}

      const char* (*get_param_name)(int)
	=(const char* (*)(int))dlsym(handle,"get_param_name");
      
      if(!get_param_name)
	{
	  throw opt_exception("symble undefined");
	}


      int (*get_num_params)()
	=(int (*)())dlsym(handle,"get_num_params");
      
      if(!get_num_params)
	{
	  throw opt_exception("symble undefined");
	}      


      T (*get_default_value)(int)
	=(T (*)(int))dlsym(handle,"get_default_value");
     

      if(!get_default_value)
	{
	  throw opt_exception("symble undefined");
	}

      
      nparams=get_num_params();
      for(int i=0;i!=nparams;++i)
	{
	  this->push_param_info(param_info<std::vector<T> >(get_param_name(i),
				get_default_value(i)));
	}
    }

    T do_eval(const T& x,const std::vector<T>& param)
    {
      if(handle==NULL)
	{
	  throw opt_exception("dl object unloaded");
	}
      return calc_model(x,&get_element(param,0));
    }

    std::string do_to_string()const
    {
      return "Dynamical load model\n"
	"Should be loaded from an shared object file\n";
    }
  };
}



#endif
#endif
//EOF