function0.h 7.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/*
 * Copyright (c) 2014, Siemens AG. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_BASE_INTERNAL_FUNCTION0_H_
#define EMBB_BASE_INTERNAL_FUNCTION0_H_

#include <new>
#include <cstddef>

#include <embb/base/internal/nil.h>
#include <embb/base/memory_allocation.h>
#include <embb/base/atomic.h>
#include <embb/base/internal/functionT.h>

namespace embb {
namespace base {

namespace internal {

template <typename R>
class Function0 {
 public:
  virtual ~Function0() {}
  virtual R operator () () = 0;
  virtual void CopyTo(void* dst) = 0;
};

template <typename R>
class FunctionPointer0
  : public Function0<R> {
 public:
  typedef R(*FuncPtrType)();
  explicit FunctionPointer0(FuncPtrType func) : function_(func) {}
  virtual R operator () () {
    return function_();
  }
  virtual void CopyTo(void* dst) {
    new(dst)FunctionPointer0(function_);
  }

 private:
  FuncPtrType function_;
};

template <>
class FunctionPointer0<void>
  : public Function0<void> {
 public:
  typedef void(*FuncPtrType)();
  explicit FunctionPointer0(FuncPtrType func) : function_(func) {}
  virtual void operator () () {
    function_();
  }
  virtual void CopyTo(void* dst) {
    new(dst)FunctionPointer0(function_);
  }

 private:
  FuncPtrType function_;
};

template <class C, typename R>
class MemberFunctionPointer0
  : public Function0<R> {
 public:
  typedef R(C::*MemFuncPtrType)();
  typedef C & ClassRefType;
  MemberFunctionPointer0(ClassRefType obj, MemFuncPtrType func)
  : object_(obj), function_(func) {}
  explicit MemberFunctionPointer0(ClassRefType obj)
  : object_(obj), function_(&C::operator()) {}
  void operator = (MemberFunctionPointer0 const & memfunc) {
    object_ = memfunc.object_;
    function_ = memfunc.function_;
  }
  virtual R operator () () {
    return (object_.*function_)();
  }
  virtual void CopyTo(void* dst) {
    new(dst)MemberFunctionPointer0(object_, function_);
  }

 private:
  ClassRefType object_;
  MemFuncPtrType function_;
};

template <class C>
class MemberFunctionPointer0<C, void>
  : public Function0<void> {
 public:
  typedef void(C::*MemFuncPtrType)();
  typedef C & ClassRefType;
  MemberFunctionPointer0(ClassRefType obj, MemFuncPtrType func)
  : object_(obj), function_(func) {}
  explicit MemberFunctionPointer0(ClassRefType obj)
  : object_(obj), function_(&C::operator()) {}
  void operator = (MemberFunctionPointer0 const & memfunc) {
    object_ = memfunc.object_;
    function_ = memfunc.function_;
  }
  virtual void operator () () {
    (object_.*function_)();
  }
  virtual void CopyTo(void* dst) {
    new(dst)MemberFunctionPointer0(object_, function_);
  }

 private:
  ClassRefType object_;
  MemFuncPtrType function_;
};

template <class C, typename R>
class FunctorWrapper0
  : public Function0<R> {
 public:
  FunctorWrapper0() : object_(NULL), ref_count_(NULL) {}
  explicit FunctorWrapper0(C const & obj) {
    object_ = Allocation::New<C>(obj);
    ref_count_ = Allocation::New<Atomic<int> >(1);
  }
  explicit FunctorWrapper0(FunctorWrapper0 const & other) {
    object_ = other.object_;
    ref_count_ = other.ref_count_;
    ++*ref_count_;
  }
  virtual ~FunctorWrapper0() {
    if (0 == --*ref_count_) {
      Allocation::Delete(ref_count_);
      Allocation::Delete(object_);
    }
  }
  virtual R operator () () {
    return (*object_)();
  }
  virtual void CopyTo(void* dst) {
    new(dst)FunctorWrapper0(*this);
  }

 private:
  C * object_;
  Atomic<int> * ref_count_;
};

template <class C>
class FunctorWrapper0<C, void>
  : public Function0<void> {
 public:
  FunctorWrapper0() : object_(NULL), ref_count_(NULL) {}
  explicit FunctorWrapper0(C const & obj) {
    object_ = Allocation::New<C>(obj);
    ref_count_ = Allocation::New<Atomic<int> >(1);
  }
  explicit FunctorWrapper0(FunctorWrapper0 const & other) {
    object_ = other.object_;
    ref_count_ = other.ref_count_;
    ++*ref_count_;
  }
  virtual ~FunctorWrapper0() {
    if (0 == --*ref_count_) {
      Allocation::Delete(ref_count_);
      Allocation::Delete(object_);
    }
  }
  virtual void operator () () {
    (*object_)();
  }
  virtual void CopyTo(void* dst) {
    new(dst)FunctorWrapper0(*this);
  }

 private:
  C * object_;
  Atomic<int> * ref_count_;
};

} // namespace internal


using embb::base::internal::Nil;

template <typename R>
class Function<R, Nil, Nil, Nil, Nil, Nil> {
 public:
  typedef internal::Function0<R> * FuncPtrType;
  Function() : function_(NULL) {}
  template <class C>
  explicit Function(C const & obj) {
    function_ = new(storage_)
      internal::FunctorWrapper0<C, R>(obj);
  }
  Function(Function const & func) {
    func.function_->CopyTo(&storage_[0]);
    function_ = reinterpret_cast<FuncPtrType>(&storage_[0]);
  }
  ~Function() {
    Free();
  }
  void operator = (R(*func)()) {
    Free();
    function_ = new(storage_)
      internal::FunctionPointer0<R>(func);
  }
  void operator = (Function & func) {
    Free();
    func.function_->CopyTo(&storage_[0]);
    function_ = reinterpret_cast<FuncPtrType>(&storage_[0]);
  }
  template <class C>
  void operator = (C const & obj) {
    Free();
    function_ = new(storage_)
      internal::FunctorWrapper0<C, R>(obj);
  }
  explicit Function(R(*func)()) {
    function_ = new(storage_)
      internal::FunctionPointer0<R>(func);
  }
  template <class C>
  Function(C & obj, R(C::*func)()) {
    function_ = new(storage_)
      internal::MemberFunctionPointer0<C, R>(obj, func);
  }
  R operator () () {
    return (*function_)();
  }

 private:
  char storage_[sizeof(
    internal::MemberFunctionPointer0<Nil, R>)];
  FuncPtrType function_;
  void Free() {
    if (NULL != function_) {
      function_->~Function0();
      function_ = NULL;
    }
  }
};

// wrap member function
template <class C, typename R>
Function<R> MakeFunction(C & obj,
  R(C::*func)()) {
  return Function<R>(obj, func);
}

// wrap function pointer
template <typename R>
Function<R> MakeFunction(
  R(*func)()) {
  return Function<R>(func);
}

// bind to Function0
template <class C, typename R>
Function<R> Bind(
  C & obj,
  R(C::*func)()) {
  return Function<R>(obj, func);
}

template <typename R>
Function<R> Bind(
  R(*func)()) {
  return Function<R>(func);
}

} // namespace base
} // namespace embb

#endif // EMBB_BASE_INTERNAL_FUNCTION0_H_