1 /* "Species" - a CoreWars evolver. Copyright (C) 2003 'Varfar'
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 1, or (at your option) any later
6 * version.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
17
18 /* to implement a generator:
19 * 1) derive publically from COperand
20 * 1) give it an identifier in the EImpl enumeration
21 * 2) make the constructor set the _type member
22 * 3) make COperand factories understand this new implementation and how to identify it
23 * 4) implement the read_ini/write_ini functionality
24 */
25
26 #include "operand_distribution.hpp"
27 #include "error.hpp"
28
29 /********** COperandDistribution implementation ******************************/
30
COperandDistribution(const field_t coresize)31 COperandDistribution::COperandDistribution(const field_t coresize):
32 COperand(coresize), CDistribution("operands",0,_coresize) {
33 _type = DISTRIBUTION;
34 }
35
COperandDistribution(const CDistribution & copy)36 COperandDistribution::COperandDistribution(const CDistribution ©):
37 CDistribution(copy), COperand(CDistribution::max()) {
38 _type = DISTRIBUTION;
39 }
40
ImplFactory(const CDistribution & copy)41 CDistribution *COperandDistribution::ImplFactory(const CDistribution ©) {
42 return new COperandDistribution(copy);
43 }
44
45
46 // actual query point
rnd(insn_t & instruction,const Field field)47 void COperandDistribution::rnd(insn_t &instruction,const Field field) {
48 switch(field) {
49 case A:
50 instruction.a = CDistribution::rnd();
51 break;
52 case B:
53 instruction.b = CDistribution::rnd();
54 break;
55 default:
56 PANIC(MISC,"invalid field in COperandDistribution::rnd",NULL);
57 }
58 }
59
write_ini(std::ostream & os)60 void COperandDistribution::write_ini(std::ostream &os) {
61 os << type_key() << "=" << impl_desc(type()) << std::endl;
62 CDistribution::write_ini(os);
63 }
64
write_override_ini(std::ostream & os,const COperand * parent)65 void COperandDistribution::write_override_ini(std::ostream &os,const COperand *parent) {
66 if(parent == this) // nothing to do?
67 return;
68 os << type_key() << "=" << impl_desc(type()) << std::endl;
69 if(same_type(this,parent)) {
70 CDistribution::write_override_ini(os,(CDistribution*)parent);
71 } else {
72 CDistribution::write_ini(os);
73 }
74 }
75
read_ini_impl(INIFile & ini)76 void COperandDistribution::read_ini_impl(INIFile &ini) {
77 CDistribution::read_ini(ini);
78 }
79
read_override_ini_impl(INIFile & ini)80 COperand *COperandDistribution::read_override_ini_impl(INIFile &ini) {
81 return (COperand*)CDistribution::read_override_ini(ini,ImplFactory);
82 }
83