/*!
 * UbusCall.h
 *
 * Copyright (c) 2015-2018, NADAL Jean-Baptiste. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 * 
 * @Author:  NADAL Jean-Baptiste
 * @Date: 21/03/2015
 *
 */

#ifndef _UBUS_CPP_H
#define _UBUS_CPP_H

/*------------------------------- INCLUDES ----------------------------------*/

extern "C" { 
#include <libubus.h>
}


namespace UBus { 

namespace detail { 

/**
 * simple POD vehicle for arrays 
 */
template<typename T>
struct Array { 
	T* ptr;
	size_t count; 
};

/**
 * construct a detail::Array with one arg
 * @note : T should be a POD and each argument will be copied as is using memcpy
 */
template<typename T>
static inline Array<T> makeArray(const T& e0) { 
	Array<T> result;
	result.count = 1;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1) { 
	Array<T> result;
	result.count = 2;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2) { 
	Array<T> result;
	result.count = 3;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3) { 
	Array<T> result;
	result.count = 4;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4) { 
	Array<T> result;
	result.count = 5;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	return result;
}


template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5) { 
	Array<T> result;
	result.count = 6;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	return result;
}


template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5,
                                 const T& e6) { 
	Array<T> result;
	result.count = 7;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	return result;
}


template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5,
                                 const T& e6, const T& e7) { 
	Array<T> result;
	result.count = 8;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	return result;
}


template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5,
                                 const T& e6, const T& e7, const T& e8) { 
	Array<T> result;
	result.count = 9;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9) {
	Array<T> result;
	result.count = 10;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9, const T& e10) {
	Array<T> result;
	result.count = 11;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	memcpy(&result.ptr[10], &e10, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9, const T& e10, const T& e11) {
	Array<T> result;
	result.count = 12;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	memcpy(&result.ptr[10], &e10, sizeof(T));
	memcpy(&result.ptr[11], &e11, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9, const T& e10, const T& e11, 
                                 const T& e12) {
	Array<T> result;
	result.count = 13;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	memcpy(&result.ptr[10], &e10, sizeof(T));
	memcpy(&result.ptr[11], &e11, sizeof(T));
	memcpy(&result.ptr[12], &e12, sizeof(T));
	return result;
}


template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9, const T& e10, const T& e11, 
                                 const T& e12, const T& e13) {
	Array<T> result;
	result.count = 14;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	memcpy(&result.ptr[10], &e10, sizeof(T));
	memcpy(&result.ptr[11], &e11, sizeof(T));
	memcpy(&result.ptr[12], &e12, sizeof(T));
	memcpy(&result.ptr[13], &e13, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9, const T& e10, const T& e11, 
                                 const T& e12, const T& e13, const T& e14) {
	Array<T> result;
	result.count = 15;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	memcpy(&result.ptr[10], &e10, sizeof(T));
	memcpy(&result.ptr[11], &e11, sizeof(T));
	memcpy(&result.ptr[12], &e12, sizeof(T));
	memcpy(&result.ptr[13], &e13, sizeof(T));
	memcpy(&result.ptr[14], &e14, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9, const T& e10, const T& e11, 
                                 const T& e12, const T& e13, const T& e14, 
                                 const T& e15) {
	Array<T> result;
	result.count = 16;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	memcpy(&result.ptr[10], &e10, sizeof(T));
	memcpy(&result.ptr[11], &e11, sizeof(T));
	memcpy(&result.ptr[12], &e12, sizeof(T));
	memcpy(&result.ptr[13], &e13, sizeof(T));
	memcpy(&result.ptr[14], &e14, sizeof(T));
	memcpy(&result.ptr[15], &e15, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9, const T& e10, const T& e11, 
                                 const T& e12, const T& e13, const T& e14, 
                                 const T& e15, const T& e16) {
	Array<T> result;
	result.count = 17;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	memcpy(&result.ptr[10], &e10, sizeof(T));
	memcpy(&result.ptr[11], &e11, sizeof(T));
	memcpy(&result.ptr[12], &e12, sizeof(T));
	memcpy(&result.ptr[13], &e13, sizeof(T));
	memcpy(&result.ptr[14], &e14, sizeof(T));
	memcpy(&result.ptr[15], &e15, sizeof(T));
	memcpy(&result.ptr[16], &e16, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9, const T& e10, const T& e11, 
                                 const T& e12, const T& e13, const T& e14, 
                                 const T& e15, const T& e16, const T& e17) {
	Array<T> result;
	result.count = 18;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	memcpy(&result.ptr[10], &e10, sizeof(T));
	memcpy(&result.ptr[11], &e11, sizeof(T));
	memcpy(&result.ptr[12], &e12, sizeof(T));
	memcpy(&result.ptr[13], &e13, sizeof(T));
	memcpy(&result.ptr[14], &e14, sizeof(T));
	memcpy(&result.ptr[15], &e15, sizeof(T));
	memcpy(&result.ptr[16], &e16, sizeof(T));
	memcpy(&result.ptr[17], &e17, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9, const T& e10, const T& e11, 
                                 const T& e12, const T& e13, const T& e14, 
                                 const T& e15, const T& e16, const T& e17, 
                                 const T& e18) {
	Array<T> result;
	result.count = 19;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	memcpy(&result.ptr[10], &e10, sizeof(T));
	memcpy(&result.ptr[11], &e11, sizeof(T));
	memcpy(&result.ptr[12], &e12, sizeof(T));
	memcpy(&result.ptr[13], &e13, sizeof(T));
	memcpy(&result.ptr[14], &e14, sizeof(T));
	memcpy(&result.ptr[15], &e15, sizeof(T));
	memcpy(&result.ptr[16], &e16, sizeof(T));
	memcpy(&result.ptr[17], &e17, sizeof(T));
	memcpy(&result.ptr[18], &e18, sizeof(T));
	return result;
}

template<typename T>
static inline Array<T> makeArray(const T& e0, const T& e1, const T& e2, 
                                 const T& e3, const T& e4, const T& e5, 
                                 const T& e6, const T& e7, const T& e8, 
                                 const T& e9, const T& e10, const T& e11, 
                                 const T& e12, const T& e13, const T& e14, 
                                 const T& e15, const T& e16, const T& e17, 
                                 const T& e18, const T& e19) {
	Array<T> result;
	result.count = 20;
	result.ptr = new T[result.count]();
	memcpy(&result.ptr[0], &e0, sizeof(T));
	memcpy(&result.ptr[1], &e1, sizeof(T));
	memcpy(&result.ptr[2], &e2, sizeof(T));
	memcpy(&result.ptr[3], &e3, sizeof(T));
	memcpy(&result.ptr[4], &e4, sizeof(T));
	memcpy(&result.ptr[5], &e5, sizeof(T));
	memcpy(&result.ptr[6], &e6, sizeof(T));
	memcpy(&result.ptr[7], &e7, sizeof(T));
	memcpy(&result.ptr[8], &e8, sizeof(T));
	memcpy(&result.ptr[9], &e9, sizeof(T));
	memcpy(&result.ptr[10], &e10, sizeof(T));
	memcpy(&result.ptr[11], &e11, sizeof(T));
	memcpy(&result.ptr[12], &e12, sizeof(T));
	memcpy(&result.ptr[13], &e13, sizeof(T));
	memcpy(&result.ptr[14], &e14, sizeof(T));
	memcpy(&result.ptr[15], &e15, sizeof(T));
	memcpy(&result.ptr[16], &e16, sizeof(T));
	memcpy(&result.ptr[17], &e17, sizeof(T));
	memcpy(&result.ptr[18], &e18, sizeof(T));
	memcpy(&result.ptr[19], &e19, sizeof(T));
	return result;
}


/**
 * simple copy of c string, free should be used to release memory
 */
static inline char* copy_str(const char* str) { 
	return  str ? strdup(str) : 0;
}

/**
 * generic function which copy an Array
 */
template<typename T>
static inline T* copy_array(const T* src, const size_t count, void(*copy_func)(T&, const T&)) { 
	if(count == 0 || src == 0) { 
		return 0;
	}

	T* result = new T[count];
	for(size_t i = 0; i < count; ++i) { 
		copy_func(result[i], src[i]);
	}

	return result;
}

/**
 * generic function which delete an array
 */
template<typename T>
static inline void delete_array(T* target, const size_t count, 
	                            void(*delete_func)(T&)) { 
	if(count == 0 || target == 0) { 
		return;
	}

	for(size_t i = 0; i < count; ++i) { 
		delete_func(target[i]);
	}

	delete[] target;
}


struct Arg : public blobmsg_policy { };

/**
 * copy ubus method argument
 * @important : does not delete the previous content
 */
static inline void copy_arg(blobmsg_policy& dest, const blobmsg_policy& src) { 
	dest.name = copy_str(src.name);
	dest.type = src.type;
}

static inline blobmsg_policy* copy_args(const blobmsg_policy* src, const size_t count) { 
	return copy_array(src, count, copy_arg);
}

static inline void delete_arg(blobmsg_policy& target) { 
	free((char*)target.name);
}

static inline void delete_args(blobmsg_policy* target, size_t count) { 
	delete_array(target, count, delete_arg);
}

/**
 * simple wrapper which avoid mixin of c and c++ declaration
 */
struct _Method : public ubus_method { };

/**
 * copy a Method
 * @important : does not delete the prpevious content !!!
 */
static inline void copy_method(ubus_method& dest, const ubus_method& src) { 
	dest.name = copy_str(src.name);
	dest.handler = src.handler;
	dest.mask = src.mask;
	dest.policy = copy_args(src.policy, src.n_policy);
	dest.n_policy = src.n_policy;
}

/**
 * copy an array of Method
 */
static inline ubus_method* copy_methods(const ubus_method* src, const size_t count) { 
	return copy_array(src, count, copy_method);
}

/**
 * delete an Method
 */
static inline void delete_method(ubus_method& target) { 
	free((char*)target.name);
	delete_args((blobmsg_policy*)target.policy, target.n_policy);
}

/**
 * delete an array of Method 
 */
static inline void delete_methods(ubus_method* target, const size_t count) { 
	delete_array(target, count, delete_method);
}

}



class ObjectType : public ubus_object_type { 
public:
	ObjectType(const char* name_, const detail::_Method& m0) {
		Init(name_, detail::makeArray(m0));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1) {
		Init(name_, detail::makeArray(m0, m1));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2) {
		Init(name_, detail::makeArray(m0, m1, m2));
	}
	
	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3) {
		Init(name_, detail::makeArray(m0, m1, m2, m3));
	}
	
	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method m4) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4));
	}
	
	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method m4,
	           const detail::_Method& m5) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5));
	}
	
	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method m4,
	           const detail::_Method& m5, const detail::_Method& m6) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6));
	}
	
	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method m4,
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7));
	}
	
	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method m4,
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7,
	           const detail::_Method& m8) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10, 
	           const detail::_Method& m11) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10, 
	           const detail::_Method& m11, const detail::_Method& m12) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10, 
	           const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, 
		     m13));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10, 
	           const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13, 
	           const detail::_Method& m14) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, 
		     m13, m14));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10, 
	           const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13, 
	           const detail::_Method& m14, const detail::_Method& m15) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, 
		     m13, m14, m15));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10, 
	           const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13, 
	           const detail::_Method& m14, const detail::_Method& m15, const detail::_Method& m16) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, 
		     m13, m14, m15, m16));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10, 
	           const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13, 
	           const detail::_Method& m14, const detail::_Method& m15, const detail::_Method& m16, 
	           const detail::_Method& m17) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, 
		     m13, m14, m15, m16, m17));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10, 
	           const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13, 
	           const detail::_Method& m14, const detail::_Method& m15, const detail::_Method& m16, 
	           const detail::_Method& m17, const detail::_Method& m18) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, 
		     m13, m14, m15, m16, m17, m18));
	}

	ObjectType(const char* name_, const detail::_Method& m0, const detail::_Method& m1, 
	           const detail::_Method& m2, const detail::_Method& m3, const detail::_Method& m4, 
	           const detail::_Method& m5, const detail::_Method& m6, const detail::_Method& m7, 
	           const detail::_Method& m8, const detail::_Method& m9, const detail::_Method& m10, 
	           const detail::_Method& m11, const detail::_Method& m12, const detail::_Method& m13, 
	           const detail::_Method& m14, const detail::_Method& m15, const detail::_Method& m16, 
	           const detail::_Method& m17, const detail::_Method& m18, const detail::_Method& m19) {
		Init(name_, detail::makeArray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, 
		     m13, m14, m15, m16, m17, m18, m19));
	}


	ObjectType(const char* name_, detail::Array<detail::_Method> m) {
		Init(name_, m);
	}


	ObjectType(const ObjectType& other) {
		name = detail::copy_str(other.name); 
		id = other.id; 
		methods = detail::copy_methods(other.methods, other.n_methods); 
		n_methods = other.n_methods;
	}

	~ObjectType() { 
		free((char*)name);
		detail::delete_methods((ubus_method*) methods, n_methods);
	}

	ObjectType& operator=(ObjectType other) { 
		Swap(other);
		return *this;
	}

	void Swap(ObjectType& other) { 
		ubus_object_type tmp = other;
		memcpy(&other, this, sizeof(*this));
		memcpy(this, &tmp, sizeof(*this));
	}

private:
	// delegate constructor ...
	void Init(const char* name_, detail::Array<detail::_Method> m) { 
		name = detail::copy_str(name_);
		id = 0;
		methods = m.ptr;
		n_methods = m.count;
	}
};


/**
 * generate an ubus_method without arg 
 */
static inline detail::_Method Method(const char* name, ubus_handler_t handler) { 
	detail::_Method result;
	result.name = detail::copy_str(name);
	result.handler = handler;
	result.mask = 0;
	result.policy = 0;
	result.n_policy = 0;

	return result;
}

/**
 * generate an ubus_method with an array of args 
 */
static inline detail::_Method Method(const char* name, ubus_handler_t handler, 
	                                detail::Array<detail::Arg> args) { 
	detail::_Method result; 
	result.name = detail::copy_str(name); 
	result.handler = handler;
	result.mask = 0;
	result.policy = args.ptr;
	result.n_policy = args.count; 
	return result;
}

/**
 * generate an ubus_method with one arg (shortcut for Array of one arg)
 */ 
static inline detail::_Method Method(const char* name, ubus_handler_t handler, 
	                             const detail::Arg& arg0) { 
	return Method(name, handler, detail::makeArray(arg0));
}

static inline detail::_Method Method(const char* name, ubus_handler_t handler, 
                                 const detail::Arg& arg0, const detail::Arg& arg1) {
	return Method(name, handler, detail::makeArray(arg0, arg1));
}

static inline detail::_Method Method(const char* name, ubus_handler_t handler, 
                                 const detail::Arg& arg0, const detail::Arg& arg1, 
                                 const detail::Arg& arg2) {
	return Method(name, handler, detail::makeArray(arg0, arg1, arg2));
}

static inline detail::_Method Method(const char* name, ubus_handler_t handler, 
                                 const detail::Arg& arg0, const detail::Arg& arg1, 
                                 const detail::Arg& arg2, const detail::Arg& arg3) {
	return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3));
}

static inline detail::_Method Method(const char* name, ubus_handler_t handler, 
                                 const detail::Arg& arg0, const detail::Arg& arg1, 
                                 const detail::Arg& arg2, const detail::Arg& arg3, 
                                 const detail::Arg& arg4) {
	return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3, arg4));
}

static inline detail::_Method Method(const char* name, ubus_handler_t handler, 
                                 const detail::Arg& arg0, const detail::Arg& arg1, 
                                 const detail::Arg& arg2, const detail::Arg& arg3, 
                                 const detail::Arg& arg4, const detail::Arg& arg5) {
	return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3, arg4, arg5));
}

static inline detail::_Method Method(const char* name, ubus_handler_t handler, 
                                 const detail::Arg& arg0, const detail::Arg& arg1, 
                                 const detail::Arg& arg2, const detail::Arg& arg3, 
                                 const detail::Arg& arg4, const detail::Arg& arg5, 
                                 const detail::Arg& arg6) {
	return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3, arg4, arg5, arg6));
}


static inline detail::_Method Method(const char* name, ubus_handler_t handler, 
                                 const detail::Arg& arg0, const detail::Arg& arg1, 
                                 const detail::Arg& arg2, const detail::Arg& arg3, 
                                 const detail::Arg& arg4, const detail::Arg& arg5, 
                                 const detail::Arg& arg6, const detail::Arg& arg7) {
	return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7));
}



static inline detail::_Method Method(const char* name, ubus_handler_t handler, 
                                 const detail::Arg& arg0, const detail::Arg& arg1, 
                                 const detail::Arg& arg2, const detail::Arg& arg3, 
                                 const detail::Arg& arg4, const detail::Arg& arg5, 
                                 const detail::Arg& arg6, const detail::Arg& arg7, 
                                 const detail::Arg& arg8) {
	return Method(name, handler, detail::makeArray(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
}


static inline detail::Arg Arg(const char* name, enum blobmsg_type type) { 
	detail::Arg res;
	res.name = detail::copy_str(name);
	res.type = type;
	return res;
}

static inline detail::Arg UnspecArg(const char* name) { 
	return Arg(name, BLOBMSG_TYPE_UNSPEC);
}

static inline detail::Arg ArrayArg(const char* name) { 
	return Arg(name, BLOBMSG_TYPE_ARRAY);
}

static inline detail::Arg TableArg(const char* name) { 
	return Arg(name, BLOBMSG_TYPE_TABLE);
}

static inline detail::Arg StringArg(const char* name) { 
	return Arg(name, BLOBMSG_TYPE_STRING);
}

static inline detail::Arg Int64Arg(const char* name) { 
	return Arg(name, BLOBMSG_TYPE_INT64);
}

static inline detail::Arg Int32Arg(const char* name) { 
	return Arg(name, BLOBMSG_TYPE_INT32);
}

static inline detail::Arg Int16Arg(const char* name) { 
	return Arg(name, BLOBMSG_TYPE_INT16);
}

static inline detail::Arg Int8Arg(const char* name) { 
	return Arg(name, BLOBMSG_TYPE_INT8);
}

static inline detail::Arg BoolArg(const char* name) { 
	return Arg(name, BLOBMSG_TYPE_BOOL);
}


}	// namespace UBus

/**
 * Generate a function which dispatch a ubus method call to the appropriate 
 * method of a aw_ubus_object.
 *
 * This template generate a function compatible with ubus_handler
 *   
 * @tparam ObjectType : the type of the object containing the method to call 
 * @tparam Method : the member function of ObjectType to call, this member function 
 * has the signature : 
 *     int(struct ubus_context *ctx, struct ubus_request_data *req, struct blob_attr *msg)
 */
template<typename ObjectType, 
         int (ObjectType::*Method)(struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
>
static int ubus_hook (struct ubus_context *ctx, struct ubus_object *obj, 
                      struct ubus_request_data *req, const char *method, 
                      struct blob_attr *msg) 
{ 
	return (static_cast<ObjectType*>(obj)->*Method)(ctx, req, msg);
}

/**
 * Declare a ubus trampoline function which map call the right method of an ubus object
 * @param CLASS : class name of the instance object targeted by the trampoline
 * @param METHOD : method name targeted by the trampoline
 * @return a ubus_handler_t  
 */
#define UBUS_CPP(CLASS, METHOD) &ubus_hook<CLASS, &CLASS::METHOD>

#endif /* _UBUS_CPP_H */
