This commit is contained in:
PodmogilnyjIvan
2021-12-03 03:34:31 -08:00
commit ff4acf84be
542 changed files with 136810 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
/* This file is part of the Pangolin Project.
* http://github.com/stevenlovegrove/Pangolin
*
* Copyright (c) 2011 Steven Lovegrove
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <pangolin/pangolin.h>
#include <list>
#include <string>
#include <fstream>
namespace pangolin
{
struct FrameInput
{
int index;
std::string var;
std::string val;
};
struct PANGOLIN_EXPORT InputRecordRepeat
{
InputRecordRepeat(const std::string& var_record_prefix);
~InputRecordRepeat();
void SetIndex(int id);
void Record();
void Stop();
void LoadBuffer(const std::string& filename);
void SaveBuffer(const std::string& filename);
void ClearBuffer();
void PlayBuffer();
void PlayBuffer(size_t start, size_t end);
void UpdateVariable(const std::string& name );
template<typename T>
inline void UpdateVariable(const Var<T>& var ) {
GuiVarChanged((void*)this, var.var->Meta().full_name, *var.var);
}
size_t Size();
protected:
bool record;
bool play;
int index;
std::ofstream file;
std::string filename;
std::list<FrameInput> play_queue;
std::list<FrameInput> record_queue;
static void GuiVarChanged(void* data, const std::string& name, VarValueGeneric& var);
};
}

View File

@@ -0,0 +1,340 @@
/* This file is part of the Pangolin Project.
* http://github.com/stevenlovegrove/Pangolin
*
* Copyright (c) 2014 Steven Lovegrove
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <stdexcept>
#include <string.h>
#include <cmath>
#include <pangolin/var/varvalue.h>
#include <pangolin/var/varwrapper.h>
#include <pangolin/var/varstate.h>
namespace pangolin
{
template<typename T>
inline void InitialiseNewVarMetaGeneric(
VarValue<T>& v, const std::string& name
) {
// Initialise meta parameters
const std::vector<std::string> parts = pangolin::Split(name,'.');
v.Meta().full_name = name;
v.Meta().friendly = parts.size() > 0 ? parts[parts.size()-1] : "";
v.Meta().range[0] = 0.0;
v.Meta().range[1] = 0.0;
v.Meta().increment = 0.0;
v.Meta().flags = META_FLAG_NONE;
v.Meta().logscale = false;
v.Meta().generic = true;
VarState::I().NotifyNewVar<T>(name, v);
}
template<typename T>
inline void InitialiseNewVarMeta(
VarValue<T>& v, const std::string& name,
double min = 0, double max = 0, int flags = META_FLAG_TOGGLE,
bool logscale = false
) {
// Initialise meta parameters
const std::vector<std::string> parts = pangolin::Split(name,'.');
v.Meta().full_name = name;
v.Meta().friendly = parts.size() > 0 ? parts[parts.size()-1] : "";
v.Meta().range[0] = min;
v.Meta().range[1] = max;
if (std::is_integral<T>::value) {
v.Meta().increment = 1.0;
} else {
v.Meta().increment = (max - min) / 100.0;
}
v.Meta().flags = flags;
v.Meta().logscale = logscale;
v.Meta().generic = false;
VarState::I().NotifyNewVar<T>(name, v);
}
template<typename T>
class Var
{
public:
static T& Attach(
const std::string& name, T& variable,
double min, double max, bool logscale = false
) {
// Find name in VarStore
VarValueGeneric*& v = VarState::I()[name];
if(v) {
throw std::runtime_error(std::string("Var with the following name already exists: ") + name);
}else{
// new VarRef<T> (owned by VarStore)
VarValue<T&>* nv = new VarValue<T&>(variable);
v = nv;
if(logscale) {
if (min <= 0 || max <= 0) {
throw std::runtime_error("LogScale: range of numbers must be positive!");
}
InitialiseNewVarMeta<T&>(*nv, name, std::log(min), std::log(max), META_FLAG_TOGGLE, logscale);
}else{
InitialiseNewVarMeta<T&>(*nv, name, min, max, META_FLAG_TOGGLE, logscale);
}
}
return variable;
}
static T& Attach(
const std::string& name, T& variable, int flags = META_FLAG_NONE
) {
// Find name in VarStore
VarValueGeneric*& v = VarState::I()[name];
if (v) {
throw std::runtime_error(std::string("Var with the following name already exists: ") + name);
}
else{
// new VarRef<T> (owned by VarStore)
VarValue<T&>* nv = new VarValue<T&>(variable);
v = nv;
InitialiseNewVarMeta<T&>(*nv, name, 0.0, 0.0, flags);
}
return variable;
}
static T& Attach(
const std::string& name, T& variable, bool toggle
) {
return Attach(name, variable, toggle ? META_FLAG_TOGGLE : META_FLAG_NONE);
}
~Var()
{
delete ptr;
}
Var( VarValueGeneric& v )
: ptr(0)
{
InitialiseFromGeneric(&v);
}
Var( const std::string& name )
: ptr(0)
{
// Find name in VarStore
VarValueGeneric*& v = VarState::I()[name];
if(v && !v->Meta().generic) {
InitialiseFromGeneric(v);
}else{
// new VarValue<T> (owned by VarStore)
VarValue<T>* nv;
if(v) {
// Specialise generic variable
nv = new VarValue<T>( Convert<T,std::string>::Do( v->str->Get() ) );
delete v;
}else{
nv = new VarValue<T>( T() );
}
v = nv;
var = nv;
InitialiseNewVarMeta(*nv, name);
}
}
Var(const std::string& name, const T& value, int flags = META_FLAG_NONE)
: ptr(0)
{
// Find name in VarStore
VarValueGeneric*& v = VarState::I()[name];
if(v && !v->Meta().generic) {
InitialiseFromGeneric(v);
}else{
// new VarValue<T> (owned by VarStore)
VarValue<T>* nv;
if(v) {
// Specialise generic variable
nv = new VarValue<T>( Convert<T,std::string>::Do( v->str->Get() ) );
delete v;
}else{
nv = new VarValue<T>(value);
}
v = nv;
var = nv;
InitialiseNewVarMeta(*nv, name, 0, 1, flags);
}
}
Var(const std::string& name, const T& value, bool toggle)
: Var(name, value, toggle ? META_FLAG_TOGGLE : META_FLAG_NONE)
{
}
Var(
const std::string& name, const T& value,
double min, double max, bool logscale = false
) : ptr(0)
{
// Find name in VarStore
VarValueGeneric*& v = VarState::I()[name];
if(v && !v->Meta().generic) {
InitialiseFromGeneric(v);
}else{
// new VarValue<T> (owned by VarStore)
VarValue<T>* nv;
if(v) {
// Specialise generic variable
nv = new VarValue<T>( Convert<T,std::string>::Do( v->str->Get() ) );
delete v;
}else{
nv = new VarValue<T>(value);
}
var = nv;
v = nv;
if(logscale) {
if (min <= 0 || max <= 0) {
throw std::runtime_error("LogScale: range of numbers must be positive!");
}
InitialiseNewVarMeta(*nv, name, std::log(min), std::log(max), META_FLAG_TOGGLE, true);
}else{
InitialiseNewVarMeta(*nv, name, min, max);
}
}
}
void Reset()
{
var->Reset();
}
const T& Get() const
{
try{
return var->Get();
}catch(const BadInputException&)
{
const_cast<Var<T> *>(this)->Reset();
return var->Get();
}
}
operator const T& () const
{
return Get();
}
const T* operator->()
{
try{
return &(var->Get());
}catch(const BadInputException&)
{
Reset();
return &(var->Get());
}
}
void operator=(const T& val)
{
var->Set(val);
}
void operator=(const Var<T>& v)
{
var->Set(v.var->Get());
}
VarMeta& Meta()
{
return var->Meta();
}
bool GuiChanged()
{
if(var->Meta().gui_changed) {
var->Meta().gui_changed = false;
return true;
}
return false;
}
VarValueT<T>& Ref()
{
return *var;
}
protected:
// Initialise from existing variable, obtain data / accessor
void InitialiseFromGeneric(VarValueGeneric* v)
{
if( !strcmp(v->TypeId(), typeid(T).name()) ) {
// Same type
var = (VarValueT<T>*)(v);
}else if( std::is_same<T,std::string>::value ) {
// Use types string accessor
var = (VarValueT<T>*)(v->str);
}else if( !strcmp(v->TypeId(), typeid(bool).name() ) ) {
// Wrapper, owned by this object
ptr = new VarWrapper<T,bool>( *(VarValueT<bool>*)v );
var = ptr;
}else if( !strcmp(v->TypeId(), typeid(short).name() ) ) {
// Wrapper, owned by this object
ptr = new VarWrapper<T,short>( *(VarValueT<short>*)v );
var = ptr;
}else if( !strcmp(v->TypeId(), typeid(int).name() ) ) {
// Wrapper, owned by this object
ptr = new VarWrapper<T,int>( *(VarValueT<int>*)v );
var = ptr;
}else if( !strcmp(v->TypeId(), typeid(long).name() ) ) {
// Wrapper, owned by this object
ptr = new VarWrapper<T,long>( *(VarValueT<long>*)v );
var = ptr;
}else if( !strcmp(v->TypeId(), typeid(float).name() ) ) {
// Wrapper, owned by this object
ptr = new VarWrapper<T,float>( *(VarValueT<float>*)v );
var = ptr;
}else if( !strcmp(v->TypeId(), typeid(double).name() ) ) {
// Wrapper, owned by this object
ptr = new VarWrapper<T,double>( *(VarValueT<double>*)v );
var = ptr;
}else{
// other types: have to go via string
// Wrapper, owned by this object
ptr = new VarWrapper<T,std::string>( *(v->str) );
var = ptr;
}
}
// Holds reference to stored variable object
// N.B. mutable because it is a cached value and Get() is advertised as const.
mutable VarValueT<T>* var;
// ptr is non-zero if this object owns the object variable (a wrapper)
VarValueT<T>* ptr;
};
}

View File

@@ -0,0 +1,92 @@
/* This file is part of the Pangolin Project.
* http://github.com/stevenlovegrove/Pangolin
*
* Copyright (c) 2014 Steven Lovegrove
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <pangolin/platform.h>
#include <pangolin/var/var.h>
#include <vector>
namespace pangolin
{
PANGOLIN_EXPORT
void ParseVarsFile(const std::string& filename);
PANGOLIN_EXPORT
void LoadJsonFile(const std::string& filename, const std::string& prefix="");
PANGOLIN_EXPORT
void SaveJsonFile(const std::string& filename, const std::string& prefix="");
PANGOLIN_EXPORT
void ProcessHistoricCallbacks(NewVarCallbackFn callback, void* data, const std::string& filter = "");
PANGOLIN_EXPORT
void RegisterNewVarCallback(NewVarCallbackFn callback, void* data, const std::string& filter = "");
PANGOLIN_EXPORT
void RegisterGuiVarChangedCallback(GuiVarChangedCallbackFn callback, void* data, const std::string& filter = "");
template<typename T>
struct SetVarFunctor
{
SetVarFunctor(const std::string& name, T val) : varName(name), setVal(val) {}
void operator()() { Var<T>(varName).Ref().Set(setVal); }
std::string varName;
T setVal;
};
struct ToggleVarFunctor
{
ToggleVarFunctor(const std::string& name) : varName(name) {}
void operator()() { Var<bool> val(varName,false); val = !val; }
std::string varName;
};
inline bool Pushed(Var<bool>& button)
{
bool val = button;
button = false;
return val;
}
inline bool Pushed(bool& button)
{
bool val = button;
button = false;
return val;
}
template<typename T>
inline std::ostream& operator<<(std::ostream& s, Var<T>& rhs)
{
s << rhs.operator const T &();
return s;
}
}

View File

@@ -0,0 +1,130 @@
/* This file is part of the Pangolin Project.
* http://github.com/stevenlovegrove/Pangolin
*
* Copyright (c) 2014 Steven Lovegrove
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <map>
#include <vector>
#include <pangolin/platform.h>
#include <pangolin/var/varvalue.h>
#include <pangolin/utils/file_utils.h>
namespace pangolin
{
typedef void (*NewVarCallbackFn)(void* data, const std::string& name, VarValueGeneric& var, bool brand_new);
typedef void (*GuiVarChangedCallbackFn)(void* data, const std::string& name, VarValueGeneric& var);
struct PANGOLIN_EXPORT NewVarCallback
{
NewVarCallback(const std::string& filter, NewVarCallbackFn fn, void* data)
:filter(filter),fn(fn),data(data) {}
std::string filter;
NewVarCallbackFn fn;
void* data;
};
struct PANGOLIN_EXPORT GuiVarChangedCallback
{
GuiVarChangedCallback(const std::string& filter, GuiVarChangedCallbackFn fn, void* data)
:filter(filter),fn(fn),data(data) {}
std::string filter;
GuiVarChangedCallbackFn fn;
void* data;
};
class PANGOLIN_EXPORT VarState
{
public:
static VarState& I();
VarState();
~VarState();
void Clear();
template<typename T>
void NotifyNewVar(const std::string& name, VarValue<T>& var )
{
var_adds.push_back(name);
// notify those watching new variables
for(std::vector<NewVarCallback>::iterator invc = new_var_callbacks.begin(); invc != new_var_callbacks.end(); ++invc) {
if( StartsWith(name,invc->filter) ) {
invc->fn( invc->data, name, var, true);
}
}
}
VarValueGeneric*& operator[](const std::string& str)
{
VarStoreContainer::iterator it = vars.find(str);
if (it == vars.end()) {
vars[str] = nullptr;
}
return vars[str];
}
bool Exists(const std::string& str) const
{
return vars.find(str) != vars.end();
}
void FlagVarChanged()
{
varHasChanged = true;
}
bool VarHasChanged()
{
const bool has_changed = varHasChanged;
varHasChanged = false;
return has_changed;
}
//protected:
typedef std::map<std::string, VarValueGeneric*> VarStoreContainer;
typedef std::vector<std::string> VarStoreAdditions;
VarStoreContainer vars;
VarStoreAdditions var_adds;
std::vector<NewVarCallback> new_var_callbacks;
std::vector<GuiVarChangedCallback> gui_var_changed_callbacks;
bool varHasChanged;
};
inline bool GuiVarHasChanged() {
return VarState::I().VarHasChanged();
}
inline void FlagVarChanged() {
VarState::I().FlagVarChanged();
}
}

View File

@@ -0,0 +1,114 @@
/* This file is part of the Pangolin Project.
* http://github.com/stevenlovegrove/Pangolin
*
* Copyright (c) 2014 Steven Lovegrove
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <pangolin/var/varvaluet.h>
#include <pangolin/var/varwrapper.h>
namespace pangolin
{
template<typename T>
class VarValue : public VarValueT<typename std::remove_reference<T>::type>
{
public:
typedef typename std::remove_reference<T>::type VarT;
~VarValue()
{
delete str_ptr;
}
VarValue()
{
Init();
}
VarValue(const T& value)
: value(value), default_value(value)
{
Init();
}
VarValue(const T& value, const VarT& default_value)
: value(value), default_value(default_value)
{
Init();
}
const char* TypeId() const
{
return typeid(VarT).name();
}
void Reset()
{
value = default_value;
}
VarMeta& Meta()
{
return meta;
}
const VarT& Get() const
{
return value;
}
VarT& Get()
{
return value;
}
void Set(const VarT& val)
{
value = val;
}
protected:
void Init()
{
if(std::is_same<VarT,std::string>::value) {
str_ptr = 0;
this->str = (VarValueT<std::string>*)this;
}else{
str_ptr = new VarWrapper<std::string,VarT>(*this);
this->str = str_ptr;
}
}
// If non-zero, this class owns this str pointer in the base-class.
VarValueT<std::string>* str_ptr;
T value;
VarT default_value;
VarMeta meta;
};
}

View File

@@ -0,0 +1,87 @@
/* This file is part of the Pangolin Project.
* http://github.com/stevenlovegrove/Pangolin
*
* Copyright (c) 2014 Steven Lovegrove
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <string>
namespace pangolin
{
constexpr int META_FLAG_NONE = 0x0000;
constexpr int META_FLAG_TOGGLE = 0x0001;
constexpr int META_FLAG_READONLY = 0x0002;
struct VarMeta
{
VarMeta() :
increment(0.),
flags(META_FLAG_NONE),
gui_changed(false),
logscale(false),
generic(false)
{
range[0] = 0.;
range[1] = 0.;
}
std::string full_name;
std::string friendly;
double range[2];
double increment;
int flags;
bool gui_changed;
bool logscale;
bool generic;
};
// Forward declaration
template<typename T>
class VarValueT;
//! Abstract base class for named Pangolin variables
class VarValueGeneric
{
public:
VarValueGeneric()
: str(0)
{
}
virtual ~VarValueGeneric()
{
}
virtual const char* TypeId() const = 0;
virtual void Reset() = 0;
virtual VarMeta& Meta() = 0;
//protected:
// String serialisation object.
VarValueT<std::string>* str;
};
}

View File

@@ -0,0 +1,46 @@
/* This file is part of the Pangolin Project.
* http://github.com/stevenlovegrove/Pangolin
*
* Copyright (c) 2014 Steven Lovegrove
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <pangolin/var/varvaluegeneric.h>
#include <pangolin/compat/type_traits.h>
namespace pangolin
{
template<typename T>
class VarValueT : public VarValueGeneric
{
public:
typedef typename std::remove_reference<T>::type VarT;
virtual const VarT& Get() const = 0;
virtual void Set(const VarT& val) = 0;
};
}

View File

@@ -0,0 +1,91 @@
/* This file is part of the Pangolin Project.
* http://github.com/stevenlovegrove/Pangolin
*
* Copyright (c) 2014 Steven Lovegrove
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <pangolin/var/varvaluegeneric.h>
#include <pangolin/compat/type_traits.h>
#include <pangolin/utils/type_convert.h>
namespace pangolin
{
template<typename T, typename S>
class VarWrapper : public VarValueT<T>
{
public:
typedef typename std::remove_reference<S>::type VarS;
VarWrapper(VarValueT<S>& src)
: src(src)
{
this->str = src.str;
}
const char* TypeId() const
{
return typeid(T).name();
}
void Reset()
{
src.Reset();
// If reset throws, it will go up to the user, since there is nothing
// more we can do
cache = Convert<T,VarS>::Do(src.Get());
}
VarMeta& Meta()
{
return src.Meta();
}
const T& Get() const
{
// This might throw, but we can't reset because this is a const method
cache = Convert<T,VarS>::Do(src.Get());
return cache;
}
void Set(const T& val)
{
cache = val;
try {
src.Set( Convert<VarS, T>::Do(val) );
}catch(const BadInputException&) {
pango_print_warn("Unable to set variable with type %s from %s. Resetting.", typeid(VarS).name(), typeid(T).name() );
Reset();
}
}
protected:
mutable T cache;
VarValueT<S>& src;
};
}