This file is indexed.

/usr/include/CLAM/Factory.hxx is in libclam-dev 1.4.0-6.

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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
 * Copyright (c) 2007 MUSIC TECHNOLOGY GROUP (MTG)
 *                         UNIVERSITAT POMPEU FABRA
 *
 *
 * This program 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 2 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef _Factory_hxx_
#define _Factory_hxx_

#include <map>
#include <string>
#include <list>
#include <set>
#include <iostream> //TODO only for debugging

#include "Assert.hxx"
#include "ErrFactory.hxx"
#include "RunTimeLibraryLoader.hxx"

namespace CLAM {

/**
Factory usage example. 
To define a factory for your types you should create your own factory subclass.

@code
// MyFactory.hxx
class MyFactory : public CLAM::Factory<MyAbstractProduct>
{
public:
	static MyFactory& GetInstance();
};
@endcode

You must define GetInstance() method in a .cxx 
@code
// MyFactory.cxx
#include "MyFactory.hxx"
MyFactory& MyFactory::GetInstance()
{	
	static MyFactory theInstance;
	return theInstance;
}
@endcode

To automatically register concrete products into the factory at program
loading time (before "main()" starts), declare a registrator object like
this:
@code
// put this in a .cxx (not in a header)
static CLAM::FactoryRegistrator<MyFactory, MyConcreteProduct> regMyConcreteProduct("MyConcreteProduct");
@endcode
 */
template <typename AbstractProductType>
class Factory
{
public:
	typedef AbstractProductType AbstractProduct;
	typedef std::string RegistryKey;

private:	
	typedef std::string Attribute;
	typedef std::string Value;
	struct Pair 
	{
		Attribute attribute;
		Value value;
	};
	typedef RegistryKey Key; // TODO remove

public:
	typedef std::list<Key> Keys;
	typedef std::list<std::string> Values;
	typedef std::list<Pair> Pairs; 

	/** Abstract class for Creator objects which are stored in the Factory::Registry */
	class Creator
	{
	public:
		virtual AbstractProductType* Create() = 0;
		virtual ~Creator(){};
	};

	Factory() {};
	~Factory() {};
/*
	static Factory& GetInstance()	{
		static Factory theInstance;
		return theInstance;
	}
*/
	/**
	 * Gives ownership of the new created Product registered with
	 * the given name.
	 * It asserts that the name is in the registry.
	 */
	AbstractProduct* Create( const RegistryKey name )
	{
		Creator& creator = _registry.GetCreator( name );
		return creator.Create();
	}



	/** Gives ownership of the new created Product registered with
	 * the given name.
	 * It throws an ErrFactory if the name isn't found in the registry.
	 */
	AbstractProduct* CreateSafe( const RegistryKey name ) throw (ErrFactory)
	{
		return  _registry.GetCreatorSafe(name).Create();
	}


	void Clear()
	{ 
		_registry.RemoveAllCreators();
	}

	void AddCreator(const RegistryKey name, Creator* creator)
	{
		_registry.AddCreator(name, creator);
	}

	void AddCreatorWarningRepetitions(const RegistryKey name, Creator* creator)
	{
		_registry.AddCreatorWarningRepetitions(name, creator);
	}

	void AddCreatorSafe(const RegistryKey name, Creator* creator) throw (ErrFactory)
	{
		_registry.AddCreatorSafe(name, creator);
	}

	void DeleteCreator(const RegistryKey name)
	{
		_registry.DeleteCreator(name);
	}

	void GetRegisteredNames( std::list<std::string>& namesList )
	{
		_registry.GetRegisteredNames( namesList );
	}

	bool KeyExists( const RegistryKey& key)
	{
		return _registry.KeyExists(key);
	}

	bool AttributeExists (const std::string& key, const std::string& attribute)
	{
		return _registry.AttributeExists(key,attribute);
	}

	/// Get all keys that have attribute==value in its metadata.
	Keys GetKeys(const std::string& attribute, const std::string& value)
	{
		return _registry.GetKeys(attribute, value);
	}
	/// Get all keys in the factory
	Keys GetKeys()
	{
		return GetKeys("","");
	}
	/// Return all the metadata available for a product key
	Pairs GetPairsFromKey(const std::string& key)
	{
		return _registry.GetPairsFromKey(key);
	}
	/// Get the set of all values present for a given metadata attribute.
	/// Example GetSetOfValues("category") could return ["modulators","generators","reverbs"] without repeated items.
	Values GetSetOfValues(const std::string& attribute)
	{
		return _registry.GetSetOfValues(attribute);
	}
	/// Return the list of values for a metadata attribute for a product key.
	Values GetValuesFromAttribute(const std::string& key, const std::string& attribute)
	{
		return _registry.GetValuesFromAttribute(key, attribute);
	}
	/// Return the value for a metadata attribute of product key.
	/// If multiple values exist returns the first value.
	Value GetValueFromAttribute(const std::string& key, const std::string& attribute)
	{
		return GetValuesFromAttribute(key,attribute).front();
	}

