diff options
Diffstat (limited to '')
-rw-r--r-- | linden/indra/newview/llviewercontrol.h | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/linden/indra/newview/llviewercontrol.h b/linden/indra/newview/llviewercontrol.h index c827f37..5de3e38 100644 --- a/linden/indra/newview/llviewercontrol.h +++ b/linden/indra/newview/llviewercontrol.h | |||
@@ -65,4 +65,114 @@ extern LLControlGroup gCrashSettings; | |||
65 | extern LLString gLastRunVersion; | 65 | extern LLString gLastRunVersion; |
66 | extern LLString gCurrentVersion; | 66 | extern LLString gCurrentVersion; |
67 | 67 | ||
68 | //! Helper function for LLCachedControl | ||
69 | template <class T> | ||
70 | eControlType get_control_type(const T& in, LLSD& out) | ||
71 | { | ||
72 | llerrs << "Usupported control type: " << typeid(T).name() << "." << llendl; | ||
73 | return TYPE_COUNT; | ||
74 | } | ||
75 | |||
76 | //! Publish/Subscribe object to interact with LLControlGroups. | ||
77 | |||
78 | //! An LLCachedControl instance to connect to a LLControlVariable | ||
79 | //! without have to manually create and bind a listener to a local | ||
80 | //! object. | ||
81 | template <class T> | ||
82 | class LLCachedControl | ||
83 | { | ||
84 | T mCachedValue; | ||
85 | LLPointer<LLControlVariable> mControl; | ||
86 | boost::signals::connection mConnection; | ||
87 | |||
88 | public: | ||
89 | LLCachedControl(const std::string& name, | ||
90 | const T& default_value, | ||
91 | const std::string& comment = "Declared In Code") | ||
92 | { | ||
93 | mControl = gSavedSettings.getControl(name); | ||
94 | if(mControl.isNull()) | ||
95 | { | ||
96 | declareTypedControl(gSavedSettings, name, default_value, comment); | ||
97 | mControl = gSavedSettings.getControl(name); | ||
98 | if(mControl.isNull()) | ||
99 | { | ||
100 | llerrs << "The control could not be created!!!" << llendl; | ||
101 | } | ||
102 | |||
103 | mCachedValue = default_value; | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | mCachedValue = (const T&)mControl->getValue(); | ||
108 | } | ||
109 | |||
110 | // Add a listener to the controls signal... | ||
111 | mControl->getSignal()->connect( | ||
112 | boost::bind(&LLCachedControl<T>::handleValueChange, this, _1) | ||
113 | ); | ||
114 | } | ||
115 | |||
116 | ~LLCachedControl() | ||
117 | { | ||
118 | if(mConnection.connected()) | ||
119 | { | ||
120 | mConnection.disconnect(); | ||
121 | } | ||
122 | } | ||
123 | |||
124 | LLCachedControl& operator =(const T& newvalue) | ||
125 | { | ||
126 | setTypeValue(*mControl, newvalue); | ||
127 | } | ||
128 | |||
129 | operator const T&() { return mCachedValue; } | ||
130 | |||
131 | private: | ||
132 | void declareTypedControl(LLControlGroup& group, | ||
133 | const std::string& name, | ||
134 | const T& default_value, | ||
135 | const std::string& comment) | ||
136 | { | ||
137 | LLSD init_value; | ||
138 | eControlType type = get_control_type<T>(default_value, init_value); | ||
139 | if(type < TYPE_COUNT) | ||
140 | { | ||
141 | group.declareControl(name, type, init_value, comment, FALSE); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | bool handleValueChange(const LLSD& newvalue) | ||
146 | { | ||
147 | mCachedValue = (const T &)newvalue; | ||
148 | return true; | ||
149 | } | ||
150 | |||
151 | void setTypeValue(LLControlVariable& c, const T& v) | ||
152 | { | ||
153 | // Implicit conversion from T to LLSD... | ||
154 | c.set(v); | ||
155 | } | ||
156 | }; | ||
157 | |||
158 | template <> eControlType get_control_type<U32>(const U32& in, LLSD& out); | ||
159 | template <> eControlType get_control_type<S32>(const S32& in, LLSD& out); | ||
160 | template <> eControlType get_control_type<F32>(const F32& in, LLSD& out); | ||
161 | template <> eControlType get_control_type<bool> (const bool& in, LLSD& out); | ||
162 | // Yay BOOL, its really an S32. | ||
163 | //template <> eControlType get_control_type<BOOL> (const BOOL& in, LLSD& out) | ||
164 | template <> eControlType get_control_type<std::string>(const std::string& in, LLSD& out); | ||
165 | template <> eControlType get_control_type<LLVector3>(const LLVector3& in, LLSD& out); | ||
166 | template <> eControlType get_control_type<LLVector3d>(const LLVector3d& in, LLSD& out); | ||
167 | template <> eControlType get_control_type<LLRect>(const LLRect& in, LLSD& out); | ||
168 | template <> eControlType get_control_type<LLColor4>(const LLColor4& in, LLSD& out); | ||
169 | template <> eControlType get_control_type<LLColor3>(const LLColor3& in, LLSD& out); | ||
170 | template <> eControlType get_control_type<LLColor4U>(const LLColor4U& in, LLSD& out); | ||
171 | template <> eControlType get_control_type<LLSD>(const LLSD& in, LLSD& out); | ||
172 | |||
173 | //#define TEST_CACHED_CONTROL 1 | ||
174 | #ifdef TEST_CACHED_CONTROL | ||
175 | void test_cached_control(); | ||
176 | #endif TEST_CACHED_CONTROL | ||
177 | |||
68 | #endif // LL_LLVIEWERCONTROL_H | 178 | #endif // LL_LLVIEWERCONTROL_H |