source: cpp/frams/genetics/genoconv.h @ 1288

Last change on this file since 1288 was 1256, checked in by Maciej Komosinski, 17 months ago
  • reasonable field names for enabling converters
  • automatically attach converter Param to GenoConvParam?
  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#ifndef _GENCONV_H_
6#define _GENCONV_H_
7
8#include "geno.h"
9#include <frams/param/param.h>
10#include <frams/param/paramlist.h>
11#include <frams/util/list.h>
12#include <frams/util/sstring.h>
13
14#include <string>
15#include <vector>
16
17
18class GenoConvManager;
19
20class GenoConvParam : public Param
21{
22        GenoConvManager *gcm;
23        std::vector<std::string> gcnames; //stores names of converters so that these names persist and pointers to these names can be safely used externally
24        char tmp_id[20]; //id() always returns a pointer to this array, filled with the appropriate field id. The root of the problem is that Param functions return char pointers having unspecified lifetime. In contrast, name() returns individual preallocated pointers for individual items, but apparently, id() was too simple to deserve a better but more tedious implementation. Param api will not change, but what could be improved here is perhaps using ParamObject's dynamic paramtab (which did not exist when GenoConvParam was first implemented) so we don't have to care about individual pointers at all.
25        void freetab();
26public:
27        GenoConvParam(GenoConvManager *g);
28        ~GenoConvParam();
29        void *getTarget(int);
30        const char* id(int i);
31        void updatetab();
32};
33
34class MultiMap;
35
36/// Base class for all Geno Converters.
37/// In constructor you have to set public fields indicating your identity and supported formats.
38/// Each converter serves one in-out format pair.
39/// The instance of your converter should be registered in GenoConvManager.
40class GenoConverter
41{
42public:
43        const char *name;       //< converter name (short)
44        SString in_format,      //< input format, e.g. "1"
45                out_format;     //< output format, e.g. "0"
46        paInt enabled;  //< don't touch this! (used by configuration module)
47        paInt mapsupport; //< set to 1 if your converter supports genotype mapping
48
49        /// You have to reimplement this method.
50        /// If your converter cannot do its job, return empty string - return SString();
51        /// Any other return value is assumed to be output genotype.
52        /// @param map if not null, mapping informaton is requested, converter should add conversion map to this object
53        virtual SString convert(SString &i, MultiMap *map, bool using_checkpoints) = 0;
54
55        /// genoconverter enable/disable fields are named "genoconv_id", where id="f"+input_format+"_f"+output_format by default.
56        /// Converters should implement id() and provide unique value - required when adding more than one converter operating on the same formats.
57        virtual SString id() { return SString("f") + in_format + SString("_f") + out_format; }
58
59        /// Optional user-settable configuration for this converter which automatically gets added to GenoConvParam
60        virtual ParamInterface* getParam() { return NULL; }
61
62        virtual ~GenoConverter() {}
63        /// Don't forget to set public fields in your constructor
64        GenoConverter() :name(""), in_format(Geno::FORMAT_UNKNOWN), out_format("0"), enabled(1), mapsupport(0) {}
65};
66
67/// This class gathers abilities of all converters and can convert a genotype to any other one, provided there is
68/// a path of GenoConverters between them.
69/// In most cases you don't use this class directly, Geno::getConverted(int) provides full converting functionality.
70/// Explicit GenoConvManager object is only needed for registering your GenoConverter.
71/// Use DefaultGenoConvManager to register the standard genotype converters automatically.
72class GenoConvManager
73{
74        friend class GenoConvParam;
75        SList converters;
76public:
77        GenoConvManager();
78        ~GenoConvManager();
79        class GenoConvParam conv_enabling_param;
80        ParamList param;
81        /// make a genotype in other format. genotype will be invalid
82        /// if GenoConvManager cannot convert it.
83        Geno convert(Geno &in, SString format_list, MultiMap *map = 0, bool using_checkpoints = false, bool *converter_missing = NULL);
84        /// register GenoConverter, the added object will be automatically deleted when GenoConvManager is destructed (call removeConverter() if this is not desirable)
85        void addConverter(GenoConverter *conv);
86        /// unregister GenoConverter
87        void removeConverter(GenoConverter *conv);
88
89        GenoConverter **getPath(const SString& in, const SString& out, GenoConverter **path, int maxlen, int *mapavailable = 0);
90        /// returns the list of converters meeting the specified criteria
91        /// pass result=0 if you only need one result (by return value)
92        /// default criteria values mean "don't care", pass anything else to narrow your search
93        GenoConverter *findConverters(SListTempl<GenoConverter*>* result = 0, const SString& in = Geno::FORMAT_UNKNOWN, const SString& out = Geno::FORMAT_UNKNOWN, int enabled = -1, char* name = 0);
94};
95
96#endif
Note: See TracBrowser for help on using the repository browser.