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 "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 | |
---|
13 | NeuroImpl* 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 | |
---|
24 | NeuroImpl* NeuroFactory::createNeuroImpl(NeuroClass *nc) |
---|
25 | { |
---|
26 | if (!nc) return 0; |
---|
27 | if (!nc->active) return 0; |
---|
28 | NeuroImpl* ni = getImplementation(nc); |
---|
29 | if (!ni) return 0; |
---|
30 | ni = ni->makeNew(); |
---|
31 | if (ni) ni->neuroclass = nc; |
---|
32 | return ni; |
---|
33 | } |
---|
34 | |
---|
35 | NeuroImpl* NeuroFactory::setImplementation(const SString& classname, NeuroImpl *ni, bool deleteold) |
---|
36 | { |
---|
37 | NeuroClass *nc = Neuro::getClass(classname); |
---|
38 | if (!nc) return ni; |
---|
39 | return setImplementation(nc, ni, deleteold); |
---|
40 | } |
---|
41 | |
---|
42 | NeuroImpl* NeuroFactory::setImplementation(NeuroClass *nc, NeuroImpl *ni, bool deleteold) |
---|
43 | { |
---|
44 | if (nc == NULL) return NULL; |
---|
45 | std::map<NeuroClass*, NeuroImpl*>::iterator it = impl.find(nc); |
---|
46 | NeuroImpl* old_ni = NULL; |
---|
47 | if (it == impl.end()) |
---|
48 | { |
---|
49 | if (ni != NULL) |
---|
50 | { |
---|
51 | impl[nc] = ni; |
---|
52 | nc->impl_count++; |
---|
53 | } |
---|
54 | return NULL; |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | old_ni = it->second; |
---|
59 | if (ni) |
---|
60 | it->second = ni; |
---|
61 | else |
---|
62 | { |
---|
63 | impl.erase(it); |
---|
64 | nc->impl_count--; |
---|
65 | } |
---|
66 | } |
---|
67 | if (deleteold && old_ni) delete old_ni; |
---|
68 | return old_ni; |
---|
69 | } |
---|
70 | |
---|
71 | #include NEURO_CLS_FACTORY |
---|
72 | |
---|
73 | void NeuroFactory::setStandardImplementation() |
---|
74 | { |
---|
75 | SETIMPLEMENTATION |
---|
76 | } |
---|
77 | |
---|
78 | void NeuroFactory::freeImplementation() |
---|
79 | { |
---|
80 | for (int i = 0; i < Neuro::getClassCount(); i++) |
---|
81 | setImplementation(Neuro::getClass(i), 0); |
---|
82 | } |
---|
83 | |
---|
84 | void NeuroFactory::removeUnimplemented() |
---|
85 | { |
---|
86 | SString removed; |
---|
87 | for (int i = 0; i < Neuro::getClassCount(); i++) |
---|
88 | { |
---|
89 | NeuroClass *nc = Neuro::getClass(i); |
---|
90 | if (nc->impl_count == 0) |
---|
91 | { |
---|
92 | removed += nc->getName(); |
---|
93 | removed += " "; |
---|
94 | NeuroLibrary::staticlibrary.classes -= i; |
---|
95 | i--; |
---|
96 | delete nc; |
---|
97 | } |
---|
98 | } |
---|
99 | if (removed.len()) |
---|
100 | logPrintf("NeuroFactory", "removeUninmplemented", LOG_INFO, |
---|
101 | "Removed Neuro classes: %s", removed.c_str()); |
---|
102 | } |
---|