[cpp-sp] 02/02: Initial Boost-backed PropertySet.
Scott Cantor
cantor.2 at osu.edu
Fri Nov 15 20:46:01 UTC 2024
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch main
in repository cpp-sp.
View the commit online:
http://git.shibboleth.net/view/?p=cpp-sp.git;a=commit;h=8cd8b76cc4f2ce75e916869748753d6ccf8271cd
commit 8cd8b76cc4f2ce75e916869748753d6ccf8271cd
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Fri Nov 15 15:45:45 2024 -0500
Initial Boost-backed PropertySet.
---
shibsp/Makefile.am | 2 +
shibsp/util/BoostPropertySet.cpp | 154 +++++++++++++++++++++++++++++++++++++++
shibsp/util/BoostPropertySet.h | 64 ++++++++++++++++
shibsp/util/DOMPropertySet.cpp | 8 --
shibsp/util/PropertySet.h | 72 +++++++++++++++++-
5 files changed, 290 insertions(+), 10 deletions(-)
diff --git a/shibsp/Makefile.am b/shibsp/Makefile.am
index d83bc41e..440f3e12 100644
--- a/shibsp/Makefile.am
+++ b/shibsp/Makefile.am
@@ -50,6 +50,7 @@ reminclude_HEADERS = \
remoting/ListenerService.h
utilinclude_HEADERS = \
+ util/BoostPropertySet.h \
util/CGIParser.h \
util/DOMPropertySet.h \
util/IPRange.h \
@@ -109,6 +110,7 @@ libshibsp_la_SOURCES = \
remoting/impl/SocketListener.cpp \
remoting/impl/TCPListener.cpp \
remoting/impl/UnixListener.cpp \
+ util/BoostPropertySet.cpp \
util/CGIParser.cpp \
util/DOMPropertySet.cpp \
util/IPRange.cpp \
diff --git a/shibsp/util/BoostPropertySet.cpp b/shibsp/util/BoostPropertySet.cpp
new file mode 100644
index 00000000..406e5553
--- /dev/null
+++ b/shibsp/util/BoostPropertySet.cpp
@@ -0,0 +1,154 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * BoostPropertySet.cpp
+ *
+ * DOM-based property set implementation.
+ */
+
+#include "internal.h"
+#include "util/BoostPropertySet.h"
+
+#include <algorithm>
+#include <boost/lexical_cast.hpp>
+#include <boost/algorithm/string.hpp>
+
+using namespace shibsp;
+using namespace boost;
+using namespace std;
+
+PropertySet::PropertySet()
+{
+}
+
+PropertySet::~PropertySet()
+{
+}
+
+BoostPropertySet::BoostPropertySet() : m_parent(nullptr)
+{
+}
+
+BoostPropertySet::~BoostPropertySet()
+{
+}
+
+const PropertySet2* BoostPropertySet::getParent() const
+{
+ return m_parent;
+}
+
+void BoostPropertySet::setParent(const PropertySet2* parent)
+{
+ m_parent = parent;
+}
+
+void BoostPropertySet::load(const property_tree::ptree& pt, const char* unsetter)
+{
+ m_pt = &pt;
+
+ // Check for unsetter, pull out and split.
+ if (unsetter) {
+ const optional<string> val = pt.get_optional<string>(unsetter);
+ if (val) {
+ split(m_unset, val.get(), is_space(), algorithm::token_compress_on);
+ }
+ }
+}
+
+bool BoostPropertySet::getBool(const char* name, bool defaultValue) const
+{
+ if (m_pt) {
+ // Check for a child node with the target name and return its value as a bool.
+ const optional<const property_tree::ptree&> child = m_pt->get_child_optional(name);
+ if (child) {
+ const string& val = child->data();
+ return val == "1" || val == "true";
+ }
+ }
+
+ // If we have a parent and the setting isn't "unset" at this layer, return its copy.
+ if (m_parent && m_unset.find(name) == m_unset.end()) {
+ return m_parent->getBool(name, defaultValue);
+ }
+
+ // Else return the default.
+ return defaultValue;
+}
+
+const char* BoostPropertySet::getString(const char* name, const char* defaultValue) const
+{
+ if (m_pt) {
+ // Check for a child node with the target name and return its value as a C string.
+ const optional<const property_tree::ptree&> child = m_pt->get_child_optional(name);
+ if (child) {
+ return child->data().c_str();
+ }
+ }
+
+ // If we have a parent and the setting isn't "unset" at this layer, return its copy.
+ if (m_parent && m_unset.find(name) == m_unset.end()) {
+ return m_parent->getString(name, defaultValue);
+ }
+
+ // Else return the default.
+ return defaultValue;
+}
+
+unsigned int BoostPropertySet::getUnsignedInt(const char* name, unsigned int defaultValue) const
+{
+ if (m_pt) {
+ // Check for a child node with the target name and return its value as a C string.
+ const optional<const property_tree::ptree&> child = m_pt->get_child_optional(name);
+ if (child) {
+ try {
+ return lexical_cast<unsigned int>(child->data());
+ }
+ catch (const bad_lexical_cast&) {
+ }
+ }
+ }
+
+ // If we have a parent and the setting isn't "unset" at this layer, return its copy.
+ if (m_parent && m_unset.find(name) == m_unset.end()) {
+ return m_parent->getUnsignedInt(name, defaultValue);
+ }
+
+ // Else return the default.
+ return defaultValue;
+}
+
+int BoostPropertySet::getInt(const char* name, int defaultValue) const
+{
+ if (m_pt) {
+ // Check for a child node with the target name and return its value as a C string.
+ const optional<const property_tree::ptree&> child = m_pt->get_child_optional(name);
+ if (child) {
+ try {
+ return lexical_cast<int>(child->data());
+ }
+ catch (const bad_lexical_cast&) {
+ }
+ }
+ }
+
+ // If we have a parent and the setting isn't "unset" at this layer, return its copy.
+ if (m_parent && m_unset.find(name) == m_unset.end()) {
+ return m_parent->getInt(name, defaultValue);
+ }
+
+ // Else return the default.
+ return defaultValue;
+}
diff --git a/shibsp/util/BoostPropertySet.h b/shibsp/util/BoostPropertySet.h
new file mode 100644
index 00000000..ed1bb5fa
--- /dev/null
+++ b/shibsp/util/BoostPropertySet.h
@@ -0,0 +1,64 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file shibsp/util/DOMPropertySet.h
+ *
+ * DOM-based property set implementation.
+ */
+
+#ifndef __shibsp_boostpropset_h__
+#define __shibsp_boostpropset_h__
+
+#include <shibsp/util/PropertySet.h>
+
+#include <set>
+#include <boost/property_tree/ptree.hpp>
+
+namespace shibsp {
+
+ /**
+ * Boost property tree-based property set implementation.
+ */
+ class SHIBSP_API BoostPropertySet : public virtual PropertySet2
+ {
+ public:
+ BoostPropertySet();
+
+ virtual ~BoostPropertySet();
+
+ const PropertySet2* getParent() const;
+ void setParent(const PropertySet2* parent);
+ bool getBool(const char* name, bool defaultValue) const;
+ const char* getString(const char* name, const char* defaultValue) const;
+ unsigned int getUnsignedInt(const char* name, unsigned int defaultValue) const;
+ int getInt(const char* name, int defaultValue) const;
+
+ /**
+ * Loads the property set from a ptree owned and managed by the caller.
+ *
+ * @param pt property tree instance to wrap
+ * @param unsetter optional name of a property containing a list of property names to "unset"
+ */
+ void load(const boost::property_tree::ptree& pt, const char* unsetter=nullptr);
+
+ private:
+ const PropertySet2* m_parent;
+ const boost::property_tree::ptree* m_pt;
+ std::set<std::string> m_unset;
+ };
+
+};
+
+#endif /* __shibsp_boostpropset_h__ */
diff --git a/shibsp/util/DOMPropertySet.cpp b/shibsp/util/DOMPropertySet.cpp
index 6f29d7a6..08e376bc 100644
--- a/shibsp/util/DOMPropertySet.cpp
+++ b/shibsp/util/DOMPropertySet.cpp
@@ -41,14 +41,6 @@ using namespace xercesc;
using namespace boost;
using namespace std;
-PropertySet::PropertySet()
-{
-}
-
-PropertySet::~PropertySet()
-{
-}
-
DOMPropertySet::Remapper::Remapper()
{
}
diff --git a/shibsp/util/PropertySet.h b/shibsp/util/PropertySet.h
index 4647c76f..e321cff2 100644
--- a/shibsp/util/PropertySet.h
+++ b/shibsp/util/PropertySet.h
@@ -1,4 +1,4 @@
-/*
+/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -21,12 +21,14 @@
#ifndef __shibsp_propset_h__
#define __shibsp_propset_h__
+#include <shibsp/base.h>
+
#include <utility>
namespace shibsp {
/**
- * Interface to a generic set of typed properties or a DOM container of additional data.
+ * Interface to a generic set of typed properties.
*/
class SHIBSP_API PropertySet
{
@@ -95,6 +97,72 @@ namespace shibsp {
*/
virtual const PropertySet* getPropertySet(const char* name) const=0;
};
+
+ /**
+ * Interface to a generic set of typed properties.
+ *
+ * TODO: This will replace the original interface and be renamed back to PropertySet
+ * once code migration is completed.
+ */
+ class SHIBSP_API PropertySet2
+ {
+ MAKE_NONCOPYABLE(PropertySet2);
+ protected:
+ PropertySet2();
+ public:
+ virtual ~PropertySet2();
+
+ /**
+ * Returns parent of this PropertySet, if any.
+ *
+ * @return the parent object, or nullptr
+ */
+ virtual const PropertySet2* getParent() const=0;
+
+ /**
+ * Establishes a "parent" PropertySet to supply inherited settings.
+ *
+ * @param parent the parent PropertySet to use
+ */
+ virtual void setParent(const PropertySet2* parent)=0;
+
+ /**
+ * Returns a boolean-valued property.
+ *
+ * @param name property name
+ * @param defaultValue default value if property is unset
+ * @return effective property value
+ */
+ virtual bool getBool(const char* name, bool defaultValue) const=0;
+
+ /**
+ * Returns a string-valued property.
+ *
+ * @param name property name
+ * @param defaultValue default value if property is unset
+ * @return effective property value
+ */
+ virtual const char* getString(const char* name, const char* defaultValue=nullptr) const=0;
+
+ /**
+ * Returns an unsigned integer-valued property.
+ *
+ * @param name property name
+ * @param defaultValue default value if property is unset
+ * @return effective property value
+ */
+ virtual unsigned int getUnsignedInt(const char* name, unsigned int defaultValue) const=0;
+
+ /**
+ * Returns an integer-valued property.
+ *
+ * @param name property name
+ * @param defaultValue default value if property is unset
+ * @return effective property value
+ */
+ virtual int getInt(const char* name, int defaultValue) const=0;
+ };
+
};
#endif /* __shibsp_propset_h__ */
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list