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

Last change on this file since 420 was 420, checked in by Maciej Komosinski, 9 years ago

Improved docs and code formatting

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
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.
4
5#include "genoobj.h"
6#include <frams/util/extvalue.h>
7#include <frams/model/autoname.h>
8#include "collectionobj.h"
9
10#define FIELDSTRUCT GenoObj
11ParamEntry geno_paramtab[] =
12{
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", },
17        { "format", 0, PARAM_NOSTATIC | PARAM_READONLY, "Format", "d", GETONLY(format), "Genotype format", },
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        },
25        { "getConverted", 0, PARAM_NOSTATIC, "get converted genotype", "p oGeno(d format)", PROCEDURE(p_getconvert), },
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), },
29        { "newFrom", 0, 0, "create new object", "p oGeno(s genotype,d format,s name,s description)", PROCEDURE(p_newfrom), },
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, },
34};
35#undef FIELDSTRUCT
36
37void GenoObj::get_isvalid(ExtValue *ret)
38{
39        ret->setInt(isValid());
40}
41
42int GenoObj::set_isvalid(const ExtValue *v)
43{
44        paInt n = v->getInt();
45        if (getValid() != n)
46        {
47                setValid(n);
48                return PSET_CHANGED;
49        }
50        return 0;
51}
52
53void GenoObj::get_genotype(ExtValue *ret)
54{
55        ret->setString(getGene());
56}
57
58void GenoObj::get_name(ExtValue *ret)
59{
60        ret->setString(getName());
61}
62
63void GenoObj::get_autoname(ExtValue *ret)
64{
65        Model m(*this);
66        ret->setString(AutoName::makeName(m));
67}
68
69int GenoObj::set_name(const ExtValue *v)
70{
71        setName(v->getString());
72        return PSET_CHANGED;
73}
74
75void GenoObj::get_info(ExtValue *ret)
76{
77        ret->setString(getComment());
78}
79
80void GenoObj::get_string(ExtValue *ret)
81{
82        ret->setString(shortString());
83}
84
85void GenoObj::get_format(ExtValue *ret)
86{
87        ret->setInt(getFormat());
88}
89
90int GenoObj::set_info(const ExtValue *v)
91{
92        setComment(v->getString());
93        return PSET_CHANGED;
94}
95
96void GenoObj::get_f0genotype(ExtValue *ret)
97{
98        ret->setString(getConverted('0').getGene());
99}
100
101void GenoObj::p_getconvert(ExtValue *args, ExtValue *ret)
102{
103        *ret = makeDynamicObjectAndDecRef(new Geno(getConverted((char)args[0].getInt())));
104}
105
106void GenoObj::p_new(ExtValue *args, ExtValue *ret)
107{
108        *ret = makeDynamicObjectAndDecRef(new Geno());
109}
110
111void GenoObj::p_newfromstring(ExtValue *args, ExtValue *ret)
112{
113        *ret = makeDynamicObjectAndDecRef(new Geno(args[0].getString()));
114}
115
116void GenoObj::p_newfrom(ExtValue *args, ExtValue *ret)
117{
118        *ret = makeDynamicObjectAndDecRef(new Geno(args[3].getString(), (char)args[2].getInt(),
119                args[1].getString(), args[0].getString()));
120}
121
122Param& GenoObj::getStaticParam()
123{
124#ifdef __CODEGUARD__
125        static GenoObj static_genoobj;
126        static Param static_genoparam(geno_paramtab,&static_genoobj);
127#else
128        static Param static_genoparam(geno_paramtab);
129#endif
130        return static_genoparam;
131}
132
133Param& GenoObj::getDynamicParam()
134{
135        static Param dynamic_genoparam(geno_paramtab);
136        return dynamic_genoparam;
137}
138
139ParamInterface* GenoObj::getInterface() { return &getStaticParam(); }
140
141ExtObject GenoObj::makeStaticObject(Geno* g)
142{
143        return ExtObject(&getStaticParam(), (void*)g);
144}
145
146ExtObject GenoObj::makeDynamicObject(Geno* g)
147{
148        return ExtObject(&getDynamicParam(), (DestrBase*)g);
149}
150
151ExtObject GenoObj::makeDynamicObjectAndDecRef(Geno* g)
152{
153        const ExtObject& o = makeDynamicObject(g);
154        g->decref();
155        return o;
156}
157
158Geno* GenoObj::fromObject(const ExtValue& v, bool warn)
159{
160        return (Geno*)v.getObjectTarget(getStaticParam().getName(), warn);
161}
162
163void GenoObj::get_toVector(ExtValue *ret)
164{
165        VectorObject *vec = new VectorObject;
166        vec->data += new ExtValue(shortString());
167        vec->data += new ExtValue(getName());
168        vec->data += new ExtValue(getComment());
169        ret->setObject(ExtObject(&VectorObject::par, vec));
170}
171
172void GenoObj::p_newfromvector(ExtValue *args, ExtValue *ret)
173{
174        VectorObject *vec = VectorObject::fromObject(args->getObject());
175        if (vec && (vec->data.size() >= 3))
176        {
177                SString g = vec->get(0) ? vec->get(0)->getString() : SString::empty();
178                SString n = vec->get(1) ? vec->get(1)->getString() : SString::empty();
179                SString c = vec->get(2) ? vec->get(2)->getString() : SString::empty();
180                *ret = makeDynamicObjectAndDecRef(new Geno(g, -1, n, c));
181        }
182        else
183                ret->setEmpty();
184}
185
186/////////////
187
188REGISTER_DESERIALIZABLE(GenoObj)
Note: See TracBrowser for help on using the repository browser.