~thumper/nux/replace-nfilename

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
#ifndef NUXCORE_PROPERTY_INL_H
#define NUXCORE_PROPERTY_INL_H

namespace nux {

template <typename VALUE_TYPE>
ConnectableProperty<VALUE_TYPE>::ConnectableProperty()
  : value_(VALUE_TYPE())
  , notify_(true)
{}

template <typename VALUE_TYPE>
ConnectableProperty<VALUE_TYPE>::ConnectableProperty(VALUE_TYPE const& initial)
  : value_(initial)
  , notify_(true)
{}

template <typename VALUE_TYPE>
VALUE_TYPE const& ConnectableProperty<VALUE_TYPE>::operator=(VALUE_TYPE const& value)
{
  set(value);
  return value_;
}

template <typename VALUE_TYPE>
ConnectableProperty<VALUE_TYPE>::operator VALUE_TYPE const&() const
{
  return value_;
}

template <typename VALUE_TYPE>
VALUE_TYPE const& ConnectableProperty<VALUE_TYPE>::operator()() const
{
  return value_;
}

template <typename VALUE_TYPE>
void ConnectableProperty<VALUE_TYPE>::operator()(VALUE_TYPE const& value)
{
  set(value);
}

template <typename VALUE_TYPE>
void ConnectableProperty<VALUE_TYPE>::disable_notifications()
{
  notify_ = false;
}

template <typename VALUE_TYPE>
void ConnectableProperty<VALUE_TYPE>::enable_notifications()
{
  notify_ = true;
}

  // get and set access
template <typename VALUE_TYPE>
VALUE_TYPE const& ConnectableProperty<VALUE_TYPE>::get() const
{
  return value_;
}

template <typename VALUE_TYPE>
void ConnectableProperty<VALUE_TYPE>::set(VALUE_TYPE const& value)
{
  if (value != value_) {
    value_ = value;
    if (notify_) {
      changed.emit(value_);
    }
  }
}

// We need to provide a default constructor since we hide the copy ctor.
inline Introspectable::Introspectable()
{}

inline void Introspectable::add_property(std::string const& name,
                                         PropertyBase* property)
{
    // check to see if it exists and if it does barf horribly as we can't
    // have two properties with the same name;
    properties_[name] = property;
}

inline bool Introspectable::set_property(std::string const& name,
                                         const char* value)
{
    PropertyContainer::iterator i = properties_.find(name);
    if (i == properties_.end())
        return false;
    else
        return i->second->set_value(value);
}

template <typename T>
bool Introspectable::set_property(std::string const& name, T const& value)
{
    PropertyContainer::iterator i = properties_.find(name);
    if (i == properties_.end())
        return false;
    else
    {
        return i->second->set_value(type::PropertyTrait<T>::to_string(value));
    }
}

template <typename T>
T Introspectable::get_property(std::string const& name, T* foo)
{
    PropertyContainer::iterator i = properties_.find(name);
    if (i == properties_.end())
        return T();

    std::string s = i->second->get_serialized_value();
    std::pair<T, bool> result = type::PropertyTrait<T>::from_string(s);
    // If this is called with a template type that the property does not
    // support nice conversion to, you'll get no error, but will get
    // a default constructed T.  We could use an exception here.
    return result.first;
}


template <typename T>
Property<T>::Property(Introspectable* owner,
                      std::string const& name)
: Base()
, name_(name)
{
    owner->add_property(name, this);
}

template <typename T>
Property<T>::Property(Introspectable* owner,
                      std::string const& name,
                      T const& initial)
: Base(initial)
, name_(name)
{
    owner->add_property(name, this);
}

template <typename T>
bool Property<T>::set_value(std::string const& serialized_form)
{
    std::pair<ValueType, bool> result = TraitType::from_string(serialized_form);
    if (result.second) {
      set(result.first);
    }
    return result.second;
}

template <typename T>
std::string Property<T>::get_serialized_value() const
{
    return TraitType::to_string(Base::get());
}

template <typename T>
typename Property<T>::ValueType const& Property<T>::operator=(typename Property<T>::ValueType const& value)
{
    set(value);
    // There are no arguments to ‘get’ that depend on a template parameter,
    // so we explicitly specify Base.
    return Base::get();
}



}

#endif