summaryrefslogtreecommitdiff
path: root/ucoo
diff options
context:
space:
mode:
authorNicolas Schodet2015-08-14 15:23:30 +0200
committerNicolas Schodet2019-10-07 00:44:50 +0200
commit7b6ad41ea67e10ce9849b5a7ae4411b272c3d43c (patch)
tree586797fc928e3b8ac57e459f73e35139f4b62a69 /ucoo
parent46344a8589a8d33223b9727d1770adc5706408c8 (diff)
ucoo/utils: add Function
Diffstat (limited to 'ucoo')
-rw-r--r--ucoo/utils/function.hh139
-rw-r--r--ucoo/utils/test/Makefile3
-rw-r--r--ucoo/utils/test/test_function.cc123
3 files changed, 264 insertions, 1 deletions
diff --git a/ucoo/utils/function.hh b/ucoo/utils/function.hh
new file mode 100644
index 0000000..d8bbefd
--- /dev/null
+++ b/ucoo/utils/function.hh
@@ -0,0 +1,139 @@
+#ifndef ucoo_utils_function_hh
+#define ucoo_utils_function_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2015 Nicolas Schodet
+//
+// 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.
+//
+// }}}
+#include "ucoo/common.hh"
+
+namespace ucoo {
+
+/// Type used to compute maximum size of a FunctionStore.
+union FunctionStoreTypes
+{
+ /// A FunctionStore can store a function pointer...
+ template<typename Res, typename ...Args>
+ using Function = Res (*) (Args...);
+ Function<void> function;
+ /// ...or a bound member function (object pointer + member function
+ /// pointer).
+ template<typename T, typename Res, typename ...Args>
+ struct BoundMember
+ {
+ T *object;
+ Res (T::*member_pointer) (Args...);
+ };
+ class SomeClass;
+ BoundMember<SomeClass, void> bound_member;
+};
+
+/// Store what is needed to call a function.
+union FunctionStore
+{
+ void *access () { return data_; }
+ const void *access () const { return data_; }
+ template<typename T>
+ T &access ()
+ {
+ static_assert (sizeof (T) <= sizeof (data_),
+ "function pointer too big");
+ return *reinterpret_cast<T *> (access ());
+ }
+ template<typename T>
+ const T &access () const
+ {
+ static_assert (sizeof (T) <= sizeof (data_),
+ "function pointer too big");
+ return *reinterpret_cast<const T *> (access ());
+ }
+ private:
+ FunctionStoreTypes unused_;
+ char data_[sizeof (FunctionStoreTypes)];
+};
+
+template<typename Signature>
+class Function;
+
+/// Store a reference to a callable element in an object which type only
+/// depends on the callable signature.
+///
+/// This is a lightweight and limited version of std::function.
+template<typename Res, typename ...Args>
+class Function<Res (Args...)>
+{
+ public:
+ /// Construct an empty function, can not be called.
+ Function () : call_ (nullptr) { }
+ /// Construct from a function pointer.
+ Function (Res (*function_pointer) (Args...))
+ {
+ assert (function_pointer);
+ using F = FunctionStoreTypes::Function<Res, Args...>;
+ F &f = store_.access<F> ();
+ f = function_pointer;
+ call_ = &Function::call_function<F>;
+ }
+ /// Construct with a bound member function.
+ template<typename T>
+ Function (T *object, Res (T::*member_pointer) (Args...))
+ {
+ assert (object && member_pointer);
+ using F = FunctionStoreTypes::BoundMember<T, Res, Args...>;
+ F &f = store_.access<F> ();
+ f.object = object;
+ f.member_pointer = member_pointer;
+ call_ = &Function::call_bound_member<F>;
+ }
+ /// Reset to empty function.
+ void reset () { call_ = nullptr; }
+ /// Call the stored function.
+ Res operator() (Args... args) const
+ {
+ assert (call_);
+ return call_ (args..., store_);
+ }
+ /// Test whether there is a stored function.
+ operator bool () const
+ {
+ return call_;
+ }
+ private:
+ template<typename F>
+ static Res call_function (Args... args, const FunctionStore &store)
+ {
+ const F &f = store.access<F> ();
+ return f (args...);
+ }
+ template<typename F>
+ static Res call_bound_member (Args... args, const FunctionStore &store)
+ {
+ const F &f = store.access<F> ();
+ return (f.object->*f.member_pointer) (args...);
+ }
+ private:
+ Res (*call_) (Args..., const FunctionStore &store);
+ FunctionStore store_;
+};
+
+} // namespace ucoo
+
+#endif // ucoo_utils_function_hh
diff --git a/ucoo/utils/test/Makefile b/ucoo/utils/test/Makefile
index e490582..e1eb3b2 100644
--- a/ucoo/utils/test/Makefile
+++ b/ucoo/utils/test/Makefile
@@ -1,11 +1,12 @@
BASE = ../../..
TARGETS = host stm32f4
-PROGS = test_fifo test_crc
+PROGS = test_fifo test_crc test_function
stm32f4_PROGS = test_delay
test_fifo_SOURCES = test_fifo.cc
test_delay_SOURCES = test_delay.cc
test_crc_SOURCES = test_crc.cc
+test_function_SOURCES = test_function.cc
MODULES = ucoo/utils ucoo/base/test ucoo/hal/usb
diff --git a/ucoo/utils/test/test_function.cc b/ucoo/utils/test/test_function.cc
new file mode 100644
index 0000000..91f0246
--- /dev/null
+++ b/ucoo/utils/test/test_function.cc
@@ -0,0 +1,123 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2015 Nicolas Schodet
+//
+// 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.
+//
+// }}}
+#include "ucoo/utils/function.hh"
+#include "ucoo/arch/arch.hh"
+#include "ucoo/base/test/test.hh"
+
+int
+sum (int a, int b)
+{
+ return a + b;
+}
+
+struct Accum
+{
+ int cur;
+ Accum (int base) : cur (base) { }
+ void acc (int val) { cur += val; }
+};
+
+struct AbstractReduce
+{
+ virtual void acc (int val) = 0;
+};
+
+struct ConcreteAccum : public AbstractReduce
+{
+ int cur;
+ ConcreteAccum (int base) : cur (base) { }
+ void acc (int val) override { cur += val; }
+};
+
+bool __attribute__ ((noinline))
+test_function (const ucoo::Function<int (int, int)> &f)
+{
+ return f (1, 2) == 3;
+}
+
+bool __attribute__ ((noinline))
+test_function (const ucoo::Function<void (int)> &f, int &cur)
+{
+ f (2);
+ return cur == 3;
+}
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::TestSuite tsuite ("function");
+ {
+ ucoo::Test test (tsuite, "function pointer");
+ ucoo::Function<int (int, int)> f (sum);
+ if (!test_function (f))
+ test.fail ();
+ }
+ {
+ ucoo::Test test (tsuite, "bound member");
+ Accum a (1);
+ ucoo::Function<void (int)> f (&a, &Accum::acc);
+ if (!test_function (f, a.cur))
+ test.fail ();
+ }
+ {
+ ucoo::Test test (tsuite, "bound virtual member");
+ ConcreteAccum a (1);
+ AbstractReduce *aa = &a;
+ ucoo::Function<void (int)> f (aa, &AbstractReduce::acc);
+ if (!test_function (f, a.cur))
+ test.fail ();
+ }
+ {
+ ucoo::Test test (tsuite, "copy");
+ ucoo::Function<int (int, int)> f (sum);
+ ucoo::Function<int (int, int)> f2 (f);
+ if (!test_function (f2))
+ test.fail ();
+ }
+ {
+ ucoo::Test test (tsuite, "assignment");
+ ucoo::Function<int (int, int)> f (sum);
+ ucoo::Function<int (int, int)> f2;
+ f2 = f;
+ if (!test_function (f2))
+ test.fail ();
+ }
+ {
+ ucoo::Test test (tsuite, "function assignment");
+ ucoo::Function<int (int, int)> f;
+ f = sum;
+ if (!f || !test_function (f))
+ test.fail ();
+ }
+ {
+ ucoo::Test test (tsuite, "nullptr assignment");
+ ucoo::Function<int (int, int)> f (sum);
+ f.reset ();
+ if (f)
+ test.fail ();
+ }
+ return tsuite.report () ? 0 : 1;
+}
+