summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/configuration_variable.h
blob: cdd9b2428449de6b94f1e44281a2f93bc36abae5 (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
#ifndef __ardour_configuration_variable_h__
#define __ardour_configuration_variable_h__

#include <sstream>
#include <ostream>

#include <pbd/xml++.h>

namespace ARDOUR {

class ConfigVariableBase {
  public:
	ConfigVariableBase (std::string str) : _name (str), _is_user (false) {}
	virtual ~ConfigVariableBase() {}

	std::string name() const { return _name; }
	bool is_user() const { return _is_user; }
	void set_is_user (bool yn) { _is_user = yn; }
	
	virtual void add_to_node (XMLNode& node) = 0;
	virtual bool set_from_node (const XMLNode& node) = 0;

  protected:
	std::string _name;
	bool _is_user;
};

template<class T>
class ConfigVariable : public ConfigVariableBase
{
  public:
	ConfigVariable (std::string str) : ConfigVariableBase (str) {}
	ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}

	virtual void set (T val) {
		value = val;
	}

	T get() const {
		return value;
	}

	void add_to_node (XMLNode& node) {
		std::stringstream ss;
		ss << value;
		XMLNode* child = new XMLNode ("Option");
		child->add_property ("name", _name);
		child->add_property ("value", ss.str());
		node.add_child_nocopy (*child);
	}

	bool set_from_node (const XMLNode& node) {
		const XMLProperty* prop;
		XMLNodeList nlist;
		XMLNodeConstIterator niter;
		XMLNode* child;

		nlist = node.children();
		
		for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
			
			child = *niter;
			
			if (child->name() == "Option") {
				if ((prop = child->property ("name")) != 0) {
					if (prop->value() == _name) {
						if ((prop = child->property ("value")) != 0) {
							std::stringstream ss;
							ss << prop->value();
							ss >> value;
							return true;
						}
					}
				}
			}
		}

		return false;
	}

  protected:
	virtual T get_for_save() { return value; }
	T value;
};

template<class T>
class ConfigVariableWithMutation : public ConfigVariable<T>
{
  public:
	ConfigVariableWithMutation (std::string name, T val, T (*m)(T)) 
		: ConfigVariable<T> (name, val), mutator (m) {}

	void set (T val) {
		unmutated_value = val;
		ConfigVariable<T>::set (mutator (val));
	}

  protected:
	virtual T get_for_save() { return unmutated_value; }
	T unmutated_value;
	T (*mutator)(T);
};

}

#endif /* __ardour_configuration_variable_h__ */