1 #include <typeinfo> // needed for FerrisLoki/DataGenerators.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"
18 #include "FerrisLoki/DataGenerators.h"
22 typedef Loki::TL::MakeTypelist
< MRoot
, MColorModel
, MSquarePixels
, MQuadTree
, MStdDomains
23 , MQuality2SE_std
, MStdEncoder
, MDifferentialVLICodec
, MSaupePredictor
, MNoPredictor
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
);
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
>
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
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_
) );
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
;
90 SettingItem
*result
= new SettingItem
[length
];
91 copy( settings
, settings
+length
, result
);
93 SettingItem
*item
= result
, *itemEnd
= result
+length
;
95 // make child modules copy themselves (deeply again)
96 while (item
!=itemEnd
) {
98 item
->m
= item
->m
->abstractClone(DeepCopy
);
103 while (item
!=itemEnd
) {
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
)
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
132 result
->settings
= copySettings(method
);
136 void Module::createDefaultSettings() {
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
;
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
) {
181 put
<Uint32
>( stream
, settings
[i
].val
.i
);
184 put
<float>( stream
, settings
[i
].val
.f
);
187 file_saveModuleType( stream
, i
);
188 settings
[i
].m
->file_saveAllSettings(stream
);
195 void Module::file_loadAllSettings(std::istream
&stream
) {
196 int setLength
= info().setLength
;
197 ASSERT(setLength
>=0);
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
) {
209 settings
[i
].val
.i
= get
<Uint32
>(stream
);
212 settings
[i
].val
.f
= get
<float>(stream
);
215 file_loadModuleType( stream
, i
);
216 settings
[i
].m
->file_loadAllSettings(stream
);
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
) {
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();
264 void ModuleFactory::instantiateModules() {
265 IterateTypes
<Modules
,Instantiator
> gendata
;
267 gendata(back_inserter(v
));