Writing - finished to the printed version.
[fic.git] / modules.cpp
blobe422dd4afae92f6ddf550376c771d06d66a8a533
1 #include <typeinfo> // needed for FerrisLoki/DataGenerators.h
3 #include "headers.h"
5 #include "modules/root.h"
6 #include "modules/colorModel.h"
7 #include "modules/squarePixels.h"
8 #include "modules/quadTree.h"
9 #include "modules/stdDomains.h"
10 #include "modules/quality2SE.h"
11 #include "modules/stdEncoder.h"
12 #include "modules/vliCodec.h"
13 #include "modules/saupePredictor.h"
14 #include "modules/noPredictor.h"
16 #include "fileUtil.h"
18 #include "FerrisLoki/DataGenerators.h"
20 using namespace std;
22 typedef Loki::TL::MakeTypelist< MRoot, MColorModel, MSquarePixels, MQuadTree, MStdDomains
23 , MQuality2SE_std, MStdEncoder, MDifferentialVLICodec, MSaupePredictor, MNoPredictor
24 , MQuality2SE_alt >
25 ::Result Modules;
27 const int powers[31]= { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2*1024 /* 2^11 */
28 , 4*1024, 8*1024, 16*1024, 32*1024, 64*1024, 128*1024, 256*1024, 512*1024 /* 2^19 */
29 , 1024*1024, 2*1024*1024, 4*1024*1024, 8*1024*1024, 16*1024*1024, 32*1024*1024 /* 2^25 */
30 , 64*1024*1024, 128*1024*1024, 256*1024*1024, 512*1024*1024, 1024*1024*1024 }; /* 2^30 */
33 const bool UpdateInfo::noTerminate;
34 const UpdateInfo UpdateInfo::none= UpdateInfo
35 ( UpdateInfo::noTerminate, &UpdateInfo::emptyFunction, &UpdateInfo::emptyFunction );
38 namespace NOSPACE {
39 using namespace Loki;
40 using namespace Loki::TL;
42 /** Compatible<TypeList,Iface> struct template - leaves in the TypeList only derivates
43 * of Iface class parameter - used by Inteface<Iface>, hidden for others */
44 template <class Typelist,class Interface>
45 struct Compatible;
47 template <class Iface>
48 struct Compatible<NullType,Iface> {
49 typedef NullType Result;
52 template <class Head,class Tail,class Iface>
53 struct Compatible< Typelist<Head,Tail> , Iface > {
54 typedef typename Select
55 <SuperSubclass<Iface,Head>::value
56 ,Typelist< Head , typename Compatible<Tail,Iface>::Result >
57 ,typename Compatible<Tail,Iface>::Result
58 >::Result Result;
61 template <class T> struct ExtractId {
62 int operator()() const
63 { return Loki::TL::IndexOf<Modules,T>::value; }
66 template<class Iface> const vector<int>& Interface<Iface>::getCompMods() {
67 using namespace Loki::TL;
68 typedef typename Compatible<Modules,Iface>::Result CompList;
69 // if compMods_ vector is empty, create it and fill it
70 if ( compMods_.empty() && Length<CompList>::value ) {
71 compMods_.reserve(Length<CompList>::value);
72 IterateTypes<CompList,ExtractId> gendata;
73 gendata( back_inserter(compMods_) );
75 return compMods_;
77 template <class Iface> std::vector<int> Interface<Iface>::compMods_;
80 //// Module class members
81 const Module::SettingTypeItem Module::SettingTypeItem
82 ::stopper= { 0, 0, {Stop,{i:-1},{text:0}} };
84 Module::SettingItem* Module::copySettings(CloneMethod method) const {
85 ASSERT( method==DeepCopy || method==ShallowCopy );
86 // copy the settings array
87 int length= info().setLength;
88 if (!length)
89 return 0;
90 SettingItem *result= new SettingItem[length];
91 copy( settings, settings+length, result );
93 SettingItem *item= result, *itemEnd= result+length;
94 if (method==DeepCopy)
95 // make child modules copy themselves (deeply again)
96 while (item!=itemEnd) {
97 if (item->m)
98 item->m= item->m->abstractClone(DeepCopy);
99 ++item;
101 else // ShallowCopy
102 // null module links
103 while (item!=itemEnd) {
104 item->m= 0;
105 ++item;
108 return result;
110 void Module::initDefaultModuleLinks() {
111 // iterate over all settings
112 const SettingTypeItem *setType= info().setType;
113 for (int i=0; setType[i].type.type!=Stop; ++i )
114 if (setType[i].type.type==ModuleCombo) {
115 // it is a module link -> initialize it with the right prototype
116 ASSERT(!settings[i].m);
117 settings[i].m= constCast(&ModuleFactory::prototype(
118 (*setType[i].type.data.compatIDs)[settings[i].val.i]
122 void Module::nullModuleLinks() {
123 // iterate over all settings
124 SettingItem *itEnd= settings+info().setLength;
125 for (SettingItem *it=settings; it!=itEnd; ++it)
126 it->m= 0;
128 template<class M> M* Module::concreteClone(CloneMethod method) const {
129 ASSERT( this && info().id == ModuleFactory::getModuleID<M>() );
130 // create a new instance of the same type and fill its settings with a copy of mine
131 M *result= new M;
132 result->settings= copySettings(method);
133 return result;
136 void Module::createDefaultSettings() {
137 ASSERT(!settings);
138 const TypeInfo &inf= info();
139 settings= new SettingItem[inf.setLength];
140 copy( inf.setType, inf.setType+inf.setLength, settings );
143 void Module::file_saveModuleType( ostream &os, int which ) {
144 // do some assertions - we expect to have the child module, etc.
145 ASSERT( which>=0 && which<info().setLength );
146 SettingItem &setItem= settings[which];
147 ASSERT( info().setType[which].type.type==ModuleCombo && setItem.m );
148 // put the module's identifier
149 put<Uchar>( os, setItem.m->info().id );
151 void Module::file_loadModuleType( istream &is, int which ) {
152 // do some assertions - we expect not to have the child module, etc.
153 ASSERT( which>=0 && which<info().setLength );
154 const SettingTypeItem &setType= info().setType[which];
155 SettingItem &setItem= settings[which];
156 ASSERT( setType.type.type==ModuleCombo && !setItem.m );
157 // get module identifier and check its existence
158 int newId= get<Uchar>(is);
159 checkThrow( 0<=newId && newId<Loki::TL::Length<Modules>::value );
160 // check module compatibility
161 const vector<int> &v= *setType.type.data.compatIDs;
162 settings[which].val.i= find(v.begin(),v.end(),newId) - v.begin();
163 checkThrow( settings[which].val.i < (int)v.size() );
164 // create a new correct empty module
165 setItem.m= ModuleFactory::newModule(newId,ShallowCopy);
168 void Module::file_saveAllSettings(std::ostream &stream) {
169 int setLength= info().setLength;
170 if (!setLength)
171 return;
172 else
173 ASSERT( settings && setLength>0 );
175 const SettingTypeItem *setType= info().setType;
176 for (int i=0; i<setLength; ++i)
177 switch(setType[i].type.type) {
178 case Int:
179 case IntLog2:
180 case Combo:
181 put<Uint32>( stream, settings[i].val.i );
182 break;
183 case Float:
184 put<float>( stream, settings[i].val.f );
185 break;
186 case ModuleCombo:
187 file_saveModuleType( stream, i );
188 settings[i].m->file_saveAllSettings(stream);
189 break;
190 default:
191 ASSERT(false);
192 } // switch
195 void Module::file_loadAllSettings(std::istream &stream) {
196 int setLength= info().setLength;
197 ASSERT(setLength>=0);
198 if (!setLength)
199 return;
200 if (!settings)
201 settings= new SettingItem[setLength];
203 const SettingTypeItem *setType= info().setType;
204 for (int i=0; i<setLength; ++i)
205 switch(setType[i].type.type) {
206 case Int:
207 case IntLog2:
208 case Combo:
209 settings[i].val.i= get<Uint32>(stream);
210 break;
211 case Float:
212 settings[i].val.f= get<float>(stream);
213 break;
214 case ModuleCombo:
215 file_loadModuleType( stream, i );
216 settings[i].m->file_loadAllSettings(stream);
217 break;
218 default:
219 ASSERT(false);
220 } // switch
223 //// ModuleFactory class members
224 ModuleFactory* ModuleFactory::instance=0;
226 template<class M> int ModuleFactory::getModuleID()
227 { return Loki::TL::IndexOf<Modules,M>::value; }
230 void ModuleFactory::initialize() {
231 // create one instance of each module-type
232 ASSERT( prototypes.empty() );
233 prototypes.reserve( Loki::TL::Length<Modules>::value );
234 Loki::TL::IterateTypes<Modules,Creator> gendata;
235 gendata( back_inserter(prototypes) );
236 // initialize the prototypes' settings and interconnections
237 for_each( prototypes, mem_fun(&Module::createDefaultSettings) );
238 for_each( prototypes, mem_fun(&Module::initDefaultModuleLinks) );
240 void ModuleFactory::changeDefaultSettings(const Module &module) {
241 // get the right prototype
242 const Module::TypeInfo &mi= module.info();
243 Module::SettingItem *protSet= prototype(mi.id).settings;
244 // replace prototype's settings
245 ASSERT( protSet && module.settings );
246 copy( module.settings, module.settings+mi.setLength, protSet );
247 // convert module-links to links to the correct prototypes
248 for (const Module::SettingTypeItem *setType= mi.setType
249 ; setType->type.type!=Module::Stop; ++setType,++protSet)
250 if ( setType->type.type==Module::ModuleCombo ) {
251 ASSERT(protSet->m);
252 protSet->m= constCast(&prototype( protSet->m->info().id ));
257 template<class T> int ModuleFactory::Instantiator<T>::operator()() const {
258 int i= getModuleID<T>();
259 Module *m= T::newCompatibleModule();
260 m= m->concreteClone<T>(Module::DeepCopy);
261 i+= T::getCompMods().front();
262 return i;
264 void ModuleFactory::instantiateModules() {
265 IterateTypes<Modules,Instantiator> gendata;
266 vector<int> v;
267 gendata(back_inserter(v));