/*
 * Make c++ code for variant classes for event selected messages
 *
 * Copyright (c) 1994, 1995, 1996 Gunther Schadow.  All rights reserved.
 *
 * 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 of the License, or
 * (at your option) any later version.
 * 
 * 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

:- module(mkc_var,[mkc_var/3]).

:- style_check(+string).

:- use_module(desc-files).
:- use_module(desc-names).
:- use_module(db-inter).
:- use_module(mkc-print).
:- use_module(mkc-grp).
:- use_module(mkc-msg).

mkc_var(FunArea, Vn, DefL) :-
	file(variant,Vn,Fnh),

	%groups(DefL,GrpL,DefLG),
	groups(DefL,DefLG), GrpL = [],	
	memo_incl(DefLG, GrpL),

	build_vdecl(DefLG,DecL),
	memo_repstruc(Vn,DecL),

	build_vdecl([evn|DefLG],DecL_evn),

	htell(Fnh,Def,message),

	nl,
        main_package(P), package(P,_,_,R,_),
	format("#include <~wEvnMessage.h>~n", [R]),

	comment('MESSAGE',Vn,FunArea,'a variant of an event selected message'),

	print_incl,

	class_begin(Vn, 'EvnMessage'),

	( length(GrpL,0)
	  -> true;
	     (
	     	ppublic,
		mkc_grps_h(GrpL, Vn),
		format("/*~n * Resume ~w~n */", Vn),
		pprivate
	     )
	),

	typedef_groups(DefLG),

	print_vdecl(DecL),

	/* itemtab */
	length(DecL_evn, NOItems),
	itemtab_h(DecL_evn),

	ppublic,

	/* ctor */

	format("~n~w(UniMesIdCode::Value);~n", Vn),

	/* get */

	forall(
	(
	    member([Type,_,Name,_,_], DecL),
	    Type \= '// void'
	),
	format("~nconst ~w& get~w() const;", [Type, Name])),

	/* set */
    
        nl,
	forall(
	(
	    member([Type,_,Name,_,_], DecL),
	    Type \= '// void'
	), 
	format("~nvoid set~w(const ~w &x);", [Name, Type])),
	class_end,
	iccinclude(Fnh),
	htold(Def),

	cctell(Fnh,message),

	nl,
	format("#include ~'EvnMessage.h~'~n"),
	format("#include ~'UniMesIdCode.h~'~n"),

	comment('VARIANT',Vn,FunArea,'a variant of an event selected message'),

	/* print_incl(DepL) ?? */

        /* itemtab */
        itemtab_cc(Vn, DecL_evn, [evn|DefLG], GrpL),

	/* ctor */

	format("~n~n~w:~/:~w(UniMesIdCode::Value type)~n", [Vn, Vn]),
	format(": EvnMessage(type, ~w, itemtab)~<~n", [NOItems]),
	       
	init(DecL),
	nl,

	(
	    length(GrpL,0) ->
	    true;
	    mkc_grps_cc(GrpL, Vn)
	),
	cctold,
	icctell(Fnh, message),

	/* get */

        forall(
	(
	    member([Type,_,Name,_,_], DecL),
	    Type \= '// void'
	),
	(
	    %scopetype(Vn,Type,ScopeType),
	    ScopeType = Type,
	    format("~ninline~n"),
	    format("const ~w& ~w::get~w() const~n",
	           [ScopeType, Vn, Name]),
	    format("{~n"),	    
	    format("  return ~w;~n", Name),
	    format("}~n")
	)),

	/* set */
    
	nl,
        forall(
	(
	    member([Type,_,Name,_,_], DecL),
	    Type \= '// void'
	),
	(
	    format("~ninline~n"),
	    format("void ~w::set~w(const ~w &x)~n", [Vn,Name,Type]),
	    format("{~>~n"),	    
	    format("~w = x;~n", [Name]),
	    format("set();"),
	    format("~<~n}~n")
	)),

	(
	    length(GrpL,0) ->
	    true;
	    mkc_grps_icc(GrpL, Vn)
	),
	nl,
	icctold,

        vcgtell,

% node

        format("node: {~>~ntitle: ~'~w~'~n", [Vn]),
        format("color: orange~n"),
        format("level: 1~<~n}~n"),

% edges
  
        forall(member([Type,_,_,_,Name], DecL),
        (
          format("edge: {~>~n"),
          uppercase(Name,UName), 
          ( ( concat("repstruc<",Tp0,Type), concat(Tp1,">",Tp0) )
            -> format("label: ~'~w(r)~'~n", [UName]);
               ( Type = 'repANYseg'
                -> ( Tp1 = 'ANYseg',
                     format("label: ~'~w(r)~'~n", [UName]) );
                   ( format("label: ~'~w~'~n", [UName]),
                     Tp1 = Type ) ) ),
          format("sourcename: ~'~w~'~n", [Vn]),
          format("targetname: ~'~w~'~<~n}~n", [Tp1])
        )),

        vcgtold.


