Trying out new LLCachedControl implementation.

This commit is contained in:
Shyotl
2011-07-16 23:36:56 -05:00
parent 5ec3795716
commit 6ad9367c24
27 changed files with 893 additions and 785 deletions

View File

@@ -33,6 +33,7 @@
#ifndef LL_LLCONTROL_H
#define LL_LLCONTROL_H
#include "llboost.h"
#include "llevent.h"
#include "llnametable.h"
#include "llmap.h"
@@ -40,6 +41,7 @@
#include "llrect.h"
#include "v4color.h"
#include "v4coloru.h"
#include "llinstancetracker.h"
#include "llcontrolgroupreader.h"
@@ -55,7 +57,16 @@
#endif
#include <boost/bind.hpp>
#if LL_WINDOWS
#pragma warning (push)
#pragma warning (disable : 4263) // boost::signals2::expired_slot::what() has const mismatch
#pragma warning (disable : 4264)
#endif
#include <boost/signals2.hpp>
#if LL_WINDOWS
#pragma warning (pop)
#endif
#if LL_WINDOWS
# if (_MSC_VER >= 1300 && _MSC_VER < 1400)
@@ -66,7 +77,6 @@
class LLVector3;
class LLVector3d;
class LLColor3;
class LLColor4U;
const BOOL NO_PERSIST = FALSE;
@@ -86,8 +96,8 @@ typedef enum e_control_type
TYPE_VEC3D,
TYPE_RECT,
TYPE_COL4,
TYPE_COL4U, //exist merely for legacy colors.xml support. Not a real control type.
TYPE_COL3,
TYPE_COL4U,
TYPE_LLSD,
TYPE_COUNT
} eControlType;
@@ -95,7 +105,10 @@ typedef enum e_control_type
class LLControlVariable : public LLRefCount
{
friend class LLControlGroup;
typedef boost::signals2::signal<void(const LLSD&)> signal_t;
public:
typedef boost::signals2::signal<bool(LLControlVariable* control, const LLSD&), boost_boolean_combiner> validate_signal_t;
typedef boost::signals2::signal<void(LLControlVariable* control, const LLSD&)> commit_signal_t;
private:
std::string mName;
@@ -105,7 +118,8 @@ private:
bool mHideFromSettingsEditor;
std::vector<LLSD> mValues;
boost::shared_ptr<signal_t> mSignal; //Signals are non-copyable. Therefore, using a pointer so vars can 'share' signals for COA
boost::shared_ptr<commit_signal_t> mCommitSignal; //Signals are non-copyable. Therefore, using a pointer so vars can 'share' signals for COA
boost::shared_ptr<validate_signal_t> mValidateSignal;
//COA stuff:
bool mIsCOA; //To have COA connection set.
@@ -121,12 +135,14 @@ public:
const std::string& getName() const { return mName; }
const std::string& getComment() const { return mComment; }
eControlType type() { return mType; }
eControlType type() const { return mType; }
bool isType(eControlType tp) const { return tp == mType; }
void resetToDefault(bool fire_signal = false);
signal_t* getSignal() { return mSignal.get(); }
commit_signal_t* getSignal() { return mCommitSignal.get(); } // shorthand for commit signal
commit_signal_t* getCommitSignal() { return mCommitSignal.get(); }
validate_signal_t* getValidateSignal() { return mValidateSignal.get(); }
bool isDefault() { return (mValues.size() == 1); }
bool isSaveValueDefault();
@@ -146,7 +162,7 @@ public:
void firePropertyChanged()
{
(*mSignal)(mValues.back());
(*mCommitSignal)(this,mValues.back());
}
//COA stuff
@@ -161,31 +177,57 @@ public:
mIsCOAParent=IsParent;
mCOAConnectedVar=pConnect;
if(!IsParent)
mSignal = pConnect->mSignal; //Share a single signal.
{
mCommitSignal = pConnect->mCommitSignal; //Share a single signal.
mValidateSignal = pConnect->mValidateSignal;
}
}
private:
LLSD getComparableValue(const LLSD& value);
bool llsd_compare(const LLSD& a, const LLSD & b);
};
typedef LLPointer<LLControlVariable> LLControlVariablePtr;
//! Helper functions for converting between static types and LLControl values
template <class T>
eControlType get_control_type()
{
llwarns << "Usupported control type: " << typeid(T).name() << "." << llendl;
return TYPE_COUNT;
}
template <class T>
LLSD convert_to_llsd(const T& in)
{
// default implementation
return LLSD(in);
}
template <class T>
T convert_from_llsd(const LLSD& sd, eControlType type, const std::string& control_name)
{
// needs specialization
return T(sd);
}
//const U32 STRING_CACHE_SIZE = 10000;
class LLControlGroup : public LLControlGroupReader
class LLControlGroup : public LLInstanceTracker<LLControlGroup, std::string>
{
protected:
typedef std::map<std::string, LLPointer<LLControlVariable> > ctrl_name_table_t;
typedef std::map<std::string, LLControlVariablePtr > ctrl_name_table_t;
ctrl_name_table_t mNameTable;
std::set<std::string> mWarnings;
std::string mTypeString[TYPE_COUNT];
eControlType typeStringToEnum(const std::string& typestr);
std::string typeEnumToString(eControlType typeenum);
std::string typeEnumToString(eControlType typeenum);
std::set<std::string> mIncludedFiles; //To prevent perpetual recursion.
public:
LLControlGroup();
LLControlGroup(const std::string& name);
~LLControlGroup();
void cleanup();
typedef LLInstanceTracker<LLControlGroup, std::string>::instance_iter instance_iter;
LLControlVariable* getControl(std::string const& name);
LLControlVariable const* getControl(std::string const& name) const;
@@ -205,7 +247,6 @@ public:
BOOL declareVec3(const std::string& name, const LLVector3 &initial_val,const std::string& comment, BOOL persist = TRUE);
BOOL declareVec3d(const std::string& name, const LLVector3d &initial_val, const std::string& comment, BOOL persist = TRUE);
BOOL declareRect(const std::string& name, const LLRect &initial_val, const std::string& comment, BOOL persist = TRUE);
BOOL declareColor4U(const std::string& name, const LLColor4U &initial_val, const std::string& comment, BOOL persist = TRUE);
BOOL declareColor4(const std::string& name, const LLColor4 &initial_val, const std::string& comment, BOOL persist = TRUE);
BOOL declareColor3(const std::string& name, const LLColor3 &initial_val, const std::string& comment, BOOL persist = TRUE);
BOOL declareLLSD(const std::string& name, const LLSD &initial_val, const std::string& comment, BOOL persist = TRUE);
@@ -213,25 +254,44 @@ public:
std::string findString(const std::string& name);
std::string getString(const std::string& name) const;
LLWString getWString(const std::string& name);
std::string getText(const std::string& name);
LLVector3 getVector3(const std::string& name);
LLVector3d getVector3d(const std::string& name);
LLRect getRect(const std::string& name);
std::string getText(const std::string& name);
BOOL getBOOL(const std::string& name);
S32 getS32(const std::string& name);
F32 getF32(const std::string& name);
U32 getU32(const std::string& name);
LLWString getWString(const std::string& name);
LLVector3 getVector3(const std::string& name);
LLVector3d getVector3d(const std::string& name);
LLRect getRect(const std::string& name);
LLSD getLLSD(const std::string& name);
// Note: If an LLColor4U control exists, it will cast it to the correct
// LLColor4 for you.
LLColor4 getColor(const std::string& name);
LLColor4U getColor4U(const std::string& name);
LLColor4 getColor4(const std::string& name);
LLColor3 getColor3(const std::string& name);
// generic getter
template<typename T> T get(const std::string& name) const
{
const LLControlVariable* control = getControl(name);
LLSD value;
eControlType type = TYPE_COUNT;
if (control)
{
value = control->get();
type = control->type();
}
else
{
llwarns << "Control " << name << " not found." << llendl;
}
return convert_from_llsd<T>(value, type, name);
}
void setBOOL(const std::string& name, BOOL val);
void setS32(const std::string& name, S32 val);
void setF32(const std::string& name, F32 val);
@@ -240,12 +300,27 @@ public:
void setVector3(const std::string& name, const LLVector3 &val);
void setVector3d(const std::string& name, const LLVector3d &val);
void setRect(const std::string& name, const LLRect &val);
void setColor4U(const std::string& name, const LLColor4U &val);
void setColor4(const std::string& name, const LLColor4 &val);
void setColor3(const std::string& name, const LLColor3 &val);
void setLLSD(const std::string& name, const LLSD& val);
void setValue(const std::string& name, const LLSD& val);
// type agnostic setter that takes LLSD
void setUntypedValue(const std::string& name, const LLSD& val);
// generic setter
template<typename T> void set(const std::string& name, const T& val)
{
LLControlVariable* control = getControl(name);
if (control && control->isType(get_control_type<T>()))
{
control->set(convert_to_llsd(val));
}
else
{
llerrs << "Invalid control " << name << llendl;
}
}
BOOL controlExists(const std::string& name) const;
@@ -254,7 +329,7 @@ public:
// as the given type.
U32 loadFromFileLegacy(const std::string& filename, BOOL require_declaration = TRUE, eControlType declare_as = TYPE_STRING);
U32 saveToFile(const std::string& filename, BOOL nondefault_only);
U32 loadFromFile(const std::string& filename, bool default_values = false);
U32 loadFromFile(const std::string& filename, bool default_values = false, bool save_values = true);
void resetToDefaults();
@@ -275,89 +350,147 @@ public:
bool handleCOASettingChange(const LLSD& newvalue);
};
//! Helper function for LLCachedControl
template <class T>
eControlType get_control_type(const T& in, LLSD& out)
{
llerrs << "Usupported control type: " << typeid(T).name() << "." << llendl;
return TYPE_COUNT;
}
//! Publish/Subscribe object to interact with LLControlGroups.
//! An LLCachedControl instance to connect to a LLControlVariable
//! Use an LLCachedControl instance to connect to a LLControlVariable
//! without have to manually create and bind a listener to a local
//! object.
template <class T>
class LLCachedControl
class LLControlCache : public LLRefCount, public LLInstanceTracker<LLControlCache<T>, std::string>
{
private:
T mCachedValue;
LLPointer<LLControlVariable> mControl;
boost::signals2::connection mConnection;
LLControlGroup *mControlGroup;
public:
LLCachedControl(const std::string& name, const T& default_value, LLControlGroup *group, const std::string& comment = "Declared In Code")
{Init(name,default_value,comment,*group);} //for gSavedPerAccountSettings, etc
LLCachedControl(const std::string& name, const T& default_value, LLControlGroup &group, const std::string& comment = "Declared In Code")
{Init(name,default_value,comment,group);} //for LLUI::sConfigGroup, etc
LLCachedControl(const std::string& name,
// This constructor will declare a control if it doesn't exist in the contol group
LLControlCache(LLControlGroup& group,
const std::string& name,
const T& default_value,
const std::string& comment = "Declared In Code",
LLControlGroup &group = gSavedSettings)
{Init(name,default_value,comment,group);} //for default (gSavedSettings)
private:
void Init( const std::string& name,
const T& default_value,
const std::string& comment,
LLControlGroup &group)
const std::string& comment)
: LLInstanceTracker<LLControlCache<T>, std::string >(name)
{
mControlGroup = &group;
mControl = mControlGroup->getControl(name);
if(mControl.isNull())
if(!group.controlExists(name))
{
declareTypedControl(*mControlGroup, name, default_value, comment);
mControl = mControlGroup->getControl(name);
if(mControl.isNull())
if(!declareTypedControl(group, name, default_value, comment))
{
llerrs << "The control could not be created!!!" << llendl;
}
mCachedValue = default_value;
}
else
bindToControl(group, name);
}
LLControlCache(LLControlGroup& group,
const std::string& name)
: LLInstanceTracker<LLControlCache<T>, std::string >(name)
{
if(!group.controlExists(name))
{
handleValueChange(mControl->getValue());
llerrs << "Control named " << name << "not found." << llendl;
}
if(mConnection.connected())
mConnection.disconnect();
bindToControl(group, name);
}
~LLControlCache()
{
}
const T& getValue() const { return mCachedValue; }
void setTypeValue(const T& v) { mControl->set(convert_to_llsd(v)); }
private:
void bindToControl(LLControlGroup& group, const std::string& name)
{
LLControlVariablePtr controlp = group.getControl(name);
mType = controlp->type();
mCachedValue = convert_from_llsd<T>(controlp->get(), mType, name);
// Add a listener to the controls signal...
// and store the connection...
mConnection = mControl->getSignal()->connect(
boost::bind(&LLCachedControl<T>::handleValueChange, this, _1)
mConnection = controlp->getSignal()->connect(
boost::bind(&LLControlCache<T>::handleValueChange, this, _2)
);
mType = controlp->type();
mControl = controlp;
}
public:
~LLCachedControl()
bool declareTypedControl(LLControlGroup& group,
const std::string& name,
const T& default_value,
const std::string& comment)
{
if(mConnection.connected())
LLSD init_value;
eControlType type = get_control_type<T>();
init_value = convert_to_llsd(default_value);
if(type < TYPE_COUNT)
{
mConnection.disconnect();
group.declareControl(name, type, init_value, comment, FALSE);
return true;
}
return false;
}
bool handleValueChange(const LLSD& newvalue)
{
mCachedValue = convert_from_llsd<T>(newvalue, mType, "");
return true;
}
private:
T mCachedValue;
LLPointer<LLControlVariable> mControl; //Hold a pointer to the control so setters are possible in cachedctrl
eControlType mType;
boost::signals2::scoped_connection mConnection;
};
template <typename T>
class LLCachedControl
{
public:
LLCachedControl(const std::string& name,
const T& default_value,
const std::string& comment = "Declared In Code")
{
mCachedControlPtr = LLControlCache<T>::getInstance(name);
if (mCachedControlPtr.isNull())
{
mCachedControlPtr = new LLControlCache<T>(gSavedSettings, name, default_value, comment);
}
}
LLCachedControl& operator =(const T& newvalue)
LLCachedControl(const std::string& name)
{
setTypeValue(*mControl, newvalue);
return *this;
mCachedControlPtr = LLControlCache<T>::getInstance(name);
if (mCachedControlPtr.isNull())
{
mCachedControlPtr = new LLControlCache<T>(gSavedSettings, name);
}
}
operator const T&() const { return mCachedValue; }
LLCachedControl(LLControlGroup& group,
const std::string& name,
const T& default_value,
const std::string& comment = "Declared In Code")
{
mCachedControlPtr = LLControlCache<T>::getInstance(name);
if (mCachedControlPtr.isNull())
{
mCachedControlPtr = new LLControlCache<T>(group, name, default_value, comment);
}
}
LLCachedControl(LLControlGroup& group,
const std::string& name)
{
mCachedControlPtr = LLControlCache<T>::getInstance(name);
if (mCachedControlPtr.isNull())
{
mCachedControlPtr = new LLControlCache<T>(group, name);
}
}
operator const T&() const { return mCachedControlPtr->getValue(); }
operator boost::function<const T&()> () const { return boost::function<const T&()>(*this); }
const T& operator()() const { return mCachedControlPtr->getValue(); }
/* Sometimes implicit casting doesn't work.
For instance, something like "LLCachedControl<LLColor4> color("blah",LLColor4()); color.getValue();"
will not compile as it will look for the function getValue() in LLCachedControl, which doesn't exist.
@@ -365,71 +498,54 @@ public:
Manually casting to (const T) would work too, but it's ugly and requires knowledge of LLCachedControl's internals
*/
const T &get() const { return mCachedValue; }
const T &get() const { return mCachedControlPtr->getValue(); }
LLCachedControl& operator =(const T& newvalue)
{
mCachedControlPtr->setTypeValue(newvalue);
return *this;
}
private:
void declareTypedControl(LLControlGroup& group,
const std::string& name,
const T& default_value,
const std::string& comment)
{
LLSD init_value;
eControlType type = get_control_type<T>(default_value, init_value);
if(type < TYPE_COUNT)
{
group.declareControl(name, type, init_value, comment, FALSE);
}
}
void setValue(const LLSD& newvalue) //default behavior
{
mCachedValue = (const T &)newvalue;
}
bool handleValueChange(const LLSD& newvalue)
{
setValue(newvalue);
return true;
}
void setTypeValue(LLControlVariable& c, const T& v)
{
// Implicit conversion from T to LLSD...
c.set(v);
}
LLPointer<LLControlCache<T> > mCachedControlPtr;
};
template <> inline void LLCachedControl<LLColor4>::setValue(const LLSD& newvalue)
{
if(this->mControl->isType(TYPE_COL4U))
//a color4u LLSD cannot be auto-converted to color4.. so do it manually.
this->mCachedValue.set(LLColor4U(newvalue));
else
this->mCachedValue = (const LLColor4 &)newvalue;
}
template <> inline void LLCachedControl<U32>::setValue(const LLSD& newvalue)
{
if(mControl->isType(TYPE_U32) || mControl->isType(TYPE_S32)) //LLSD does not support U32 fully
mCachedValue = (U32)newvalue.asInteger();
else if(this->mControl->isType(TYPE_F32))
mCachedValue = (U32)newvalue.asReal();
else
mCachedValue = (U32)0; //What to do...
}
//Following is actually defined in newview/llviewercontrol.cpp, but extern access is fine (Unless GCC bites me)
template <> eControlType get_control_type<U32>(const U32& in, LLSD& out);
template <> eControlType get_control_type<S32>(const S32& in, LLSD& out);
template <> eControlType get_control_type<F32>(const F32& in, LLSD& out);
template <> eControlType get_control_type<bool> (const bool& in, LLSD& out);
template <> eControlType get_control_type<U32>();
template <> eControlType get_control_type<S32>();
template <> eControlType get_control_type<F32>();
template <> eControlType get_control_type<bool>();
// Yay BOOL, its really an S32.
//template <> eControlType get_control_type<BOOL> (const BOOL& in, LLSD& out)
template <> eControlType get_control_type<std::string>(const std::string& in, LLSD& out);
template <> eControlType get_control_type<LLVector3>(const LLVector3& in, LLSD& out);
template <> eControlType get_control_type<LLVector3d>(const LLVector3d& in, LLSD& out);
template <> eControlType get_control_type<LLRect>(const LLRect& in, LLSD& out);
template <> eControlType get_control_type<LLColor4>(const LLColor4& in, LLSD& out);
template <> eControlType get_control_type<LLColor3>(const LLColor3& in, LLSD& out);
template <> eControlType get_control_type<LLColor4U>(const LLColor4U& in, LLSD& out);
template <> eControlType get_control_type<LLSD>(const LLSD& in, LLSD& out);
//template <> eControlType get_control_type<BOOL> ()
template <> eControlType get_control_type<std::string>();
template <> eControlType get_control_type<LLVector3>();
template <> eControlType get_control_type<LLVector3d>();
template <> eControlType get_control_type<LLRect>();
template <> eControlType get_control_type<LLColor4>();
template <> eControlType get_control_type<LLColor3>();
template <> eControlType get_control_type<LLSD>();
template <> LLSD convert_to_llsd<U32>(const U32& in);
template <> LLSD convert_to_llsd<LLVector3>(const LLVector3& in);
template <> LLSD convert_to_llsd<LLVector3d>(const LLVector3d& in);
template <> LLSD convert_to_llsd<LLRect>(const LLRect& in);
template <> LLSD convert_to_llsd<LLColor4>(const LLColor4& in);
template <> LLSD convert_to_llsd<LLColor3>(const LLColor3& in);
template<> std::string convert_from_llsd<std::string>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> LLWString convert_from_llsd<LLWString>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> LLVector3 convert_from_llsd<LLVector3>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> LLVector3d convert_from_llsd<LLVector3d>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> LLRect convert_from_llsd<LLRect>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> bool convert_from_llsd<bool>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> S32 convert_from_llsd<S32>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> F32 convert_from_llsd<F32>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> U32 convert_from_llsd<U32>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> LLColor3 convert_from_llsd<LLColor3>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> LLColor4 convert_from_llsd<LLColor4>(const LLSD& sd, eControlType type, const std::string& control_name);
template<> LLSD convert_from_llsd<LLSD>(const LLSD& sd, eControlType type, const std::string& control_name);
//#define TEST_CACHED_CONTROL 1
#ifdef TEST_CACHED_CONTROL
void test_cached_control();
#endif // TEST_CACHED_CONTROL
#endif