	void AddAttribute(const std::string& key, const std::string& attribute, const std::string& value)
	{
		_registry.AddAttribute(key, attribute, value);
	}

public: // Inner classes. Public for better testing
	/**
	 * This class is an implementation class of the Factory. It is basically
	 * a container that maps keys with creators. It is not ment to be used
	 * directly by the user.
	 */
	class Registry
	{
	private:
		struct FactoryEntry {
			Creator * creator;
			Pairs pairs;
		};
		typedef std::map<Key, FactoryEntry> FactoryEntries;

	public:
		Creator& GetCreator( RegistryKey creatorId) 
		{
			CLAM_ASSERT(_factoryEntries.begin() != _factoryEntries.end(),
				"the Factory Registry shouldn't be empty");

			Creator* res = CommonGetCreator(creatorId);
			if (!res)
			{
				std::string errmsg("GetCreator invoked with a non existent key: ");
				errmsg += creatorId + "\nRegistered keys are:\n";
				errmsg += GetRegisteredNames();
				CLAM_ASSERT(res,errmsg.c_str());
			}

			return *res;
		}

		Creator& GetCreatorSafe( RegistryKey creatorId) throw (ErrFactory) 
		{
			if ( _factoryEntries.begin() == _factoryEntries.end() )
				throw ErrFactory("GetCreatorSafe invoked on an empty registry");

			Creator* res = CommonGetCreator(creatorId);
			if (!res)
			{
				std::string msg("GetCreatorSafe invoked with a non existent key: ");
				msg += creatorId;
				msg += "\nRegistered keys are:\n";
				msg += GetRegisteredNames();
				throw ErrFactory(msg.c_str());
			}
			return *res;
		}

		void AddCreator( RegistryKey creatorId, Creator* creator ) 
		{
			bool res = CommonAddCreator(creatorId, creator);
			if (!res)
			{
				std::string errmsg("Adding creator method in the factory: CreatorId '");
				errmsg += creatorId + "' was already registered.\nRegistered keys are:\n";
				errmsg += GetRegisteredNames();
				CLAM_ASSERT(res, errmsg.c_str());
			}
		}
		void AddCreatorWarningRepetitions( RegistryKey creatorId, Creator* creator ) 
		{
			bool res = CommonAddCreator(creatorId, creator);
			if (!res)
			{
				std::string errmsg("WARNING. While adding a creator method in the factory, id '");
				errmsg += creatorId + "' was already registered.";
//				errmsg += "\n Registered keys: " + GetRegisteredNames();
				CLAM_WARNING(false, errmsg.c_str() );
			}
		}

		void AddCreatorSafe( RegistryKey creatorId, Creator* creator ) throw (ErrFactory) 
		{
			if( !CommonAddCreator( creatorId, creator ) ) 
				throw ErrFactory("A repeated key was passed");
		}

		void DeleteCreator( RegistryKey creatorId)
		{
			if (CommonDeleteCreator(creatorId)==false)
				std::cout<<"WARNING: attempted to delete an inexistent creator"<<std::endl;
		}

		void RemoveAllCreators() 
		{
			_factoryEntries.clear();
		}

		std::size_t Count() { return _factoryEntries.size(); }

		void GetRegisteredNames( std::list<RegistryKey>& namesList )
		{
			typename FactoryEntries::const_iterator i;

			for ( i = _factoryEntries.begin(); i != _factoryEntries.end(); i++ )
			{
				namesList.push_back( i->first );
			}
		}
		std::string GetRegisteredNames()
		{
			std::string result;
			typedef std::list<RegistryKey> Names;
			Names names;
			GetRegisteredNames(names);
			for(Names::iterator it=names.begin(); it!=names.end(); it++)
			{
				result += (*it)+", ";
			}
			return result;
			
		}

