File: Factorable.hpp

package info (click to toggle)
yade 2021.01a-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 25,208 kB
  • sloc: cpp: 85,587; python: 37,126; sh: 351; makefile: 117
file content (75 lines) | stat: -rw-r--r-- 6,423 bytes parent folder | download
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
/*************************************************************************
*  Copyright (C) 2004 by Olivier Galizzi                                 *
*  olivier.galizzi@imag.fr                                               *
*  Copyright (C) 2004 by Janek Kozicki                                   *
*  cosurgi@berlios.de                                                    *
*                                                                        *
*  This program is free software; it is licensed under the terms of the  *
*  GNU General Public License v2 or later. See file LICENSE for details. *
*************************************************************************/

#pragma once

#include "ClassFactory.hpp"

#include <sstream>
#include <string>

namespace yade { // Cannot have #include directive inside.

//! macro for registering both class and its base
#define REGISTER_CLASS_AND_BASE(cn, bcn)                                                                                                                       \
	REGISTER_CLASS_NAME(cn);                                                                                                                               \
	REGISTER_BASE_CLASS_NAME(bcn);

#define REGISTER_CLASS_NAME(cn)                                                                                                                                \
public:                                                                                                                                                        \
	virtual string getClassName() const { return #cn; };

// FIXME[1] - that macro below should go to another class! factorable has nothing to do with inheritance tree.

#define REGISTER_BASE_CLASS_NAME(bcn)                                                                                                                          \
public:                                                                                                                                                        \
	virtual string getBaseClassName(unsigned int i = 0) const                                                                                              \
	{                                                                                                                                                      \
		string             token;                                                                                                                      \
		vector<string>     tokens;                                                                                                                     \
		string             str = #bcn;                                                                                                                 \
		std::istringstream iss(str);                                                                                                                   \
		while (!iss.eof()) {                                                                                                                           \
			iss >> token;                                                                                                                          \
			tokens.push_back(token);                                                                                                               \
		}                                                                                                                                              \
		if (i >= token.size())                                                                                                                         \
			return "";                                                                                                                             \
		else                                                                                                                                           \
			return tokens[i];                                                                                                                      \
	}                                                                                                                                                      \
                                                                                                                                                               \
public:                                                                                                                                                        \
	virtual int getBaseClassNumber()                                                                                                                       \
	{                                                                                                                                                      \
		string             token;                                                                                                                      \
		vector<string>     tokens;                                                                                                                     \
		string             str = #bcn;                                                                                                                 \
		std::istringstream iss(str);                                                                                                                   \
		while (!iss.eof()) {                                                                                                                           \
			iss >> token;                                                                                                                          \
			tokens.push_back(token);                                                                                                               \
		}                                                                                                                                              \
		return tokens.size();                                                                                                                          \
	}


class Factorable {
public:
	Factorable() { }
	virtual ~Factorable() { }

	virtual string getBaseClassName(unsigned int = 0) const { return ""; }
	virtual int    getBaseClassNumber() { return 0; }

	REGISTER_CLASS_NAME(Factorable);
};

} // namespace yade