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

Last change on this file since 1317 was 1256, checked in by Maciej Komosinski, 18 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
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[1256]2// Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
[286]3// See LICENSE.txt for details.
[109]4
5#ifndef _GENCONV_H_
6#define _GENCONV_H_
7
8#include "geno.h"
9#include <frams/param/param.h>
[1256]10#include <frams/param/paramlist.h>
[109]11#include <frams/util/list.h>
12#include <frams/util/sstring.h>
13
14#include <string>
15#include <vector>
16
17
18class GenoConvManager;
19
[150]20class GenoConvParam : public Param
[109]21{
[150]22        GenoConvManager *gcm;
[783]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
[1256]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.
[150]25        void freetab();
[109]26public:
[150]27        GenoConvParam(GenoConvManager *g);
28        ~GenoConvParam();
29        void *getTarget(int);
30        const char* id(int i);
31        void updatetab();
[109]32};
33
34class MultiMap;
35
36/// Base class for all Geno Converters.
[999]37/// In constructor you have to set public fields indicating your identity and supported formats.
[109]38/// Each converter serves one in-out format pair.
[999]39/// The instance of your converter should be registered in GenoConvManager.
[109]40class GenoConverter
41{
42public:
[150]43        const char *name;       //< converter name (short)
[999]44        SString in_format,      //< input format, e.g. "1"
45                out_format;     //< output format, e.g. "0"
[247]46        paInt enabled;  //< don't touch this! (used by configuration module)
47        paInt mapsupport; //< set to 1 if your converter supports genotype mapping
[109]48
[150]49        /// You have to reimplement this method.
[513]50        /// If your converter cannot do its job, return empty string - return SString();
51        /// Any other return value is assumed to be output genotype.
[150]52        /// @param map if not null, mapping informaton is requested, converter should add conversion map to this object
[735]53        virtual SString convert(SString &i, MultiMap *map, bool using_checkpoints) = 0;
[109]54
[1256]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
[150]62        virtual ~GenoConverter() {}
63        /// Don't forget to set public fields in your constructor
[988]64        GenoConverter() :name(""), in_format(Geno::FORMAT_UNKNOWN), out_format("0"), enabled(1), mapsupport(0) {}
[109]65};
66
[1256]67/// This class gathers abilities of all converters and can convert a genotype to any other one, provided there is
[109]68/// a path of GenoConverters between them.
[1256]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.
[109]71/// Use DefaultGenoConvManager to register the standard genotype converters automatically.
72class GenoConvManager
73{
[150]74        friend class GenoConvParam;
75        SList converters;
[109]76public:
[150]77        GenoConvManager();
78        ~GenoConvManager();
[1256]79        class GenoConvParam conv_enabling_param;
80        ParamList param;
[150]81        /// make a genotype in other format. genotype will be invalid
82        /// if GenoConvManager cannot convert it.
[972]83        Geno convert(Geno &in, SString format_list, MultiMap *map = 0, bool using_checkpoints = false, bool *converter_missing = NULL);
[150]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);
[109]88
[955]89        GenoConverter **getPath(const SString& in, const SString& out, GenoConverter **path, int maxlen, int *mapavailable = 0);
[150]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
[988]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);
[109]94};
95
96#endif
Note: See TracBrowser for help on using the repository browser.