Cast.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2011 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/More/MEAL/MEAL/Cast.h
10
11#ifndef __MEAL_Cast_H
12#define __MEAL_Cast_H
13
14namespace MEAL {
15
17
18 template<class To, class From>
19 class Cast : public To
20 {
21
22 public:
23
24 typedef typename To::Result Result;
25
27 Cast (From* _from)
28 {
29 from = _from;
30 this->copy_parameter_policy (from);
31 }
32
33 std::string get_name () const
34 { return std::string("Cast")
35 + "<To=" + To::Name + ",From=" + From::Name + ">"; }
36
37 protected:
38
40 void calculate (Result& result, std::vector<Result>* gradient)
41 {
42 std::vector<typename From::Result> from_grad;
43 std::vector<typename From::Result>* from_gradptr = &from_grad;
44 if (!gradient)
45 from_gradptr = 0;
46
47 typename From::Result from_result = from->evaluate (from_gradptr);
48
49 result = static_cast<Result> (from_result);
50
51 if (!gradient)
52 return;
53
54 gradient->resize( from_grad.size() );
55 for (unsigned igrad=0; igrad<from_grad.size(); igrad++)
56 (*gradient)[igrad] = static_cast<Result> (from_grad[igrad]);
57 }
58
61
62 };
63
64 template<class To, class From>
65 To* cast (From* from)
66 { return new Cast<To, From> (from); }
67
68 template<class To, class From>
69 To* cast (Reference::To<From>& from)
70 { return new Cast<To, From> (from.get()); }
71
72}
73
74#endif
75
Converts an evaluable model to one of another type.
Definition Cast.h:20
Reference::To< From > from
The model to be cast to a different type.
Definition Cast.h:60
Cast(From *_from)
Default constructor.
Definition Cast.h:27
void calculate(Result &result, std::vector< Result > *gradient)
Convert.
Definition Cast.h:40
Type * get() const
Namespace in which all modeling and calibration related code is declared.
Definition ExampleComplex2.h:16

Generated using doxygen 1.14.0