source: cpp/frams/neuro/neurofactory.cpp @ 1296

Last change on this file since 1296 was 973, checked in by Maciej Komosinski, 4 years ago

Increased SString and std::string compatibility: introduced length(), size(), and capacity(), and removed legacy methods that have std::string equivalents

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "neurofactory.h"
6#include <frams/util/sstring.h>
7#include <frams/param/param.h>
8#include "neuroimpl.h"
9#include "neurolibrary.h"
10
11#include NEURO_IMPL_FILES
12
13NeuroImpl* NeuroFactory::getImplementation(NeuroClass *nc)
14{
15        if (nc != NULL)
16        {
17                std::map<NeuroClass*, NeuroImpl*>::iterator it = impl.find(nc);
18                if (it != impl.end())
19                        return it->second;
20        }
21        return NULL;
22}
23
24NeuroImpl* NeuroFactory::createNeuroImpl(NeuroClass *nc, Model::ShapeType shape_type)
25{
26        if (!nc) return 0;
27        if (!nc->active) return 0;
28        if (!(nc->isShapeTypeSupported(shape_type)))
29        {
30                logPrintf("NeuroFactory", "createNeuroImpl", LOG_WARN,
31                        "Neuro class '%s' does not support shape type '%d'", nc->name.c_str(), (int)shape_type);
32                return NULL;
33        }
34        NeuroImpl* ni = getImplementation(nc);
35        if (!ni) return 0;
36        ni = ni->makeNew();
37        if (ni) ni->neuroclass = nc;
38        return ni;
39}
40
41NeuroImpl* NeuroFactory::setImplementation(const SString& classname, NeuroImpl *ni, bool deleteold)
42{
43        NeuroClass *nc = Neuro::getClass(classname);
44        if (!nc) return ni;
45        return setImplementation(nc, ni, deleteold);
46}
47
48NeuroImpl* NeuroFactory::setImplementation(NeuroClass *nc, NeuroImpl *ni, bool deleteold)
49{
50        if (nc == NULL) return NULL;
51        std::map<NeuroClass*, NeuroImpl*>::iterator it = impl.find(nc);
52        NeuroImpl* old_ni = NULL;
53        if (it == impl.end())
54        {
55                if (ni != NULL)
56                {
57                        impl[nc] = ni;
58                        nc->impl_count++;
59                }
60                return NULL;
61        }
62        else
63        {
64                old_ni = it->second;
65                if (ni)
66                        it->second = ni;
67                else
68                {
69                        impl.erase(it);
70                        nc->impl_count--;
71                }
72        }
73        if (deleteold && old_ni) delete old_ni;
74        return old_ni;
75}
76
77#include NEURO_CLS_FACTORY
78
79void NeuroFactory::setStandardImplementation()
80{
81        SETIMPLEMENTATION
82}
83
84void NeuroFactory::freeImplementation()
85{
86        for (int i = 0; i < Neuro::getClassCount(); i++)
87                setImplementation(Neuro::getClass(i), 0);
88}
89
90void NeuroFactory::removeUnimplemented()
91{
92        SString removed;
93        for (int i = 0; i < Neuro::getClassCount(); i++)
94        {
95                NeuroClass *nc = Neuro::getClass(i);
96                if (nc->impl_count == 0)
97                {
98                        removed += nc->getName();
99                        removed += " ";
100                        NeuroLibrary::staticlibrary.classes -= i;
101                        i--;
102                        delete nc;
103                }
104        }
105        if (removed.length())
106                logPrintf("NeuroFactory", "removeUninmplemented", LOG_INFO,
107                        "Removed Neuro classes: %s", removed.c_str());
108}
Note: See TracBrowser for help on using the repository browser.