Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r48790 - sandbox/libs/extension/examples/value
From: oryol_at_[hidden]
Date: 2008-09-15 15:26:08


Author: jeremypack
Date: 2008-09-15 15:26:07 EDT (Mon, 15 Sep 2008)
New Revision: 48790
URL: http://svn.boost.org/trac/boost/changeset/48790

Log:
Examples of writing Adapters for factories of objects that don't use inheritance.

Added:
   sandbox/libs/extension/examples/value/
   sandbox/libs/extension/examples/value/Jamfile.v2 (contents, props changed)
   sandbox/libs/extension/examples/value/shared.hpp (contents, props changed)
   sandbox/libs/extension/examples/value/value.cpp (contents, props changed)

Added: sandbox/libs/extension/examples/value/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/value/Jamfile.v2 2008-09-15 15:26:07 EDT (Mon, 15 Sep 2008)
@@ -0,0 +1 @@
+exe value : value.cpp ;
\ No newline at end of file

Added: sandbox/libs/extension/examples/value/shared.hpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/value/shared.hpp 2008-09-15 15:26:07 EDT (Mon, 15 Sep 2008)
@@ -0,0 +1,43 @@
+/*
+ * Boost.Extension
+ *
+ * (C) Copyright Jeremy Pack 2008
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#ifndef BOOST_EXTENSION_SHARED_HPP
+#define BOOST_EXTENSION_SHARED_HPP
+
+#include <boost/scoped_ptr.hpp>
+
+class generic_animal_impl {
+ public:
+ virtual ~generic_animal_impl() {}
+ virtual const std::string& get_name() const = 0;
+};
+
+template <class T>
+class animal_impl : public generic_animal_impl {
+ public:
+ animal_impl() : impl_(new T()) {}
+ virtual const std::string& get_name() const {
+ return impl_->get_name();
+ }
+ boost::scoped_ptr<T> impl_;
+};
+
+class any_animal {
+ public:
+ explicit any_animal(generic_animal_impl* a) : impl_(a) {
+ }
+ const std::string& get_name() const {
+ return impl_->get_name();
+ }
+ boost::scoped_ptr<generic_animal_impl> impl_;
+};
+
+#endif // BOOST_EXTENSION_SHARED_HPP

Added: sandbox/libs/extension/examples/value/value.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/value/value.cpp 2008-09-15 15:26:07 EDT (Mon, 15 Sep 2008)
@@ -0,0 +1,34 @@
+/*
+ * Boost.Extension
+ *
+ * (C) Copyright Jeremy Pack 2008
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#include <iostream>
+
+#include <boost/extension/factory.hpp>
+
+#include "shared.hpp"
+
+class tiger {
+ public:
+ tiger () : name_("A ferocious tiger") {}
+ const std::string& get_name() const {
+ return name_;
+ }
+ private:
+ std::string name_;
+};
+
+int main() {
+ using namespace boost::extensions;
+ factory<generic_animal_impl> f;
+ f.set<animal_impl<tiger> >();
+ any_animal animal(f.create());
+ std::cout << animal.get_name() << std::endl;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk