/* Copyright (c) 1997-2009
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program 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 General Public License for more details.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: GenericStruct.h 9298 2009-09-02 23:10:56Z gawrilow $
*/

#ifndef _POLYMAKE_COMMON_PERL_BINDINGS_GenericStruct_H
#define _POLYMAKE_COMMON_PERL_BINDINGS_GenericStruct_H

#include_next <GenericStruct.h>
#include <polymake.h>

namespace pm { namespace perl {

   template <typename Struct, int i>
   struct StructUtils_helper {
      typedef typename Struct::field_types type_list;
      static const int next= i+1 < list_length<type_list>::value ? i+1 : i;
      typedef StructUtils_helper<Struct,next> recurse_down;

      static void _do_fields(ArrayBase& arr)
      {
	 size_t nl;
	 const char *n=Struct::get_field_name(nl,int2type<i>());
	 arr.push(pm_perl_newSVstr_shared(n,nl));
	 if (next>i) recurse_down::_do_fields(arr);
      }
   };

   template <typename Struct>
   struct StructUtils {
      typedef typename Struct::field_types type_list;

      static SV** (*push())(SV **stack) { return &TypeList_helper<type_list,0>::_do_push; }

      static SV* field_names()
      {
	 ArrayBase ret(list_length<type_list>::value);
	 StructUtils_helper<Struct,0>::_do_fields(ret);
	 return ret.get();
      }
   };

   template <typename Struct, size_t pkgl, bool exact_match>
   SV4recognize<exact_match>* get_Struct_type(const char (&pkg)[pkgl], bool2type<exact_match>)
   {
      return static_cast<SV4recognize<exact_match>*>(get_type(pkg,pkgl-1,StructUtils<Struct>::push(),exact_match));
   }
} }

namespace polymake { namespace perl_bindings {
   template <typename T, typename Struct>
   pm::perl::SV4recognize<pm::identical<T, Struct>::value>* recognize(bait*, T*, GenericStruct<Struct>*)
   {
      return pm::perl::get_Struct_type<Struct>("Polymake::common::Tuple", pm::identical<T, Struct>());
   }

   template <typename Struct>
   SV* field_names(bait*, GenericStruct<Struct>*)
   {
      return pm::perl::StructUtils<Struct>::field_names();
   }

///==== Automatically generated contents follow.    Please do not delete this line. ====
///==== Automatically generated contents end here.  Please do not delete this line. ====
} }

#endif // _POLYMAKE_COMMON_PERL_BINDINGS_GenericStruct_H

// Local Variables:
// mode:C++
// c-basic-offset:3
// End:
