This file is indexed.

/usr/include/mia-2.4/mia/core/factory.hh is in libmia-2.4-dev 2.4.3-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* -*- mia-c++  -*-
 *
 * This file is part of MIA - a toolbox for medical image analysis 
 * Copyright (c) Leipzig, Madrid 1999-2016 Gert Wollny
 *
 * MIA is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MIA; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef mia_core_factory_hh
#define mia_core_factory_hh

#include <iostream>
#include <memory>
#include <string>
#include <mia/core/handler.hh>
#include <mia/core/msgstream.hh>
#include <mia/core/errormacro.hh>
#include <mia/core/product_base.hh>
#include <mia/core/optionparser.hh>
#include <mia/core/productcache.hh>
#include <mia/core/traits.hh>
#include <mia/core/import_handler.hh>

NS_MIA_BEGIN


/** 
    \ingroup plugin

    @brief This is tha base of all plugins that create "things", like filters, cost functions 
    time step operatores and the like. 
    
    This template is the model for all factory plugins, i.e. plugins that create certain objects.
   \tparam P the object type created by the factory.
*/
template <typename P>
class EXPORT_HANDLER TFactory: 
	public TPlugin<typename P::plugin_data, typename P::plugin_type> {
public: 

	/// typedef to describe the product of the factory 
	typedef P Product; 
	
	/// typedef for the shared version of the product 
	typedef std::shared_ptr<P > SharedProduct; 


	/// typedef for the unique version of the product 
	typedef std::unique_ptr<P > UniqueProduct; 
	
	/** initialise the plugin by the names 
	    \remark what are these names and types good for?
	*/
	TFactory(char const * const  name);
	
	/** This function creates the object handled by this plugin 
	    It uses options to set its parameters and, if successfull, 
	    sets the init string of the object to params and 
	    returns the newly created object as a shared pointer. 
	    
	    @param options the options to initialise the plugin 
	    @param params original parameter string 
	    @returns an instance of the requested object
	*/
	virtual Product *create(const CParsedOptions& options, char const *params) __attribute__((warn_unused_result));
	
private:
	virtual Product *do_create() const __attribute__((warn_unused_result)) = 0 ;
	CMutex m_mutex; 
};


/**
    \ingroup plugin

   @brief the Base class for all plugn handlers that deal with factory plugins.  
   
   Base class for all plugin handlers that are derived from TFactory. 
 */
template <typename  I>
class EXPORT_HANDLER TFactoryPluginHandler: public  TPluginHandler< I > {
protected: 
	//! \name Constructors
        //@{
        /*! \brief Initializes the plugin handler 
	*/

	
	TFactoryPluginHandler(); 
        //@}
public: 
	/// The type of the the object this plug in hander produces 
	typedef typename I::Product Product; 

	/// The shared pointer type of the the object this plug in hander produces 
	typedef typename I::SharedProduct ProductPtr; 

	/// The unique pointer type of the the object this plug in hander produces 
	typedef typename I::UniqueProduct UniqueProduct; 

        /**
	   Create an object according to the given description. If creation fails, the function 
	   will throw an invalid_argument exception 
	   \param plugindescr the description of the plug-in 
	   \returns a shared pointer containing the product of the plug-in according to the description 
	*/
	ProductPtr produce(const std::string& plugindescr)const;  
	
	/// \overload produce(const std::string& plugindescr)
	ProductPtr produce(const char *plugindescr) const{
		return produce(std::string(plugindescr)); 
	}

	/**
	   Create an object according to the given description. If creation fails, the function 
	   will throw an invalid_argument exception 
	   \param plugindescr the description of the plug-in 
	   \returns a unique pointer of the product of the plug-in according to the description 
	*/

	UniqueProduct produce_unique(const std::string& plugindescr)const; 
		
	/// \overload produce(const std::string& plugindescr)
	UniqueProduct produce_unique(const char *plugindescr) const{
		return produce_unique(std::string(plugindescr)); 
	}

	/**
	   Sets whether the created shared pointer products should be cached. 
	   Unique products will never be cached. 
	   \param enable 
	 */
	void set_caching(bool enable) const; 


private: 
	std::string get_handler_type_string_and_help(std::ostream& os) const; 
	
	std::string do_get_handler_type_string() const; 

        virtual bool do_validate_parameter_string(const std::string& s) const;

	typename I::Product *produce_raw(const std::string& plugindescr) const;

	mutable TProductCache<ProductPtr> m_cache; 

	template <typename Handler, typename Chained, bool chainable> 
		friend struct create_plugin; 

}; 

/*
  Implementation of the factory
*/

template <typename I>
TFactory<I>::TFactory(char const * const  name):
	TPlugin<typename I::plugin_data, typename I::plugin_type>(name)
{
}

template <typename I>
typename TFactory<I>::Product *TFactory<I>::create(const CParsedOptions& options, char const *params)
{
	CScopedLock lock(m_mutex); 
	try {
		this->set_parameters(options);
		this->check_parameters();
		auto product = this->do_create();
		if (product) {
			product->set_module(this->get_module()); 
			product->set_init_string(params); 
		}
		return product; 
	}
	catch (std::length_error& x) {
		std::stringstream msg; 
		msg << "CParamList::set: Some string was not created properly\n"; 
		msg << "  options were:\n"; 
		for (auto i = options.begin();
		     i != options.end(); ++i) {
			msg << "    " << i->first << "=" << i->second << "\n";
		}
		cverr() << msg.str(); 
		throw std::logic_error("Probably a race condition"); 

	}
}

template <typename  I>
TFactoryPluginHandler<I>::TFactoryPluginHandler():
	m_cache(this->get_descriptor())
{
	set_caching(__cache_policy<I>::apply()); 
}

template <typename  I>
void TFactoryPluginHandler<I>::set_caching(bool enable) const 
{
	cvdebug() << this->get_descriptor() << ":Set cache policy to " << enable << "\n"; 
	m_cache.enable_write(enable); 
}

template <typename  I>
typename TFactoryPluginHandler<I>::ProductPtr 
TFactoryPluginHandler<I>::produce(const std::string& plugindescr) const
{
	auto result = m_cache.get(plugindescr);
	if (!result) {
		result.reset(this->produce_raw(plugindescr)); 
		m_cache.add(plugindescr, result); 
	}else
		cvdebug() << "Use cached '" << plugindescr << "'\n"; 
	return result; 
}

template <typename  I>
typename TFactoryPluginHandler<I>::UniqueProduct
TFactoryPluginHandler<I>::produce_unique(const std::string& plugindescr) const
{
	return UniqueProduct(this->produce_raw(plugindescr)); 
}

template <typename  I>
std::string TFactoryPluginHandler<I>::get_handler_type_string_and_help(std::ostream& os) const
{
	os << " The string value will be used to construct a plug-in."; 
	return do_get_handler_type_string(); 
}

template <typename  I>
std::string TFactoryPluginHandler<I>::do_get_handler_type_string() const
{
	return "factory"; 
}

template <typename  I>
bool TFactoryPluginHandler<I>::do_validate_parameter_string(const std::string& s) const
{
	cvdebug() << "Check whether factory '" << this->get_descriptor() << "' can understand '" << s << "'\n"; 
        // find the part describing the plug-in name and check whether it
        // is available
        auto colon_pos = s.find(':');
        auto plugin_name = s.substr(0, colon_pos);
        if (this->plugin(plugin_name.c_str())) 
                return true;
        return false;
}

template <typename Handler, typename Chained, bool chainable> 
struct create_plugin {
	typedef typename Handler::Product Product; 
	static Product *apply(const Handler& h, const CComplexOptionParser& param_list, const std::string& params) {
		if (param_list.size() > 1) {
			throw create_exception<std::invalid_argument>( "Factory " , h.get_descriptor(), 
								       ": No chaining supported but ", param_list.size(), 
								       " plugin descriptors were given. "
								       "If the description contains a '+' sign as part "
								       "of a parameter you must protect it by enclosing the "
								       "value in square brackets like this: [1e+6]");
		}
		
		cvdebug() << "TFactoryPluginHandler<P>::produce use '" << param_list.begin()->first << "'\n"; 
		const std::string& factory_name = param_list.begin()->first; 
		
		if (factory_name == plugin_help) {
			cvdebug() << "print help\n"; 
			cvmsg() << "\n"; 
			h.print_help(cverb);
			return nullptr; 
		}
		
		cvdebug() << "TFactoryPluginHandler<>::produce: Create plugin from '" << factory_name << "'\n"; 
		
		auto factory = h.plugin(factory_name.c_str());
		if (!factory) 
			throw create_exception<std::invalid_argument>("Unable to find plugin for '", factory_name.c_str(), "'");
		return factory->create(param_list.begin()->second,params.c_str());
	}
}; 

template <typename Handler, typename ProductChained> 
struct create_plugin<Handler, ProductChained, true> {
	typedef typename Handler::Product Product; 
	
	static Product *apply(const Handler& h, const CComplexOptionParser& param_list, const std::string& params) {
		
		if (param_list.size() == 1) 
			return create_plugin<Handler, ProductChained, false>::apply(h, param_list, params); 

		ProductChained *result = new ProductChained();
		try {
			for (auto ipl = param_list.begin(); ipl != param_list.end(); ++ipl) {
				
				const std::string& factory_name = ipl->first; 
				cvdebug() << "TFactoryPluginHandler<P>::produce use '" << factory_name << "\n"; 
				
				if (factory_name == plugin_help) {
					cvdebug() << "print help\n"; 
					cvmsg() << "\n"; 
					h.print_help(cverb);
					delete result; 
					return nullptr; 
				}
				
				auto factory = h.plugin(factory_name.c_str());
				if (!factory) {
					delete result; 
					throw create_exception<std::invalid_argument>("Unable to find plugin for '", factory_name.c_str(), "'");
				}
				
				auto r = factory->create(ipl->second,params.c_str());
				result->push_back(typename Product::Pointer(r)); 
			}
			result->set_init_string(params.c_str()); 
		}
		catch (std::exception& x) {
			delete result; 
			throw x; 
		}
		return result; 
	}
}; 

	
template <typename  I>
typename I::Product *TFactoryPluginHandler<I>::produce_raw(const std::string& params)const
{
	if (params.empty()) {
		throw create_exception<std::invalid_argument>("Factory ", this->get_descriptor(), ": Empty description string given. "
						    "Supported plug-ins are '", this->get_plugin_names(), "'. " 
						    "Set description to 'help' for more information."); 
	}
	
	CComplexOptionParser param_list(params);
		
	if (param_list.size() < 1) {
		throw create_exception<std::invalid_argument>( "Factory " , this->get_descriptor(), ": Description string '"
		      , params , "' can not be interpreted. "
		      "Supported plug-ins are '" , this->get_plugin_names() , "'. " 
		      "Set description to 'help' for more information."); 
	}

	return create_plugin<TFactoryPluginHandler<I>, 
			     typename plugin_can_chain<I>::Chained, 
			     plugin_can_chain<I>::value>::apply(*this, param_list, params); 
}

/**     
	\ingroup plugin
	Do some explicit instanciation for a plugin based on TFactory 
*/
#define EXPLICIT_INSTANCE_PLUGIN(T) \
	template class TPlugin<T::plugin_data, T::plugin_type>; \
	template class TFactory<T>;					

/**     
	\ingroup plugin
	Do some explicit instanciation for a plugin based on TFactoryPluginHandler
*/
#define EXPLICIT_INSTANCE_PLUGIN_HANDLER(P) \
	template class TPluginHandler<P>;			\
	template class TFactoryPluginHandler<P>;		\
	template class THandlerSingleton<TFactoryPluginHandler<P> >;

/** 
    \ingroup plugin
    Do some explicit instanciation for the plugin classe and the handler of 
    a plugin based on TFactoryPluginHandler 
*/
#define EXPLICIT_INSTANCE_HANDLER(T) \
	template class TPlugin<T::plugin_data, T::plugin_type>; \
	template class TFactory<T>;					\
	template class TPluginHandler<TFactory<T> >;			\
	template class TFactoryPluginHandler<TFactory<T> >;		\
	template class THandlerSingleton<TFactoryPluginHandler<TFactory<T> > >;

/** 
    \ingroup plugin
   Do an explicit instanciation of plug-in classes and handlers for plugins that are 
   explicitely derived from TFactory.  
 */

#define EXPLICIT_INSTANCE_DERIVED_FACTORY_HANDLER(T, F)		\
	template class TPlugin<T::plugin_data, T::plugin_type>; \
	template class TFactory<T>;					\
	template class TPluginHandler<F>;			\
	template class TFactoryPluginHandler<F>;		\
	template class THandlerSingleton<TFactoryPluginHandler<F> >;



NS_MIA_END
#endif