		bool KeyExists(const RegistryKey& key)
		{
			typename FactoryEntries::const_iterator it = _factoryEntries.find(key);
			if(it == _factoryEntries.end())
			{
				return false;
			}
			return true;
		}
		bool AttributeExists(const std::string& key, const std::string& attribute)
		{
			Pairs pairsFromKey = GetPairsFromKey(key);
			typename Pairs::const_iterator itPairs;
			for (itPairs=pairsFromKey.begin();itPairs!=pairsFromKey.end();itPairs++)
			{
				if (itPairs->attribute==attribute) return true;
			}
			return false;
		}

		/// Get all keys that have attribute==value in its metadata.
		Keys GetKeys(const std::string& attribute, const std::string& value)
		{
			Keys result;
			typename FactoryEntries::const_iterator it;
			for(it = _factoryEntries.begin(); it != _factoryEntries.end(); it++)
			{
				if( (attribute == "") )
				{
					result.push_back(it->first);
					continue;
				}
				Pairs attributes = it->second.pairs;
				typename Pairs::const_iterator itAtt;
				for(itAtt = attributes.begin(); itAtt != attributes.end(); itAtt++)
				{
					if( ((*itAtt).attribute == attribute) && ((*itAtt).value == value) )
					{
						result.push_back(it->first);
					}
				}
			}
			return result;
		}
		/// Get all keys in the factory
		Keys GetKeys()
		{
			return GetKeys("","");
		}
		/// Return all the metadata available for a product key
		Pairs GetPairsFromKey(const std::string& key)
		{
			Pairs attributes;
			typename FactoryEntries::const_iterator it = _factoryEntries.find(key);
			if(it!=_factoryEntries.end())
			{
				attributes = it->second.pairs;
			}
			return attributes;
		}
		/// Get the set of all values present for a given metadata attribute.
		/// Example GetSetOfValues("category") could return ["modulators","generators","reverbs"] without repeated items.
		Values GetSetOfValues(const std::string& attribute)
		{
			std::set<Value> AttributeSet;
			std::set<Value>::const_iterator itSet;
			Values values;
			typename FactoryEntries::const_iterator it;
			for(it = _factoryEntries.begin(); it != _factoryEntries.end(); it++)
			{
				Pairs attributes = it->second.pairs;
				typename Pairs::const_iterator itAtt;
				for(itAtt = attributes.begin(); itAtt != attributes.end(); itAtt++)
				{
					if((*itAtt).attribute == attribute)
					{
						itSet = AttributeSet.find((*itAtt).value);
						if(itSet == AttributeSet.end())
						{
							AttributeSet.insert((*itAtt).value);
						}
					}
				}
			}
			// keep using the ProcessingFactory::Values
			for(itSet = AttributeSet.begin(); itSet != AttributeSet.end(); itSet++)
			{
				values.push_back(*itSet);
			}
			return values;
		}
		/// Return the list of values for a metadata attribute for a product key.
		Values GetValuesFromAttribute(const std::string& key, const std::string& attribute)
		{
			Values values;
			typename FactoryEntries::const_iterator it = _factoryEntries.find(key);
			if(it != _factoryEntries.end())
			{
				typename Pairs::const_iterator itAtt;
				for(itAtt = it->second.pairs.begin(); itAtt != it->second.pairs.end(); itAtt++)
				{
					if((*itAtt).attribute == attribute)
					{
						values.push_back((*itAtt).value);
					}
				}
			}
			return values;
		}
		/// Return the value for a metadata attribute of product key.
		Value GetValueFromAttribute(const std::string& key, const std::string& attribute)
		{
			return GetValuesFromAttribute(key,attribute).front();
		}

		void AddAttribute(const std::string& key, const std::string& attribute, const std::string& value)
		{
			typename FactoryEntries::const_iterator it;
			it = _factoryEntries.find(key);
			/*if(!KeyExists(key)) // NOT NEEDED AFETER UNIFYING
			{
				std::cout << "[Factory] tryind to add metadata to a non-existing key \"" << key << "\"" << std::endl; 
		//		return;  //pau: debugging: add metadata anyway. maybe factory registrator is about to be instantiated.
			}*/
			
			Pair pair;
			pair.attribute = attribute;
			pair.value = value;

			_factoryEntries[key].pairs.push_back(pair);
			/*Pairs pairs;
			if(it == _factoryEntries.end()) // it's a new key: insert it in the _factoryEntries map
			{
				pairs.push_back(pair);
				_factoryEntries.insert( typename FactoryEntries::value_type( key, pairs ) );

			} 
			else
			{
				_factoryEntries[key].push_back(pair);
			}*/
		}


	private: // data
		FactoryEntries _factoryEntries;

		// helper methods:
		Creator* CommonGetCreator( RegistryKey& creatorId ) 
		{
			typename FactoryEntries::const_iterator i = 
				_factoryEntries.find(creatorId);
			if ( i==_factoryEntries.end() ) // not found
				return 0;
			return i->second.creator;
		}

		bool CommonAddCreator( RegistryKey& creatorId, Creator* creator) 
		{
			FactoryEntry factoryEntry;
			Pairs pairs;
			factoryEntry.creator = creator;
			factoryEntry.pairs = pairs;
			// returns false if the key was repeated.
			typedef typename FactoryEntries::value_type ValueType;
			return  _factoryEntries.insert( ValueType( creatorId, factoryEntry ) ).second;
		}

		bool CommonDeleteCreator (RegistryKey& creatorId)
		{
			typename FactoryEntries::iterator i =
				_factoryEntries.find(creatorId);
			if ( i == _factoryEntries.end() ) //not found
				return false;
			Creator * creator = i->second.creator; 
			delete creator;
			_factoryEntries.erase(i);
			return true;
		}
	};

	int Count() { return _registry.Count(); }

private:
	Registry _registry;
};


/**
* This class provides a convenient way to add items (creators) into a factory.
* To add class A (subclass of Base) to the factory it's useful to declare a static 
* FactoryRegistrator object like this: static FactoryRegistrator<MyTypeFactory, MyConcreteType> reg("key");
* The FactoryRegistrator constructor called at load-time is in charge to insert the creator
* to the factory.
* Various constructors exists giving the user options like using either 
* the singleton factory or a given one.
*/
template< typename TheFactoryType, typename ConcreteProductType>
class FactoryRegistrator
{
	typedef typename TheFactoryType::AbstractProduct AbstractProduct;
	typedef typename TheFactoryType::RegistryKey RegistryKey;
public:
	FactoryRegistrator( const char* metadata[] ) 
	{
		CLAM_ASSERT(std::string(metadata[0])==std::string("key"), "FactoryRegistrator: first char* metadata should be 'key'"); //TODO fix
		CLAM_ASSERT(metadata[1], "FactoryRegistrator: value for first attriute ('key') must not be 0");
		std::string key = metadata[1];

		TheFactoryType & factory = TheFactoryType::GetInstance();
		factory.AddCreatorWarningRepetitions( key, new ConcreteCreator() );
		std::string attribute, value;
		for(unsigned i = 2; metadata[i]; i++)
		{
			attribute = metadata[i];
			if(!metadata[i+1])
			{
				std::cout << "[METADATA] error with attribute \"" << attribute << "\"" << std::endl;
				CLAM_ASSERT(false, "Bad metadata value");
				break;
			}
			value = metadata[++i];
			factory.AddAttribute(key, attribute, value);
		}
		const std::string & libraryFileName=RunTimeLibraryLoader::FileOfSymbol(this);
		if (libraryFileName!="")
			factory.AddAttribute(key,"library",libraryFileName);
	}

	FactoryRegistrator( RegistryKey key, TheFactoryType& fact ) 
	{
//		std::cout << "FactoryRegistrator(key,factory) " << key << std::endl;
		fact.AddCreatorWarningRepetitions( key, new ConcreteCreator() );
	}

	FactoryRegistrator( TheFactoryType& fact ) 
	{
		ConcreteProductType dummy;
		RegistryKey key=dummy.GetClassName();
//		std::cout << "FactoryRegistrator(factory) " << dummy.GetClassName() << std::endl;
		fact.AddCreatorWarningRepetitions( key, new ConcreteCreator() );
	}

	FactoryRegistrator( RegistryKey key ) 
	{
//		std::cout << "FactoryRegistrator(key) " << key << std::endl;
		TheFactoryType::GetInstance().AddCreatorWarningRepetitions( key, new ConcreteCreator() );
	}

/*
	FactoryRegistrator( ) 
	{
		ConcreteProductType dummy;
		RegistryKey key=dummy.GetClassName();
//		std::cout << "FactoryRegistrator() " << key << std::endl;
		TheFactoryType::GetInstance().AddCreatorWarningRepetitions( key, new ConcreteCreator() );
	}
*/
	~FactoryRegistrator() 
	{
//		std::cout << "~FactoryRegistrator() " << std::endl;
	}
	
	class ConcreteCreator : public TheFactoryType::Creator
	{
	public:
		AbstractProduct *Create()
		{	
			return new ConcreteProductType();
		}

	};
};

} // namespace

#endif // _Factory_hxx_