synfig-core  1.0.3
pair.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_PAIR_H
26 #define __SYNFIG_PAIR_H
27 
28 /* === H E A D E R S ======================================================= */
29 
30 #include <utility>
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 namespace types_namespace {
43  class TypePairBase: public Type
44  {
45  public:
46  virtual Type& get_first_type() = 0;
47  virtual Type& get_second_type() = 0;
48  virtual ValueBase create_value(const ValueBase &first, const ValueBase &second) = 0;
49  virtual ValueBase extract_first(const ValueBase &value) = 0;
50  virtual ValueBase extract_second(const ValueBase &value) = 0;
51  };
52 
53  template<typename T1, typename T2>
54  class TypePair: public TypePairBase
55  {
56  typedef std::pair<T1, T2> ValueType;
57 
58  static String to_string(const ValueType &x) {
59  return etl::strprintf("Pair (%s, %s)", value_to_string(x.first).c_str(), value_to_string(x.second).c_str());
60  }
61  void initialize_vfunc(Description &description)
62  {
63  Type &type_first = get_type_alias(T1()).type;
64  type_first.initialize();
65  Type &type_second = get_type_alias(T2()).type;
66  type_second.initialize();
67 
68  Type::initialize_vfunc(description);
69  description.name = "pair_" + type_first.description.name + "_" + type_second.description.name;
70  description.local_name = N_("Pair") + String(" (")
71  + type_first.description.local_name + String(", ")
72  + type_second.description.local_name + String(")");
73  register_all_but_compare<ValueType, TypePair::to_string>();
74  }
75  virtual Type& get_first_type() { return get_type_alias(T1()).type; }
76  virtual Type& get_second_type() { return get_type_alias(T2()).type; }
77  virtual ValueBase create_value(const ValueBase &first, const ValueBase &second)
78  { return ValueType(first.get(T1()), second.get(T2())); };
79  virtual ValueBase extract_first(const ValueBase &value)
80  { return value.get(ValueType()).first; };
81  virtual ValueBase extract_second(const ValueBase &value)
82  { return value.get(ValueType()).second; };
83  public:
84  static TypePair instance;
85  };
86 
87  template<typename T1, typename T2>
88  TypePair<T1, T2> TypePair<T1, T2>::instance;
89 
90  template<typename T1, typename T2>
91  TypeAlias< std::pair<T1, T2> > get_type_alias(typename std::pair<T1, T2> const&)
93 }
94 
95 
96 }; // END of namespace synfig
97 
98 #endif