source: cpp/frams/vm/classes/genoobj.cpp @ 534

Last change on this file since 534 was 534, checked in by Maciej Komosinski, 8 years ago

Renamed: get/setGene -> get/setGenes, setGeneOnly -> setGenesAssumingSameFormat

  • Property svn:eol-style set to native
File size: 6.0 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
[138]4
5#include "genoobj.h"
6#include <frams/util/extvalue.h>
7#include <frams/model/autoname.h>
[203]8#include "collectionobj.h"
[138]9
10#define FIELDSTRUCT GenoObj
[420]11ParamEntry geno_paramtab[] =
[138]12{
[420]13        { "Geno", 1, 15, "Geno", "All information about a single genotype.\nThis is a genetics-only object which does not contain any performance data. See also: Genotype class" },
14        { "name", 0, PARAM_NOSTATIC, "Name", "s 0 40", GETSET(name), },
15        { "rawgenotype", 0, PARAM_NOSTATIC | PARAM_READONLY, "Raw genotype", "s 1", GETONLY(genotype), "Genotype, excluding the format specifier" },
16        { "info", 0, PARAM_NOSTATIC, "Info", "s 1", GETSET(info), "Additional information or comments", },
[516]17        { "format", 0, PARAM_NOSTATIC | PARAM_READONLY, "Format", "s", GETONLY(format), "Genotype format", },
[420]18        { "genotype", 0, PARAM_NOSTATIC | PARAM_READONLY, "Genotype", "s 1", GETONLY(string), "Genes as a string of characters", },
19        { "isValid", 0, PARAM_NOSTATIC | PARAM_READONLY | PARAM_DEPRECATED, "Valid", "d 0 1", GETONLY(isvalid), "Use 'is_valid' instead of 'isValid'." },
20        { "is_valid", 0, PARAM_NOSTATIC, "Validity", "d -1 1 -1", GETSET(isvalid),
21        "0 = invalid genotype\n"
22        "1 = valid genotype\n"
23        "-1 = validity is not known. This is a transient state. The value of \"is_valid\" will never be -1 when read. It is safe to treat is_valid as boolean in statements like \"if (g.is_valid) ...\". Setting \"is_valid=-1\" will make it 0 or 1 again. This third state (-1) is only needed for loading Genotype objects from files where the \"is_valid\" field might not be present."
24        },
[516]25        { "getConverted", 0, PARAM_NOSTATIC, "get converted genotype", "p oGeno(s format)", PROCEDURE(p_getconvert), },
[420]26        { "f0genotype", 0, PARAM_NOSTATIC | PARAM_READONLY, "f0 genotype", "s 1", GETONLY(f0genotype), "converted to f0 genotype", },
27        { "new", 0, 0, "create new empty object", "p oGeno()", PROCEDURE(p_new), },
28        { "newFromString", 0, 0, "create new object from supplied string argument", "p oGeno(s genotype)", PROCEDURE(p_newfromstring), },
[516]29        { "newFrom", 0, 0, "create new object", "p oGeno(s genotype,s format,s name,s description)", PROCEDURE(p_newfrom), },
[420]30        { "autoname", 0, PARAM_NOSTATIC | PARAM_READONLY, "Autogenerated name", "s", GETONLY(autoname), },
31        { "toVector", 0, PARAM_READONLY | PARAM_NOSTATIC, "serialization support", "oVector", GETONLY(toVector), },
32        { "newFromVector", 0, 0, "serialization support", "p oGeno(oVector)", PROCEDURE(p_newfromvector), },
33        { 0, 0, 0, },
[138]34};
35#undef FIELDSTRUCT
36
37void GenoObj::get_isvalid(ExtValue *ret)
[420]38{
39        ret->setInt(isValid());
40}
[138]41
[415]42int GenoObj::set_isvalid(const ExtValue *v)
43{
[420]44        paInt n = v->getInt();
45        if (getValid() != n)
[415]46        {
[420]47                setValid(n);
48                return PSET_CHANGED;
[415]49        }
[420]50        return 0;
[415]51}
52
[138]53void GenoObj::get_genotype(ExtValue *ret)
[420]54{
[534]55        ret->setString(getGenes());
[420]56}
[138]57
58void GenoObj::get_name(ExtValue *ret)
[420]59{
60        ret->setString(getName());
61}
[138]62
63void GenoObj::get_autoname(ExtValue *ret)
64{
[420]65        Model m(*this);
66        ret->setString(AutoName::makeName(m));
[138]67}
68
69int GenoObj::set_name(const ExtValue *v)
[420]70{
71        setName(v->getString());
72        return PSET_CHANGED;
73}
[138]74
75void GenoObj::get_info(ExtValue *ret)
[420]76{
77        ret->setString(getComment());
78}
[138]79
80void GenoObj::get_string(ExtValue *ret)
[420]81{
[534]82        ret->setString(getGenesAndFormat());
[420]83}
[138]84
85void GenoObj::get_format(ExtValue *ret)
[420]86{
[516]87        char format_as_string[2]={getFormat(),0};
88        ret->setString(format_as_string);
[420]89}
[138]90
91int GenoObj::set_info(const ExtValue *v)
[420]92{
93        setComment(v->getString());
94        return PSET_CHANGED;
95}
[138]96
97void GenoObj::get_f0genotype(ExtValue *ret)
[420]98{
[534]99        ret->setString(getConverted('0').getGenes());
[420]100}
[138]101
[516]102char GenoObj::formatFromExtValue(ExtValue& v)
103{
104if (v.getType()==TInt)
105        return v.getInt();
106if (v.getType()==TString)
107        {
108        SString s=v.getString();
109        if (s.len()==1)
110                return s.charAt(0);
111        }
112return Geno::INVALID_FORMAT;
113}
114
[420]115void GenoObj::p_getconvert(ExtValue *args, ExtValue *ret)
116{
[516]117        *ret = makeDynamicObjectAndDecRef(new Geno(getConverted(formatFromExtValue(args[0]))));
[420]118}
[138]119
[420]120void GenoObj::p_new(ExtValue *args, ExtValue *ret)
121{
122        *ret = makeDynamicObjectAndDecRef(new Geno());
123}
[138]124
[420]125void GenoObj::p_newfromstring(ExtValue *args, ExtValue *ret)
126{
127        *ret = makeDynamicObjectAndDecRef(new Geno(args[0].getString()));
128}
[138]129
[420]130void GenoObj::p_newfrom(ExtValue *args, ExtValue *ret)
131{
[516]132        *ret = makeDynamicObjectAndDecRef(new Geno(args[3].getString(), formatFromExtValue(args[2]),
[420]133                args[1].getString(), args[0].getString()));
134}
[138]135
136Param& GenoObj::getStaticParam()
137{
138#ifdef __CODEGUARD__
[420]139        static GenoObj static_genoobj;
140        static Param static_genoparam(geno_paramtab,&static_genoobj);
[138]141#else
[420]142        static Param static_genoparam(geno_paramtab);
[138]143#endif
[420]144        return static_genoparam;
[138]145}
146
147Param& GenoObj::getDynamicParam()
148{
[420]149        static Param dynamic_genoparam(geno_paramtab);
150        return dynamic_genoparam;
[138]151}
152
[420]153ParamInterface* GenoObj::getInterface() { return &getStaticParam(); }
[138]154
155ExtObject GenoObj::makeStaticObject(Geno* g)
[420]156{
157        return ExtObject(&getStaticParam(), (void*)g);
158}
[138]159
160ExtObject GenoObj::makeDynamicObject(Geno* g)
[420]161{
162        return ExtObject(&getDynamicParam(), (DestrBase*)g);
163}
[138]164
165ExtObject GenoObj::makeDynamicObjectAndDecRef(Geno* g)
166{
[420]167        const ExtObject& o = makeDynamicObject(g);
168        g->decref();
169        return o;
[138]170}
171
[171]172Geno* GenoObj::fromObject(const ExtValue& v, bool warn)
[138]173{
[420]174        return (Geno*)v.getObjectTarget(getStaticParam().getName(), warn);
[138]175}
[203]176
177void GenoObj::get_toVector(ExtValue *ret)
178{
[420]179        VectorObject *vec = new VectorObject;
[534]180        vec->data += new ExtValue(getGenesAndFormat());
[420]181        vec->data += new ExtValue(getName());
182        vec->data += new ExtValue(getComment());
183        ret->setObject(ExtObject(&VectorObject::par, vec));
[203]184}
185
[420]186void GenoObj::p_newfromvector(ExtValue *args, ExtValue *ret)
[203]187{
[420]188        VectorObject *vec = VectorObject::fromObject(args->getObject());
189        if (vec && (vec->data.size() >= 3))
[203]190        {
[420]191                SString g = vec->get(0) ? vec->get(0)->getString() : SString::empty();
192                SString n = vec->get(1) ? vec->get(1)->getString() : SString::empty();
193                SString c = vec->get(2) ? vec->get(2)->getString() : SString::empty();
194                *ret = makeDynamicObjectAndDecRef(new Geno(g, -1, n, c));
[203]195        }
[420]196        else
197                ret->setEmpty();
[203]198}
[222]199
200/////////////
201
202REGISTER_DESERIALIZABLE(GenoObj)
Note: See TracBrowser for help on using the repository browser.