synfig-core  1.0.3
weightedvalue.h
Go to the documentation of this file.
1 /* === S Y N F I G ========================================================= */
21 /* ========================================================================= */
22 
23 /* === S T A R T =========================================================== */
24 
25 #ifndef __SYNFIG_WEIGHTEDVALUE_H
26 #define __SYNFIG_WEIGHTEDVALUE_H
27 
28 /* === H E A D E R S ======================================================= */
29 
30 #include "real.h"
31 #include "type.h"
32 #include "value.h"
33 
34 /* === M A C R O S ========================================================= */
35 
36 /* === T Y P E D E F S ===================================================== */
37 
38 /* === C L A S S E S & S T R U C T S ======================================= */
39 
40 namespace synfig {
41 
42 template<typename T>
44 {
45 public:
46  typedef T ValueType;
50  explicit WeightedValue(Real weight): weight(weight), value() { }
51  explicit WeightedValue(Real weight, const ValueType &value): weight(weight), value(value) { }
52 };
53 
54 namespace types_namespace {
56  {
57  public:
58  virtual Type& get_contained_type() = 0;
59  virtual ValueBase create_weighted_value(Real weight, const ValueBase &value) = 0;
60  virtual Real extract_weight(const ValueBase &value) = 0;
61  virtual ValueBase extract_value(const ValueBase &value) = 0;
62  };
63 
64  template<typename T>
66  {
67  static String to_string(const WeightedValue<T> &x) {
68  return etl::strprintf("Weight (%f) %s", x.weight, value_to_string(x.value).c_str());
69  }
70  void initialize_vfunc(Description &description)
71  {
72  Type &type = get_type_alias(T()).type;
73  type.initialize();
74 
75  Type::initialize_vfunc(description);
76  description.name = "weighted_" + type.description.name;
77  description.local_name = N_("weighted") + String(" ") + type.description.local_name;
78  register_all_but_compare<WeightedValue<T>, TypeWeightedValue<T>::to_string>();
79  }
80  Type& get_contained_type() { return get_type_alias(T()).type; }
81  ValueBase create_weighted_value(Real weight, const ValueBase &value)
82  { return WeightedValue<T>(weight, value.get(T())); };
83  Real extract_weight(const ValueBase &value)
84  { return value.get(WeightedValue<T>()).weight; };
85  ValueBase extract_value(const ValueBase &value)
86  { return value.get(WeightedValue<T>()).value; };
87  public:
89  };
90 
91  template<typename T>
92  TypeWeightedValue<T> TypeWeightedValue<T>::instance;
93 
94  template<typename T>
96 }
97 
98 
99 }; // END of namespace synfig
100 
101 #endif