diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-24 17:43:19 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-24 17:43:19 +0000 |
commit | 0292059f4a16434600564cfa3f0ad2309a508a54 (patch) | |
tree | d95953cd53011917c4df679b96aedca39401b54f /doc/sources/c++ | |
download | libksquirrel-0292059f4a16434600564cfa3f0ad2309a508a54.tar.gz libksquirrel-0292059f4a16434600564cfa3f0ad2309a508a54.zip |
Added libksquirrel for KDE3
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/libraries/libksquirrel@1095624 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'doc/sources/c++')
-rw-r--r-- | doc/sources/c++/README | 3 | ||||
-rwxr-xr-x | doc/sources/c++/compile | 5 | ||||
-rw-r--r-- | doc/sources/c++/main.cpp | 44 | ||||
-rw-r--r-- | doc/sources/c++/polygon.hpp | 25 | ||||
-rw-r--r-- | doc/sources/c++/triangle.cpp | 22 |
5 files changed, 99 insertions, 0 deletions
diff --git a/doc/sources/c++/README b/doc/sources/c++/README new file mode 100644 index 0000000..189ebab --- /dev/null +++ b/doc/sources/c++/README @@ -0,0 +1,3 @@ +Originally written by Aaron Isotton. +Look for article "C++ dlopen mini HOWTO" in internet or +visit http://ksquirrel.sf.net/ksquirrel-libs-olibs2.php
\ No newline at end of file diff --git a/doc/sources/c++/compile b/doc/sources/c++/compile new file mode 100755 index 0000000..64687fa --- /dev/null +++ b/doc/sources/c++/compile @@ -0,0 +1,5 @@ +#!/bin/sh + +g++ -O2 -Wall -fPIC -c triangle.cpp +g++ -shared -o triangle.so triangle.o +g++ -o test main.cpp -ldl
\ No newline at end of file diff --git a/doc/sources/c++/main.cpp b/doc/sources/c++/main.cpp new file mode 100644 index 0000000..b8fb8cd --- /dev/null +++ b/doc/sources/c++/main.cpp @@ -0,0 +1,44 @@ +#include "polygon.hpp" + +#include <iostream> +#include <dlfcn.h> + +int main() +{ + using std::cout; + using std::cerr; + + // load the triangle library + void* triangle = dlopen("./triangle.so", RTLD_LAZY); + + if (!triangle) + { + cerr << "Cannot load library: " << dlerror() << '\n'; + return 1; + } + + // load the symbols + create_t* create_triangle = (create_t*) dlsym(triangle, "create"); + destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy"); + + if (!create_triangle || !destroy_triangle) + { + cerr << "Cannot load symbols: " << dlerror() << '\n'; + return 1; + } + + // create an instance of the class + polygon* poly = create_triangle(); + + // use the class + poly->set_side_length(7); + cout << "The area is: " << poly->area() << '\n'; + + // destroy the class + destroy_triangle(poly); + + // unload the triangle library + dlclose(triangle); + + return 0; +} diff --git a/doc/sources/c++/polygon.hpp b/doc/sources/c++/polygon.hpp new file mode 100644 index 0000000..e3189bf --- /dev/null +++ b/doc/sources/c++/polygon.hpp @@ -0,0 +1,25 @@ +#ifndef POLYGON_HPP +#define POLYGON_HPP + +class polygon +{ + protected: + double side_length_; + + public: + polygon() : side_length_(0) + {} + + void set_side_length(double side_length) + { + side_length_ = side_length; + } + + virtual double area() const = 0; +}; + +// the types of the class factories +typedef polygon* create_t(); +typedef void destroy_t(polygon*); + +#endif diff --git a/doc/sources/c++/triangle.cpp b/doc/sources/c++/triangle.cpp new file mode 100644 index 0000000..96185d0 --- /dev/null +++ b/doc/sources/c++/triangle.cpp @@ -0,0 +1,22 @@ +#include "polygon.hpp" +#include <cmath> + +class triangle : public polygon +{ + public: + virtual double area() const + { + return side_length_ * side_length_ * sqrt(3) / 2; + } +}; + +// the class factories +extern "C" polygon* create() +{ + return new triangle; +} + +extern "C" void destroy(polygon* p) +{ + delete p; +} |