From f53559b8d0167f61466e6d1c5b57030e0d73927c Mon Sep 17 00:00:00 2001 From: James Turner Date: Sat, 6 Nov 2010 14:39:52 +0000 Subject: [PATCH] Initial propertyObject work. --- simgear/props/Makefile.am | 6 +- simgear/props/propertyObject.cxx | 91 ++++++++++++++++ simgear/props/propertyObject.hxx | 173 +++++++++++++++++++++++++++++++ 3 files changed, 268 insertions(+), 2 deletions(-) create mode 100644 simgear/props/propertyObject.cxx create mode 100644 simgear/props/propertyObject.hxx diff --git a/simgear/props/Makefile.am b/simgear/props/Makefile.am index 658fe126..c39bd0a0 100644 --- a/simgear/props/Makefile.am +++ b/simgear/props/Makefile.am @@ -7,13 +7,15 @@ include_HEADERS = \ props.hxx \ props_io.hxx \ AtomicChangeListener.hxx \ - ExtendedPropertyAdapter.hxx + ExtendedPropertyAdapter.hxx \ + propertyObject.hxx libsgprops_a_SOURCES = \ condition.cxx \ props.cxx \ props_io.cxx \ - AtomicChangeListener.cxx + AtomicChangeListener.cxx \ + propertyObject.cxx check_PROGRAMS = props_test diff --git a/simgear/props/propertyObject.cxx b/simgear/props/propertyObject.cxx new file mode 100644 index 00000000..ff90ce77 --- /dev/null +++ b/simgear/props/propertyObject.cxx @@ -0,0 +1,91 @@ +// Copyright (C) 2010 James Turner - zakalawe@mac.com +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#ifdef HAVE_CONFIG_H +# include "simgear_config.h" +#endif + +#include "propertyObject.hxx" + +namespace simgear +{ + +SGPropertyNode* static_defaultRoot = NULL; + +void PropertyObjectBase::setDefaultRoot(SGPropertyNode* aRoot) +{ + static_defaultRoot = aRoot; +} + +PropertyObjectBase::PropertyObjectBase(const char* aChild) : + _base(NULL), + _path(aChild), + _prop(NULL) +{ +} + +PropertyObjectBase::PropertyObjectBase(SGPropertyNode* aNode, const char* aChild) : + _base(aNode), + _path(aChild), + _prop(NULL) +{ + +} + +SGPropertyNode* PropertyObjectBase::node(bool aCreate) const +{ + if (_prop) { + return _prop; + } + + _prop = _base ? _base : static_defaultRoot; + if (_path) { + _prop = _prop->getNode(_path, aCreate); + } + + return _prop; +} + +} // of namespace simgear + +void test() +{ + SGPropObjDouble foo("/bar/foo"); + + SGPropertyNode* zoob; + + SGPropObjDouble foo2 = SGPropObjDouble::create(zoob, "foo2", 42.0); + + foo = 1123.0; + foo2 = 43; + + std::string s("lalala"); + + foo = "lalal"; + + + SGPropObjString sp(zoob); + sp = "fooo"; + s = sp; + + + SGPropObjBool bp("/some nice big path"); + bp = false; + + bp = 456; + int i5 = bp; +} diff --git a/simgear/props/propertyObject.hxx b/simgear/props/propertyObject.hxx new file mode 100644 index 00000000..0067cb5e --- /dev/null +++ b/simgear/props/propertyObject.hxx @@ -0,0 +1,173 @@ +// Copyright (C) 2010 James Turner - zakalawe@mac.com +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#ifndef SG_PROPERTY_OBJECT +#define SG_PROPERTY_OBJECT + +#include +#include + +namespace simgear +{ + +class PropertyObjectBase +{ +public: + static void setDefaultRoot(SGPropertyNode* aRoot); + + PropertyObjectBase(const char* aChild); + + PropertyObjectBase(SGPropertyNode* aNode, const char* aChild = NULL); + + SGPropertyNode* node(bool aCreate) const; + +protected: + SGPropertyNode* _base; + const char* _path; + mutable SGPropertyNode* _prop; +}; + +template +class PropertyObject : PropertyObjectBase +{ +public: + /** + * Create from path relative to the default root, and option default value + */ + PropertyObject(const char* aChild) : + PropertyObjectBase(aChild) + { } + + /** + * Create from a node, with optional relative path + */ + PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) : + PropertyObjectBase(aNode, aChild) + { + + } + +// create() form creates the property immediately + static PropertyObject create(const char* aPath, T aValue) + { + PropertyObject p(aPath); + p = aValue; + return p; + } + + static PropertyObject create(SGPropertyNode* aNode, T aValue) + { + PropertyObject p(aNode); + p = aValue; + return p; + } + + static PropertyObject create(SGPropertyNode* aNode, const char* aChild, T aValue) + { + PropertyObject p(aNode, aChild); + p = aValue; + return p; + } + +// conversion operators + operator T () const + { + SGPropertyNode* n = node(false); + if (!n) { + throw sg_exception("read of undefined property:", _path); + } + + return n->getValue(); + } + + T operator=(const T& aValue) + { + SGPropertyNode* n = node(true); + if (!n) { + return aValue; + } + + n->setValue(aValue); + return aValue; + } + +}; // of template PropertyObject + + +// specialization for const char* / std::string + +template <> +class PropertyObject : PropertyObjectBase +{ +public: + PropertyObject(const char* aChild) : + PropertyObjectBase(aChild) + { } + + + + PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) : + PropertyObjectBase(aNode, aChild) + { + + } + + + + operator std::string () const + { + SGPropertyNode* n = node(false); + if (!n) { + throw sg_exception("read of undefined property:", _path); + } + + return n->getStringValue(); + } + + const char* operator=(const char* aValue) + { + SGPropertyNode* n = node(true); + if (!n) { + return aValue; + } + + n->setStringValue(aValue); + return aValue; + } + + std::string operator=(const std::string& aValue) + { + SGPropertyNode* n = node(true); + if (!n) { + return aValue; + } + + n->setStringValue(aValue); + return aValue; + } + +private: +}; + +} // of namespace simgear + +typedef simgear::PropertyObject SGPropObjDouble; +typedef simgear::PropertyObject SGPropObjBool; +typedef simgear::PropertyObject SGPropObjString; +typedef simgear::PropertyObject SGPropObjInt; + +#endif -- 2.39.5