functionN.lua 20.5 KB
Newer Older
Michael Schmid committed
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
-- Copyright (c) 2014-2016, 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.

max_args = 5

function make_functionN(arg_cnt)
  local file = io.open("function"..arg_cnt..".h", "w")

  local include_guard = "EMBB_BASE_INTERNAL_FUNCTION"..arg_cnt.."_H_"

  local templ_param = ""
  local param_list_a = ""
  local param_list_c = ""
  local param_list = ""
  for i = 1, arg_cnt, 1 do
    if i > 1 then
      templ_param = templ_param..", "
      param_list_a = param_list_a..", "
      param_list_c = param_list_c..", "
      param_list = param_list..", "
    end
    templ_param = templ_param.."typename T"..i
    param_list_a = param_list_a.."T"..i
    param_list_c = param_list_c.."T"..i.." p"..i
    param_list = param_list.."p"..i
  end

  local param_list_a_nil = param_list_a
  for i = arg_cnt+1, max_args, 1 do
    if i > 1 then
	  param_list_a_nil = param_list_a_nil..", "
	end
    param_list_a_nil = param_list_a_nil.."Nil"
  end

  local CR_templ
  local C_templ
  local R_templ
  local void_templ
  local R_param_list_a
  local void_param_list_a
  if arg_cnt > 0 then
    CR_templ = "template <class C, typename R,\n  "..templ_param..">"
    C_templ = "template <class C,\n  "..templ_param..">"
    R_templ = "template <typename R,\n  "..templ_param..">"
    void_templ = "template <"..templ_param..">"
    R_param_list_a = "R, "..param_list_a
    void_param_list_a = "void, "..param_list_a
  else
    CR_templ = "template <class C, typename R>"
    C_templ = "template <class C>"
    R_templ = "template <typename R>"
	void_templ = "template <>"
    R_param_list_a = "R"
    void_param_list_a = "void"
  end

  local FunctionN = "Function"..arg_cnt
  local FunctionPointerN = "FunctionPointer"..arg_cnt
  local MemberFunctionPointerN = "MemberFunctionPointer"..arg_cnt
  local FunctorWrapperN = "FunctorWrapper"..arg_cnt


  file:write("#ifndef "..include_guard.."\n")
  file:write("#define "..include_guard.."\n")
  file:write("\n")

  file:write("#include <new>\n")
  file:write("#include <cstddef>\n")
  file:write("\n")
  file:write("#include <embb/base/internal/nil.h>\n")
  file:write("#include <embb/base/memory_allocation.h>\n")
  file:write("#include <embb/base/atomic.h>\n")
  file:write("#include <embb/base/internal/functionT.h>\n")
  if arg_cnt > 0 then
    file:write("#include <embb/base/internal/function0.h>\n")
  end
  if arg_cnt > 1 then
    file:write("#include <embb/base/internal/function1.h>\n")
  end
  file:write("\n")

  file:write("namespace embb {\n")
  file:write("namespace base {\n")
  file:write("\n")
  file:write("namespace internal {\n")
  file:write("\n")

  file:write(R_templ.."\n")
  file:write("class "..FunctionN.." {\n")
  file:write(" public:\n")
  file:write("  virtual ~"..FunctionN.."() {}\n")
  file:write("  virtual R operator () ("..param_list_a..") = 0;\n")
  file:write("  virtual void CopyTo(void* dst) = 0;\n")
  file:write("};\n")
  file:write("\n")

  file:write(R_templ.."\n")
  file:write("class "..FunctionPointerN.."\n  : public "..FunctionN.."<"..R_param_list_a.."> {\n")
  file:write(" public:\n")
  file:write("  typedef R(*FuncPtrType)("..param_list_a..");\n")
  file:write("  explicit "..FunctionPointerN.."(FuncPtrType func) : function_(func) {}\n")
  file:write("  virtual R operator () ("..param_list_c..") {\n")
  file:write("    return function_("..param_list..");\n")
  file:write("  }\n")
  file:write("  virtual void CopyTo(void* dst) {\n")
  file:write("    new(dst)"..FunctionPointerN.."(function_);\n")
  file:write("  }\n")
  file:write("\n")
  file:write(" private:\n")
  file:write("  FuncPtrType function_;\n")
  file:write("};\n")
  file:write("\n")

  file:write(void_templ.."\n")
  file:write("class "..FunctionPointerN.."<"..void_param_list_a..">\n  : public "..FunctionN.."<"..void_param_list_a.."> {\n")
  file:write(" public:\n")
  file:write("  typedef void(*FuncPtrType)("..param_list_a..");\n")
  file:write("  explicit "..FunctionPointerN.."(FuncPtrType func) : function_(func) {}\n")
  file:write("  virtual void operator () ("..param_list_c..") {\n")
  file:write("    function_("..param_list..");\n")
  file:write("  }\n")
  file:write("  virtual void CopyTo(void* dst) {\n")
  file:write("    new(dst)"..FunctionPointerN.."(function_);\n")
  file:write("  }\n")
  file:write("\n")
  file:write(" private:\n")
  file:write("  FuncPtrType function_;\n")
  file:write("};\n")
  file:write("\n")

  file:write(CR_templ.."\n")
  file:write("class "..MemberFunctionPointerN.."\n  : public "..FunctionN.."<"..R_param_list_a.."> {\n")
  file:write(" public:\n")
  file:write("  typedef R(C::*MemFuncPtrType)("..param_list_a..");\n")
  file:write("  typedef C & ClassRefType;\n")
  file:write("  "..MemberFunctionPointerN.."(ClassRefType obj, MemFuncPtrType func)\n  : object_(obj), function_(func) {}\n")
  file:write("  explicit "..MemberFunctionPointerN.."(ClassRefType obj)\n  : object_(obj), function_(&C::operator()) {}\n")
  file:write("  void operator = ("..MemberFunctionPointerN.." const & memfunc) {\n")
  file:write("    object_ = memfunc.object_;\n")
  file:write("    function_ = memfunc.function_;\n")
  file:write("  }\n")
  file:write("  virtual R operator () ("..param_list_c..") {\n")
  file:write("    return (object_.*function_)("..param_list..");\n")
  file:write("  }\n")
  file:write("  virtual void CopyTo(void* dst) {\n")
  file:write("    new(dst)"..MemberFunctionPointerN.."(object_, function_);\n")
  file:write("  }\n")
  file:write("\n")
  file:write(" private:\n")
  file:write("  ClassRefType object_;\n")
  file:write("  MemFuncPtrType function_;\n")
  file:write("};\n")
  file:write("\n")
 
  file:write(C_templ.."\n")
  file:write("class "..MemberFunctionPointerN.."<C, "..void_param_list_a..">\n  : public "..FunctionN.."<"..void_param_list_a.."> {\n")
  file:write(" public:\n")
  file:write("  typedef void(C::*MemFuncPtrType)("..param_list_a..");\n")
  file:write("  typedef C & ClassRefType;\n")
  file:write("  "..MemberFunctionPointerN.."(ClassRefType obj, MemFuncPtrType func)\n  : object_(obj), function_(func) {}\n")
  file:write("  explicit "..MemberFunctionPointerN.."(ClassRefType obj)\n  : object_(obj), function_(&C::operator()) {}\n")
  file:write("  void operator = ("..MemberFunctionPointerN.." const & memfunc) {\n")
  file:write("    object_ = memfunc.object_;\n")
  file:write("    function_ = memfunc.function_;\n")
  file:write("  }\n")
  file:write("  virtual void operator () ("..param_list_c..") {\n")
  file:write("    (object_.*function_)("..param_list..");\n")
  file:write("  }\n")
  file:write("  virtual void CopyTo(void* dst) {\n")
  file:write("    new(dst)"..MemberFunctionPointerN.."(object_, function_);\n")
  file:write("  }\n")
  file:write("\n")
  file:write(" private:\n")
  file:write("  ClassRefType object_;\n")
  file:write("  MemFuncPtrType function_;\n")
  file:write("};\n")
  file:write("\n")

  file:write(CR_templ.."\n")
  file:write("class "..FunctorWrapperN.."\n  : public "..FunctionN.."<"..R_param_list_a.."> {\n")
  file:write(" public:\n")
  file:write("  "..FunctorWrapperN.."() : object_(NULL), ref_count_(NULL) {}\n")
  file:write("  explicit "..FunctorWrapperN.."(C const & obj) {\n")
  file:write("    object_ = Allocation::New<C>(obj);\n")
  file:write("    ref_count_ = Allocation::New<Atomic<int> >(1);\n")
  file:write("  }\n")
  file:write("  explicit "..FunctorWrapperN.."("..FunctorWrapperN.." const & other) {\n")
  file:write("    object_ = other.object_;\n")
  file:write("    ref_count_ = other.ref_count_;\n")
  file:write("    ++*ref_count_;\n")
  file:write("  }\n")
  file:write("  virtual ~"..FunctorWrapperN.."() {\n")
  file:write("    if (0 == --*ref_count_) {\n")
  file:write("      Allocation::Delete(ref_count_);\n")
  file:write("      Allocation::Delete(object_);\n")
  file:write("    }\n")
  file:write("  }\n")
  file:write("  virtual R operator () ("..param_list_c..") {\n")
  file:write("    return (*object_)("..param_list..");\n")
  file:write("  }\n")
  file:write("  virtual void CopyTo(void* dst) {\n")
  file:write("    new(dst)"..FunctorWrapperN.."(*this);\n")
  file:write("  }\n")
  file:write("\n")
  file:write(" private:\n")
  file:write("  C * object_;\n")
  file:write("  Atomic<int> * ref_count_;\n")
  file:write("};\n")
  file:write("\n")

  file:write(C_templ.."\n")
  file:write("class "..FunctorWrapperN.."<C, "..void_param_list_a..">\n  : public "..FunctionN.."<"..void_param_list_a.."> {\n")
  file:write(" public:\n")
  file:write("  "..FunctorWrapperN.."() : object_(NULL), ref_count_(NULL) {}\n")
  file:write("  explicit "..FunctorWrapperN.."(C const & obj) {\n")
  file:write("    object_ = Allocation::New<C>(obj);\n")
  file:write("    ref_count_ = Allocation::New<Atomic<int> >(1);\n")
  file:write("  }\n")
  file:write("  explicit "..FunctorWrapperN.."("..FunctorWrapperN.." const & other) {\n")
  file:write("    object_ = other.object_;\n")
  file:write("    ref_count_ = other.ref_count_;\n")
  file:write("    ++*ref_count_;\n")
  file:write("  }\n")
  file:write("  virtual ~"..FunctorWrapperN.."() {\n")
  file:write("    if (0 == --*ref_count_) {\n")
  file:write("      Allocation::Delete(ref_count_);\n")
  file:write("      Allocation::Delete(object_);\n")
  file:write("    }\n")
  file:write("  }\n")
  file:write("  virtual void operator () ("..param_list_c..") {\n")
  file:write("    (*object_)("..param_list..");\n")
  file:write("  }\n")
  file:write("  virtual void CopyTo(void* dst) {\n")
  file:write("    new(dst)"..FunctorWrapperN.."(*this);\n")
  file:write("  }\n")
  file:write("\n")
  file:write(" private:\n")
  file:write("  C * object_;\n")
  file:write("  Atomic<int> * ref_count_;\n")
  file:write("};\n")
  file:write("\n")

  -- bind to function0
  if arg_cnt > 0 then
    local param_list_ = ""
    for i = 1, arg_cnt, 1 do
      if i > 1 then
        param_list_ = param_list_..", "
      end
      param_list_ = param_list_.."p"..i.."_"
    end
    file:write("// bind to function0\n")
    file:write(R_templ.."\n")
    file:write("class Bound"..arg_cnt.."Functor0 {\n")
    file:write(" public:\n")
    file:write("  Bound"..arg_cnt.."Functor0(Function<"..R_param_list_a.."> func,\n    "..param_list_c..") : function_(func)\n      ")
    for i = 1, arg_cnt, 1 do
      file:write(", p"..i.."_(p"..i..")")
    end
    file:write(" {}\n")
    file:write("  Bound"..arg_cnt.."Functor0(Bound"..arg_cnt.."Functor0 const & func) : function_(func.function_)\n    ")
    for i = 1, arg_cnt, 1 do
      file:write(", p"..i.."_(func.p"..i.."_)")
	  if i == 4 then
	    file:write("\n    ")
      end
    end
    file:write(" {}\n")
    file:write("  R operator() () {\n")
    file:write("    return function_("..param_list_..");\n")
    file:write("  }\n")
  file:write("\n")
    file:write(" private:\n")
    file:write("  Function<"..R_param_list_a.."> function_;\n")
    for i = 1, arg_cnt, 1 do
      file:write("  T"..i.." p"..i.."_;\n")
    end
    file:write("};\n")
    file:write("\n")
  end

  -- bind to function1
  if arg_cnt > 1 then
    file:write("// bind to function1\n")
    for arg = 1, arg_cnt, 1 do
      local param_list_ = ""
      local param_list_c_ = ""
      for i = 1, arg_cnt, 1 do
        if i > 1 then
          param_list_ = param_list_..", "
        end
        param_list_ = param_list_.."p"..i
        if i ~= arg then
          param_list_ = param_list_.."_"
          param_list_c_ = param_list_c_..", T"..i.." p"..i
        end
      end
      file:write(R_templ.."\n")
      file:write("class Bound"..arg_cnt.."Functor1_Arg"..arg.." {\n")
      file:write(" public:\n")
      file:write("  Bound"..arg_cnt.."Functor1_Arg"..arg.."(Function<"..R_param_list_a.."> func\n    "..param_list_c_..") : function_(func)\n      ")
      for i = 1, arg_cnt, 1 do
        if i ~= arg then
          file:write(", p"..i.."_(p"..i..")")
        end
      end
      file:write(" {}\n")
      file:write("  Bound"..arg_cnt.."Functor1_Arg"..arg.."(Bound"..arg_cnt.."Functor1_Arg"..arg.." const & func)\n    : function_(func.function_)\n    ")
      for i = 1, arg_cnt, 1 do
        if i ~= arg then
          file:write(", p"..i.."_(func.p"..i.."_)")
        end
      end
      file:write(" {}\n")
      file:write("  R operator() (T"..arg.." p"..arg..") {\n")
      file:write("    return function_("..param_list_..");\n")
      file:write("  }\n")
  file:write("\n")
      file:write(" private:\n")
      file:write("  Function<"..R_param_list_a.."> function_;\n")
      for i = 1, arg_cnt, 1 do
        if i ~= arg then
          file:write("  T"..i.." p"..i.."_;\n")
        end
      end
      file:write("};\n")
      file:write("\n")
    end
  end
  
  file:write("} // namespace internal\n")
  file:write("\n")

  file:write("\n")
  file:write("using embb::base::internal::Nil;\n")
  file:write("\n")

  file:write(R_templ.."\n")
  if arg_cnt < max_args then
    file:write("class Function<R, "..param_list_a_nil.."> {\n")
  else
    file:write("class Function {\n")
  end
  file:write(" public:\n")
  file:write("  typedef internal::"..FunctionN.."<"..R_param_list_a.."> * FuncPtrType;\n")
  file:write("  Function() : function_(NULL) {}\n")
  file:write("  template <class C>\n")
  file:write("  explicit Function(C const & obj) {\n")
  file:write("    function_ = new(storage_)\n      internal::"..FunctorWrapperN.."<C, "..R_param_list_a..">(obj);\n")
  file:write("  }\n")
  file:write("  Function(Function const & func) {\n")
  file:write("    func.function_->CopyTo(&storage_[0]);\n")
  file:write("    function_ = reinterpret_cast<FuncPtrType>(&storage_[0]);\n")
  file:write("  }\n")
  file:write("  ~Function() {\n")
  file:write("    Free();\n")
  file:write("  }\n")
  file:write("  void operator = (R(*func)("..param_list_a..")) {\n")
  file:write("    Free();\n")
  file:write("    function_ = new(storage_)\n      internal::"..FunctionPointerN.."<"..R_param_list_a..">(func);\n")
  file:write("  }\n")
  file:write("  void operator = (Function & func) {\n")
  file:write("    Free();\n")
  file:write("    func.function_->CopyTo(&storage_[0]);\n")
  file:write("    function_ = reinterpret_cast<FuncPtrType>(&storage_[0]);\n")
  file:write("  }\n")
  file:write("  template <class C>\n")
  file:write("  void operator = (C const & obj) {\n")
  file:write("    Free();\n")
  file:write("    function_ = new(storage_)\n      internal::"..FunctorWrapperN.."<C, "..R_param_list_a..">(obj);\n")
  file:write("  }\n")
  file:write("  explicit Function(R(*func)("..param_list_a..")) {\n")
  file:write("    function_ = new(storage_)\n      internal::"..FunctionPointerN.."<"..R_param_list_a..">(func);\n")
  file:write("  }\n")
  file:write("  template <class C>\n")
  file:write("  Function(C & obj, R(C::*func)("..param_list_a..")) {\n")
  file:write("    function_ = new(storage_)\n      internal::"..MemberFunctionPointerN.."<C, "..R_param_list_a..">(obj, func);\n")
  file:write("  }\n")
  file:write("  R operator () ("..param_list_c..") {\n")
  file:write("    return (*function_)("..param_list..");\n")
  file:write("  }\n")
  file:write("\n")
  file:write(" private:\n")
  file:write("  char storage_[sizeof(\n    internal::"..MemberFunctionPointerN.."<Nil, "..R_param_list_a..">)];\n")
  file:write("  FuncPtrType function_;\n")
  file:write("  void Free() {\n")
  file:write("    if (NULL != function_) {\n")
  file:write("      function_->~"..FunctionN.."();\n")
  file:write("      function_ = NULL;\n")
  file:write("    }\n")
  file:write("  }\n")
  file:write("};\n")
  file:write("\n")

  -- wrap member function
  file:write("// wrap member function\n")
  file:write(CR_templ.."\n")
  file:write("Function<"..R_param_list_a.."> MakeFunction(C & obj,\n  R(C::*func)("..param_list_a..")) {\n")
  file:write("  return Function<"..R_param_list_a..">(obj, func);\n")
  file:write("}\n")
  file:write("\n")

  -- wrap function pointer
  file:write("// wrap function pointer\n")
  file:write(R_templ.."\n")
  file:write("Function<"..R_param_list_a.."> MakeFunction(\n  R(*func)("..param_list_a..")) {\n")
  file:write("  return Function<"..R_param_list_a..">(func);\n")
  file:write("}\n")
  file:write("\n")

  -- bind to function0
  if arg_cnt > 0 then
    file:write("// bind to function0\n")

    file:write(R_templ.."\n")
    file:write("Function<R> Bind(Function<"..R_param_list_a.."> func,\n  "..param_list_c..") {\n")
    file:write("  return Function<R>(\n  internal::Bound"..arg_cnt.."Functor0<"..R_param_list_a..">(\n    func, "..param_list.."));\n")
    file:write("}\n")
    file:write("\n")

    file:write(R_templ.."\n")
    file:write("Function<R> Bind(R(*func)("..param_list_a.."),\n  "..param_list_c..") {\n")
    file:write("  return Bind(Function<"..R_param_list_a..">(func), "..param_list..");\n")
    file:write("}\n")
    file:write("\n")

    file:write(CR_templ.."\n")
    file:write("Function<R> Bind(C & obj, R(C::*func)("..param_list_a.."),\n  "..param_list_c..") {\n")
    file:write("  return Bind(Function<"..R_param_list_a..">(obj, func), "..param_list..");\n")
    file:write("}\n")
    file:write("\n")
  end

  -- bind to function1
  if arg_cnt > 1 then
    file:write("// bind to function1\n")
    for arg = 1, arg_cnt, 1 do
      local target_arg = "T"..arg
      local bound_param_list_a = ""
      local bound_param_list_c = ""
      local bound_param_list = ""
      for i = 1, arg_cnt, 1 do
        if i == arg then
          bound_param_list_c = bound_param_list_c..", Placeholder::Arg_1 p"..i
          bound_param_list_a = bound_param_list_a..", Placeholder::Arg_1"
        else
          bound_param_list_c = bound_param_list_c..", T"..i.." p"..i
          bound_param_list_a = bound_param_list_a..", T"..i.." p"..i
          bound_param_list = bound_param_list..", p"..i
        end
      end

      file:write(R_templ.."\n")
      file:write("Function<R, "..target_arg.."> Bind(Function<"..R_param_list_a.."> func\n  "..bound_param_list_a..") {\n")
      file:write("  return Function<R, "..target_arg..">(\n  internal::Bound"..arg_cnt.."Functor1_Arg"..arg.."<"..R_param_list_a..">(func\n    "..bound_param_list.."));\n")
      file:write("}\n")
      file:write("\n")

      file:write(R_templ.."\n")
      file:write("Function<R, "..target_arg.."> Bind(R(*func)("..param_list_a..")\n  "..bound_param_list_c..") {\n")
      file:write("  return Bind(Function<"..R_param_list_a..">(func), "..param_list..");\n")
      file:write("}\n")
      file:write("\n")

      file:write(CR_templ.."\n")
      file:write("Function<R, "..target_arg.."> Bind(C & obj, R(C::*func)("..param_list_a..")\n  "..bound_param_list_c..") {\n")
      file:write("  return Bind(Function<"..R_param_list_a..">(obj, func), "..param_list..");\n")
      file:write("}\n")
      file:write("\n")
    end
  end

  -- bind to functionN
  file:write("// bind to "..FunctionN.."\n")

  local bound_param_list_c = ""
  for i = 1, arg_cnt, 1 do
    bound_param_list_c = bound_param_list_c..",\n  Placeholder::Arg_"..i
  end

  file:write(CR_templ.."\n")
  file:write("Function<"..R_param_list_a.."> Bind(\n  C & obj,\n  R(C::*func)("..param_list_a..")"..bound_param_list_c..") {\n")
  file:write("  return Function<"..R_param_list_a..">(obj, func);\n")
  file:write("}\n")
  file:write("\n")

  file:write(R_templ.."\n")
  file:write("Function<"..R_param_list_a.."> Bind(\n  R(*func)("..param_list_a..")"..bound_param_list_c..") {\n")
  file:write("  return Function<"..R_param_list_a..">(func);\n")
  file:write("}\n")
  file:write("\n")

  file:write("} // namespace base\n")
  file:write("} // namespace embb\n")
  file:write("\n")

  file:write("#endif // "..include_guard.."\n")

  file:flush()
  file:close()
end

for i = 0, max_args, 1 do
  make_functionN(i)
end