propertysystem.hh 38.9 KB
Newer Older
1
2
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
3
/*****************************************************************************
4
 *   See the file COPYING for full copying permissions.                      *
5
 *                                                                           *
6
 *   This program is free software: you can redistribute it and/or modify    *
7
 *   it under the terms of the GNU General Public License as published by    *
8
9
 *   the Free Software Foundation, either version 2 of the License, or       *
 *   (at your option) any later version.                                     *
10
 *                                                                           *
11
12
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
13
 *   MERCHANTBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
14
15
16
17
 *   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, see <http://www.gnu.org/licenses/>.   *
18
19
20
21
22
 *****************************************************************************/
/*!
 * \file
 * \brief Provides the magic behind the DuMuX property system.
 *
23
 * \ingroup Properties
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 * Properties allow to associate arbitrary data types to
 * identifiers. A property is always defined on a pair (TypeTag,
 * PropertyTag) where TypeTag is the identifier for the object the
 * property is defined for and PropertyTag is an unique identifier of
 * the property.
 *
 * Type tags are hierarchic and inherit properties defined on their
 * ancesters. At each level, properties defined on lower levels can be
 * overwritten or even made undefined. It is also possible to define
 * defaults for properties if it makes sense.
 *
 * Properties may make use other properties for the respective type
 * tag and these properties can also be defined on an arbitrary level
 * of the hierarchy.
 */
#ifndef DUMUX_PROPERTIES_HH
#define DUMUX_PROPERTIES_HH

42
43
44
45
46
47
48
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
49
// For is_base_of
50
#include <type_traits>
51
52

// Integral Constant Expressions
53
54
#include <dune/common/classname.hh>

55
56
57
58
namespace Dumux
{
namespace Properties
{
59
60
61
62
63
64
#define DUMUX_GET_HEAD_(Arg1, ...) Arg1
#define DUMUX_GET_TAIL_(Arg1, ...) Blubber // __VA_ARGS__

#define DUMUX_STRINGIGY_HEAD_(Arg1, ...) #Arg1
#define DUMUX_STRINGIGY_TAIL_(Arg1, ...) #__VA_ARGS__

65
#if !defined NO_PROPERTY_INTROSPECTION
66

67
//! Internal macro which is only required if the property introspection is enabled
68
#define PROP_INFO_(EffTypeTagName, PropKind, PropTagName, ...)          \
69
    template <>                                                         \
70
    struct PropertyInfo<TTAG(EffTypeTagName), PTAG_(PropTagName)>       \
71
    {                                                                   \
72
73
74
75
76
    static int init() {                                                 \
        PropertyRegistryKey key(                                        \
            /*effTypeTagName=*/ Dune::className<TTAG(EffTypeTagName)>(), \
            /*kind=*/PropKind,                                          \
            /*name=*/#PropTagName,                                      \
77
            /*value=*/#__VA_ARGS__,                                     \
78
79
80
81
82
83
84
            /*file=*/__FILE__,                                          \
            /*line=*/__LINE__);                                         \
        PropertyRegistry::addKey(key);                                  \
        return 0;                                                       \
    };                                                                  \
    static int foo;                                                     \
    };                                                                  \
85
86
87
    int PropertyInfo<TTAG(EffTypeTagName), PTAG_(PropTagName)>::foo =   \
    PropertyInfo<TTAG(EffTypeTagName), PTAG_(PropTagName)>::init();

88
//! Internal macro which is only required if the property introspection is enabled
89
#define TTAG_INFO_(...)                                                 \
90
    template <>                                                         \
91
    struct TypeTagInfo<DUMUX_GET_HEAD_(__VA_ARGS__)>               \
92
93
    {                                                                   \
        static int init() {                                             \
94
            TypeTagRegistry::addChildren<__VA_ARGS__>();                \
95
            return 0;                                                   \
96
        }                                                              \
97
        static int foo;                                                 \
98
    };                                                                  \
99
100
    int TypeTagInfo<DUMUX_GET_HEAD_(__VA_ARGS__)>::foo =           \
        TypeTagInfo<DUMUX_GET_HEAD_(__VA_ARGS__)>::init();
101

102
#else
103
//! Don't do anything if introspection is disabled
104
#define PROP_INFO_(EffTypeTagName, PropKind, PropTagName, ...)
105
#define TTAG_INFO_(EffTypeTagName, ...)
106
107
108
109
110
111
112
#endif

// some macros for simplification

/*!
 * \brief Makes a type out of a type tag name
 */
113
#define TTAG(TypeTagName) ::Dumux::Properties::TTag::TypeTagName
114
115
116
117

/*!
 * \brief Makes a type out of a property tag name
 */
118
119
120
121
122
123
#define PTAG(PropTagName) PropTagName

/*!
 * \brief Makes a type out of a property tag name
 */
#define PTAG_(PropTagName) ::Dumux::Properties::PTag::PropTagName
124
125
126
127
128
129
130

/*!
 * \brief Define a new type tag.
 *
 * A type tag can inherit the properties defined on up to five parent
 * type tags. Examples:
 *
131
 * \code
132
133
134
135
136
137
138
139
140
141
 * // The type tag doesn't inherit any properties from other type tags
 * NEW_TYPE_TAG(FooTypeTag);
 *
 * // BarTypeTag inherits all properties from FooTypeTag
 * NEW_TYPE_TAG(BarTypeTag, INHERITS_FROM(FooTypeTag));
 *
 * // FooBarTypeTag inherits the properties of FooTypeTag as well as
 * // those of BarTypeTag. Properties defined on BarTypeTag have
 * // preceedence over those defined for FooTypeTag:
 * NEW_TYPE_TAG(FooBarTypeTag, INHERITS_FROM(FooTypeTag, BarTypeTag));
142
 * \endcode
143
 */
144
145
146
147
148
149
150
#define NEW_TYPE_TAG(...)                                               \
    namespace TTag {                                                    \
    struct DUMUX_GET_HEAD_(__VA_ARGS__, blubb)                          \
        : public TypeTag<__VA_ARGS__>                                   \
    { };                                                                \
    TTAG_INFO_(__VA_ARGS__, void)                                       \
    }                                                                   \
151
    extern int semicolonHack_
152
153
154
155
156
157

/*!
 * \brief Syntactic sugar for NEW_TYPE_TAG.
 *
 * See the documentation for NEW_TYPE_TAG.
 */
158
#define INHERITS_FROM(...) __VA_ARGS__
159
160
161
162
163
164
165
166
167
168

/*!
 * \brief Define a property tag.
 *
 * A property tag is the unique identifier for a property. It may only
 * be declared once in your program. There is also no hierarchy of
 * property tags as for type tags.
 *
 * Examples:
 *
169
 * \code
170
171
 * NEW_PROP_TAG(blubbPropTag);
 * NEW_PROP_TAG(blabbPropTag);
172
 * \endcode
173
 */
174
#define NEW_PROP_TAG(PTagName)                             \
175
176
177
    namespace PTag {                                       \
    struct PTagName; } extern int semicolonHack_

178
//! Internal macro
179
#define SET_PROP_(EffTypeTagName, PropKind, PropTagName, ...)       \
180
181
182
    template <class TypeTag>                                        \
    struct Property<TypeTag,                                        \
                    TTAG(EffTypeTagName),                           \
183
                    PTAG_(PropTagName)>;                            \
184
185
186
    PROP_INFO_(EffTypeTagName,                                      \
               /*kind=*/PropKind,                                   \
               PropTagName,                                         \
187
               /*value=*/__VA_ARGS__)                               \
188
    template <class TypeTag>                                        \
189
190
    struct Property<TypeTag,                                        \
                    TTAG(EffTypeTagName),                           \
191
                    PTAG_(PropTagName) >
192
193
194
195
196
197
198
199
200
201
202

/*!
 * \brief Set a property for a specific type tag.
 *
 * After this macro, you must to specify a complete body of a class
 * template, including the trailing semicolon. If you need to retrieve
 * another property within the class body, you can use TypeTag as the
 * argument for the type tag for the GET_PROP macro.
 *
 * Example:
 *
203
 * \code
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
 * SET_PROP(FooTypeTag, blubbPropTag)
 * {
 *    static int value = 10;
 *    static int calculate(int arg)
 *    { calculateInternal_(arg); }
 *
 * private:
 *    // retrieve the blabbProp property for the real TypeTag the
 *    // property is defined on. Note that blabbProb does not need to
 *    // be defined on FooTypeTag, but can also be defined for some
 *    // derived type tag.
 *    typedef typename GET_PROP(TypeTag, blabbProp) blabb;
 *
 *    static int calculateInternal_(int arg)
 *    { return arg * blabb::value; };
219
 * \endcode
220
221
 * };
 */
222
#define SET_PROP(EffTypeTagName, PropTagName)                   \
223
    template <class TypeTag>                                    \
224
225
    struct Property<TypeTag,                                    \
                    TTAG(EffTypeTagName),                       \
226
                    PTAG_(PropTagName)>;                        \
227
228
229
230
    PROP_INFO_(EffTypeTagName,                                  \
               /*kind=*/"opaque",                               \
               PropTagName,                                     \
               /*value=*/"<opaque>")                            \
231
    template <class TypeTag>                                    \
232
233
    struct Property<TypeTag,                                    \
                    TTAG(EffTypeTagName),                       \
234
                    PTAG_(PropTagName) >
235
236
237
238

/*!
 * \brief Explicitly unset a property for a type tag.
 *
239
 * This means that the property will not be inherited from the type
240
241
242
243
 * tag's parents and that no default will be used.
 *
 * Example:
 *
244
 * \code
245
246
 * // make the blabbPropTag property undefined for the BarTypeTag.
 * UNSET_PROP(BarTypeTag, blabbPropTag);
247
 * \endcode
248
 */
249
#define UNSET_PROP(EffTypeTagName, PropTagName)                 \
250
    template <>                                                 \
251
    struct PropertyUnset<TTAG(EffTypeTagName),                  \
252
                         PTAG_(PropTagName) >;                  \
253
    PROP_INFO_(EffTypeTagName,                                  \
254
               /*kind=*/"withdraw",                             \
255
               PropTagName,                                     \
256
               /*value=*/<none>)                                \
257
    template <>                                                 \
258
    struct PropertyUnset<TTAG(EffTypeTagName),                  \
259
                         PTAG_(PropTagName) >                   \
260
        : public PropertyExplicitlyUnset                        \
261
262
263
264
265
266
267
        {}

/*!
 * \brief Set a property to a simple constant integer value.
 *
 * The constant can be accessed by the 'value' attribute.
 */
268
#define SET_INT_PROP(EffTypeTagName, PropTagName, /*Value*/...)    \
269
    SET_PROP_(EffTypeTagName,                                   \
270
              /*kind=*/"int   ",                                \
271
              PropTagName,                                      \
272
              /*value=*/__VA_ARGS__)                            \
273
274
    {                                                           \
        typedef int type;                                       \
275
        static constexpr int value = __VA_ARGS__;               \
276
277
278
279
280
281
282
    }

/*!
 * \brief Set a property to a simple constant boolean value.
 *
 * The constant can be accessed by the 'value' attribute.
 */
283
284
285
286
287
288
289
290
#define SET_BOOL_PROP(EffTypeTagName, PropTagName, /*Value*/...)    \
    SET_PROP_(EffTypeTagName,                                       \
              /*kind=*/"bool  ",                                    \
              PropTagName,                                          \
              /*value=*/__VA_ARGS__)                                \
    {                                                               \
        typedef bool type;                                          \
        static constexpr bool value = __VA_ARGS__;                  \
291
292
293
294
295
296
297
    }

/*!
 * \brief Set a property which defines a type.
 *
 * The type can be accessed by the 'type' attribute.
 */
298
299
300
301
302
303
304
#define SET_TYPE_PROP(EffTypeTagName, PropTagName, /*Value*/...)  \
    SET_PROP_(EffTypeTagName,                                     \
              /*kind=*/"type  ",                                  \
              PropTagName,                                        \
              /*value=*/__VA_ARGS__)                              \
    {                                                             \
        typedef __VA_ARGS__ type;                                 \
305
306
307
308
309
310
311
312
313
    }

/*!
 * \brief Set a property to a simple constant scalar value.
 *
 * The constant can be accessed by the 'value' attribute. In order to
 * use this macro, the property tag "Scalar" needs to be defined for
 * the real type tag.
 */
314
#define SET_SCALAR_PROP(EffTypeTagName, PropTagName, ...)               \
315
316
317
    SET_PROP_(EffTypeTagName,                                           \
              /*kind=*/"scalar",                                        \
              PropTagName,                                              \
318
              /*value=*/__VA_ARGS__)                                    \
319
    {                                                                   \
320
        typedef typename GET_PROP_TYPE(TypeTag, Scalar) Scalar;         \
321
322
    public:                                                             \
        typedef Scalar type;                                            \
323
324
325
326
327
        static const Scalar value;                                      \
    };                                                                  \
    template <class TypeTag>                                            \
    const typename Property<TypeTag, TTAG(EffTypeTagName), PTAG_(PropTagName)>::type   \
    Property<TypeTag, TTAG(EffTypeTagName), PTAG_(PropTagName)>::value(__VA_ARGS__)
328

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*!
 * \brief Set a property to a simple constant string value.
 *
 * The constant can be accessed by the 'value' attribute and is of
 * type std::string.
 */
#define SET_STRING_PROP(EffTypeTagName, PropTagName, ...)               \
    SET_PROP_(EffTypeTagName,                                           \
              /*kind=*/"string",                                        \
              PropTagName,                                              \
              /*value=*/__VA_ARGS__)                                    \
    {                                                                   \
    public:                                                             \
        typedef std::string type;                                       \
        static const std::string value;                                 \
    };                                                                  \
    template <class TypeTag>                                            \
346
347
    const typename Property<TypeTag, TTAG(EffTypeTagName), PTAG_(PropTagName)>::type \
    Property<TypeTag, TTAG(EffTypeTagName), PTAG_(PropTagName)>::value(__VA_ARGS__)
348

349
350
351
352
353
354
355
/*!
 * \brief Get the property for a type tag.
 *
 * If you use GET_PROP within a template and want to refer to some
 * type (including the property itself), GET_PROP must be preceeded by
 * the 'typename' keyword.
 */
356
357
358
#define GET_PROP(TypeTag, PropTagName) \
    ::Dumux::Properties::GetProperty<TypeTag, PTAG_(PropTagName)>::p
#define GET_PROP_(TypeTag, PropTag) \
359
    ::Dumux::Properties::GetProperty<TypeTag, PropTag>::p
360
361
362
363
364

/*!
 * \brief Access the 'value' attribute of a property for a type tag.
 *
 * This is just for convenience and equivalent to GET_PROP(TypeTag,
Bernd Flemisch's avatar
Bernd Flemisch committed
365
 * PropTag) :: value.  If the property doesn't have an attribute named
366
367
 * 'value', this yields a compiler error.
 */
368
369
370
#define GET_PROP_VALUE(TypeTag, PropTagName)                            \
    ::Dumux::Properties::GetProperty<TypeTag, PTAG_(PropTagName)>::p::value
#define GET_PROP_VALUE_(TypeTag, PropTag)                               \
371
    ::Dumux::Properties::GetProperty<TypeTag, PropTag>::p::value
372
373
374
375
376

/*!
 * \brief Access the 'type' attribute of a property for a type tag.
 *
 * This is just for convenience and equivalent to GET_PROP(TypeTag,
Bernd Flemisch's avatar
Bernd Flemisch committed
377
 * PropTag) :: type.  If the property doesn't have an attribute named
378
379
380
 * 'type', this yields a compiler error. Also, if you use this macro
 * within a template, it must be preceeded by the 'typename' keyword.
 */
381
382
383
#define GET_PROP_TYPE(TypeTag, PropTagName) \
    ::Dumux::Properties::GetProperty<TypeTag, PTAG_(PropTagName)>::p::type
#define GET_PROP_TYPE_(TypeTag, PropTag) \
384
    ::Dumux::Properties::GetProperty<TypeTag, PropTag>::p::type
385
386
387
388
389
390
391
392
393
394
395

#if !defined NO_PROPERTY_INTROSPECTION
/*!
 * \brief Return a human readable diagnostic message how exactly a
 *        property was defined.
 *
 * This is only enabled if the NO_PROPERTY_INTROSPECTION macro is not
 * defined.
 *
 * Example:
 *
396
 * \code
397
398
399
400
 * int main()
 * {
 *    std::cout << PROP_DIAGNOSTIC(FooBarTypeTag, blabbPropTag) << "\n";
 * };
401
 * \endcode
402
 */
403
404
405
#define PROP_DIAGNOSTIC(TypeTag, PropTagName) \
    ::Dumux::Properties::getDiagnostic<TypeTag>(#PropTagName)

406
407
408
409
410
411
412
413
414
415
#else
/*!
 * \brief Return a human readable diagnostic message how exactly a
 *        property was defined.
 *
 * This is only enabled if the NO_PROPERTY_INTROSPECTION macro is not
 * defined.
 *
 * Example:
 *
416
 * \code
417
418
419
420
 * int main()
 * {
 *    std::cout << PROP_DIAGNOSTIC(FooBarTypeTag, blabbPropTag) << "\n";
 * };
421
 * \endcode
422
 */
423
#define PROP_DIAGNOSTIC(TypeTag, PropTagName) "Property introspection disabled by NO_PROPERTY_INTROSPECTION"
424
425
426
427
428
429
430
431
#endif


//////////////////////////////////////////////
// some serious template kung fu. Don't look at it too closely, it
// might damage your brain!
//////////////////////////////////////////////

Thomas Fetzer's avatar
Thomas Fetzer committed
432
//! \cond false
433

434
435
436
namespace PTag {}
namespace TTag {}

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
#if !defined NO_PROPERTY_INTROSPECTION

namespace TTag
{
template <class EffTypeTag>
struct TypeTagInfo
{};
}

template <class EffTypeTagName, class PropTagName>
struct PropertyInfo
{};
class PropertyRegistryKey
{
public:
    PropertyRegistryKey()
    {};

    PropertyRegistryKey(const std::string &effTypeTagName,
                        const std::string &propertyKind,
                        const std::string &propertyName,
                        const std::string &propertyValue,
                        const std::string &fileDefined,
                        int lineDefined)
        : effTypeTagName_(effTypeTagName)
        , propertyKind_(propertyKind)
        , propertyName_(propertyName)
        , propertyValue_(propertyValue)
        , fileDefined_(fileDefined)
        , lineDefined_(lineDefined)
    {
    };

    // copy constructor
    PropertyRegistryKey(const PropertyRegistryKey &v)
        : effTypeTagName_(v.effTypeTagName_)
        , propertyKind_(v.propertyKind_)
        , propertyName_(v.propertyName_)
        , propertyValue_(v.propertyValue_)
        , fileDefined_(v.fileDefined_)
        , lineDefined_(v.lineDefined_)
    {};

    const std::string &effTypeTagName() const
    { return effTypeTagName_; }
    const std::string &propertyKind() const
    { return propertyKind_; }
    const std::string &propertyName() const
    { return propertyName_; }
486
    const std::string &propertyValue() const
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
    { return propertyValue_; }
    const std::string &fileDefined() const
    { return fileDefined_; }
    int lineDefined() const
    { return lineDefined_; }

private:
    std::string effTypeTagName_;
    std::string propertyKind_;
    std::string propertyName_;
    std::string propertyValue_;
    std::string fileDefined_;
    int lineDefined_;
};

class PropertyRegistry
{
public:
    typedef std::map<std::string, PropertyRegistryKey> KeyList;
    typedef std::map<std::string, KeyList> KeyListMap;

    static void addKey(const PropertyRegistryKey &key)
    {
        keys_[key.effTypeTagName()][key.propertyName()] = key;
    }

    static const PropertyRegistryKey &getKey(const std::string &effTypeTagName,
                                             const std::string &propertyName)
    {
        return keys_[effTypeTagName][propertyName];
    };

    static const KeyList &getKeys(const std::string &effTypeTagName)
    {
        return keys_[effTypeTagName];
    };

private:
    static KeyListMap keys_;
};
PropertyRegistry::KeyListMap PropertyRegistry::keys_;

class TypeTagRegistry
{
public:
    typedef std::list<std::string> ChildrenList;
    typedef std::map<std::string, ChildrenList> ChildrenListMap;
534

535
    template <class TypeTag, class Child1, class Child2, class Child3, class Child4, class Child5, class Dummy>
536
537
538
539
540
541
542
543
544
545
546
547
548
    static void addChildren()
    {
        std::string typeTagName = Dune::className<TypeTag>();
        if (typeid(Child1) != typeid(void))
            keys_[typeTagName].push_front(Dune::className<Child1>());
        if (typeid(Child2) != typeid(void))
            keys_[typeTagName].push_front(Dune::className<Child2>());
        if (typeid(Child3) != typeid(void))
            keys_[typeTagName].push_front(Dune::className<Child3>());
        if (typeid(Child4) != typeid(void))
            keys_[typeTagName].push_front(Dune::className<Child4>());
        if (typeid(Child5) != typeid(void))
            keys_[typeTagName].push_front(Dune::className<Child5>());
549
    }
550

551
    template <class TypeTag, class Child1, class Child2, class Child3, class Child4, class Dummy>
552
    static void addChildren()
553
    { addChildren<TypeTag, Child1, Child2, Child3, Child4, void, Dummy>(); }
554

555
    template <class TypeTag, class Child1, class Child2, class Child3, class Dummy>
556
    static void addChildren()
557
    { addChildren<TypeTag, Child1, Child2, Child3, void, void, Dummy>(); }
558

559
    template <class TypeTag, class Child1, class Child2, class Dummy>
560
    static void addChildren()
561
    { addChildren<TypeTag, Child1, Child2, void, void,  void, Dummy>(); }
562

563
    template <class TypeTag, class Child1, class Dummy>
564
    static void addChildren()
565
    { addChildren<TypeTag, Child1, void, void, void, void, Dummy>(); }
566

567
    template <class TypeTag, class Dummy>
568
    static void addChildren()
569
    { addChildren<TypeTag, void, void, void, void, void, Dummy>(); }
570

571
572
573
574
    static const ChildrenList &children(const std::string &typeTagName)
    {
        return keys_[typeTagName];
    };
575

576
577
578
579
580
private:
    static ChildrenListMap keys_;
};

TypeTagRegistry::ChildrenListMap TypeTagRegistry::keys_;
581

582
583
#endif // !defined NO_PROPERTY_INTROSPECTION

584
585
using std::is_void;
using std::is_base_of;
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622

// logical AND, OR and NOT operations to be used for template meta programming
template <bool b1, bool b2, bool b3 = true, bool b4 = true, bool b5 = true, bool b6 = true, bool b7 = true>
struct ice_and
{
    static const bool value = false;
};

template <>
struct ice_and<true, true, true, true, true, true, true>
{
    static const bool value = true;
};

template <bool b1, bool b2, bool b3 = false, bool b4 = false, bool b5 = false, bool b6 = false, bool b7 = false>
struct ice_or
{
    static const bool value = true;
};

template <>
struct ice_or<false, false, false, false, false, false, false>
{
    static const bool value = false;
};

template <bool b>
struct ice_not
{
    static const bool value = false;
};

template <>
struct ice_not<false>
{
    static const bool value = true;
};
623

624
//! \internal
625
class PropertyUndefined {};
626
//! \internal
627
628
class PropertyExplicitlyUnset {};

629
//! \internal
630
631
632
633
634
635
636
template <class RealTypeTag,
          class EffectiveTypeTag,
          class PropertyTag>
struct Property : public PropertyUndefined
{
};

637
//! \internal
638
639
640
641
642
643
template <class EffectiveTypeTag,
          class PropertyTag>
struct PropertyUnset : public PropertyUndefined
{
};

644
//! \internal
645
646
647
648
649
650
template <class RealTypeTag,
          class PropertyTag>
struct DefaultProperty : public PropertyUndefined
{
};

651
//! \internal
652
653
654
655
656
657
658
659
660
661
template <class Tree, class PropertyTag>
struct propertyExplicitlyUnset
{
    const static bool value =
        is_base_of<PropertyExplicitlyUnset,
                   PropertyUnset<typename Tree::SelfType,
                                 PropertyTag>
                   >::value;
};

662
//! \internal
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
template <class Tree, class PropertyTag>
class propertyExplicitlyUnsetOnTree
{
    static const bool explicitlyUnset = propertyExplicitlyUnset<Tree, PropertyTag>::value;

    static const bool isLeaf = ice_and<is_void<typename Tree::Child1>::value,
                                       is_void<typename Tree::Child2>::value,
                                       is_void<typename Tree::Child3>::value,
                                       is_void<typename Tree::Child4>::value,
                                       is_void<typename Tree::Child5>::value >::value;

public:
    static const bool value =
        ice_or<explicitlyUnset,
               ice_and<ice_not<isLeaf>::value,
                       propertyExplicitlyUnsetOnTree<typename Tree::Child1, PropertyTag>::value,
                       propertyExplicitlyUnsetOnTree<typename Tree::Child2, PropertyTag>::value,
                       propertyExplicitlyUnsetOnTree<typename Tree::Child3, PropertyTag>::value,
                       propertyExplicitlyUnsetOnTree<typename Tree::Child4, PropertyTag>::value,
                       propertyExplicitlyUnsetOnTree<typename Tree::Child5, PropertyTag>::value
                       >::value
               >::value;
};

687
//! \internal
688
689
690
template <class PropertyTag>
struct propertyExplicitlyUnsetOnTree<void, PropertyTag>
{
691
    const static bool value = std::true_type::value;
692
693
};

694
//! \internal
695
696
697
698
699
700
701
702
703
704
705
706
template <class RealTypeTag, class Tree, class PropertyTag>
struct propertyDefinedOnSelf
{
    const static bool value =
        ice_not<is_base_of<PropertyUndefined,
                           Property<RealTypeTag,
                                    typename Tree::SelfType,
                                    PropertyTag>
                           >::value
                           >::value;
};

707
//! \internal
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
template <class RealTypeTag, class Tree, class PropertyTag>
class propertyDefinedOnTree
{
    static const bool notExplicitlyUnset =
        ice_not<propertyExplicitlyUnsetOnTree<Tree,
                                              PropertyTag>::value >::value;

public:
    static const bool value =
        ice_and<notExplicitlyUnset,
                ice_or<propertyDefinedOnSelf<RealTypeTag, Tree, PropertyTag>::value,
                       propertyDefinedOnTree<RealTypeTag, typename Tree::Child1, PropertyTag>::value,
                       propertyDefinedOnTree<RealTypeTag, typename Tree::Child2, PropertyTag>::value,
                       propertyDefinedOnTree<RealTypeTag, typename Tree::Child3, PropertyTag>::value,
                       propertyDefinedOnTree<RealTypeTag, typename Tree::Child4, PropertyTag>::value,
                       propertyDefinedOnTree<RealTypeTag, typename Tree::Child5, PropertyTag>::value
                       >::value >::value;
};

727
//! \internal
728
729
730
731
template <class RealTypeTag, class PropertyTag>
class propertyDefinedOnTree<RealTypeTag, void, PropertyTag>
{
public:
732
    static const bool value = std::false_type::value;
733
734
};

735
//! \internal
736
737
738
739
740
741
742
743
744
745
746
template <class RealTypeTag, class PropertyTag>
struct defaultPropertyDefined
{
    const static bool value =
        ice_not<is_base_of<PropertyUndefined,
                           DefaultProperty<RealTypeTag,
                                           PropertyTag>
                           >::value
                           >::value;
};

747
//! \internal
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
template <class RealTypeTag, class Tree, class PropertyTag>
class defaultPropertyDefinedOnTree
{
    static const bool isLeaf = ice_and<is_void<typename Tree::Child1>::value,
                                       is_void<typename Tree::Child2>::value,
                                       is_void<typename Tree::Child3>::value,
                                       is_void<typename Tree::Child4>::value,
                                       is_void<typename Tree::Child5>::value >::value;

    static const bool explicitlyUnset =
        propertyExplicitlyUnsetOnTree<Tree, PropertyTag>::value;

public:
    static const bool value =
        ice_and<ice_not<explicitlyUnset>::value,
                ice_or<ice_and<isLeaf,defaultPropertyDefined<RealTypeTag, PropertyTag>::value >::value,
                       defaultPropertyDefinedOnTree<RealTypeTag,typename Tree::Child1, PropertyTag>::value,
                       defaultPropertyDefinedOnTree<RealTypeTag,typename Tree::Child2, PropertyTag>::value,
                       defaultPropertyDefinedOnTree<RealTypeTag,typename Tree::Child3, PropertyTag>::value,
                       defaultPropertyDefinedOnTree<RealTypeTag,typename Tree::Child4, PropertyTag>::value,
                       defaultPropertyDefinedOnTree<RealTypeTag,typename Tree::Child5, PropertyTag>::value
                       >::value >::value;
};

772
//! \internal
773
774
775
template <class RealTypeTag, class PropertyTag>
struct defaultPropertyDefinedOnTree<RealTypeTag,void, PropertyTag>
{
776
    static const bool value = std::false_type::value;
777
778
};

779
//! \internal
780
781
782
783
template <class RealTypeTag, class Tree, class PropertyTag>
class propertyDefined
{
public:
784
    static const bool onSelf = propertyDefinedOnSelf<RealTypeTag,Tree,PropertyTag>::value;
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803

    static const bool onChild1 = propertyDefinedOnTree<RealTypeTag,typename Tree::Child1,PropertyTag>::value;
    static const bool onChild2 = propertyDefinedOnTree<RealTypeTag,typename Tree::Child2,PropertyTag>::value;
    static const bool onChild3 = propertyDefinedOnTree<RealTypeTag,typename Tree::Child3,PropertyTag>::value;
    static const bool onChild4 = propertyDefinedOnTree<RealTypeTag,typename Tree::Child4,PropertyTag>::value;
    static const bool onChild5 = propertyDefinedOnTree<RealTypeTag,typename Tree::Child5,PropertyTag>::value;

    static const bool asDefault =
        defaultPropertyDefinedOnTree<RealTypeTag, Tree,PropertyTag>::value;

    static const bool onChildren =
        ice_or<onChild1,
               onChild2,
               onChild3,
               onChild4,
               onChild5
               >::value;

    static const bool value =
804
        ice_or<onSelf ,
805
806
807
808
809
               onChildren>::value;


};

810
//! \internal
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
template <class RealTypeTag, class Tree, class PropertyTag>
class propertyTagIndex
{
    typedef propertyDefined<RealTypeTag, Tree, PropertyTag> definedWhere;

public:
    static const int value =
        definedWhere::onSelf ? 0 :
        ( definedWhere::onChild5 ? 5 :
          ( definedWhere::onChild4 ? 4 :
            ( definedWhere::onChild3 ? 3 :
              ( definedWhere::onChild2 ? 2 :
                ( definedWhere::onChild1 ? 1 :
                  ( definedWhere::asDefault ? -1 :
                    -1000))))));
};


829
//! \internal
830
831
832
833
834
835
836
837
838
template <class SelfT,
          class Child1T = void,
          class Child2T = void,
          class Child3T = void,
          class Child4T = void,
          class Child5T = void>
class TypeTag
{
public:
839
    typedef SelfT SelfType;
840

841
842
843
844
845
    typedef Child1T Child1;
    typedef Child2T Child2;
    typedef Child3T Child3;
    typedef Child4T Child4;
    typedef Child5T Child5;
846
847
};

848
849
NEW_TYPE_TAG(__Default);

850
//! \internal
851
852
853
854
855
856
857
858
859
template <class EffectiveTypeTag,
          class PropertyTag,
          class RealTypeTag=EffectiveTypeTag,
          int tagIndex = propertyTagIndex<RealTypeTag, EffectiveTypeTag, PropertyTag>::value >
struct GetProperty
{
};

// property not defined, but a default property is available
860
//! \internal
861
862
863
864
865
866
867
template <class TypeTag, class PropertyTag, class RealTypeTag>
struct GetProperty<TypeTag, PropertyTag, RealTypeTag, -1>
{
    typedef DefaultProperty<RealTypeTag, PropertyTag>  p;
};

// property defined on self
868
//! \internal
869
870
871
872
873
874
template <class TypeTag, class PropertyTag, class RealTypeTag>
struct GetProperty<TypeTag, PropertyTag, RealTypeTag, 0>
{
    typedef Property<RealTypeTag, TypeTag, PropertyTag>   p;
};

875
//! \internal
876
877
878
template <class TypeTag, class PropertyTag, class RealTypeTag>
struct GetProperty<TypeTag, PropertyTag, RealTypeTag, 1>
{
879
    typedef typename GetProperty<typename TypeTag::Child1, PropertyTag, RealTypeTag>::p p;
880
881
};

882
//! \internal
883
884
885
template <class TypeTag, class PropertyTag, class RealTypeTag>
struct GetProperty<TypeTag, PropertyTag, RealTypeTag, 2>
{
886
    typedef typename GetProperty<typename TypeTag::Child2, PropertyTag, RealTypeTag>::p p;
887
888
};

889
//! \internal
890
891
892
template <class TypeTag, class PropertyTag, class RealTypeTag>
struct GetProperty<TypeTag, PropertyTag, RealTypeTag, 3>
{
893
    typedef typename GetProperty<typename TypeTag::Child3, PropertyTag, RealTypeTag>::p p;
894
895
};

896
//! \internal
897
898
899
template <class TypeTag, class PropertyTag, class RealTypeTag>
struct GetProperty<TypeTag, PropertyTag, RealTypeTag, 4>
{
900
    typedef typename GetProperty<typename TypeTag::Child4, PropertyTag, RealTypeTag>::p p;
901
902
};

903
//! \internal
904
905
906
template <class TypeTag, class PropertyTag, class RealTypeTag>
struct GetProperty<TypeTag, PropertyTag, RealTypeTag, 5>
{
907
    typedef typename GetProperty<typename TypeTag::Child5, PropertyTag, RealTypeTag>::p p;
908
909
910
};

#if !defined NO_PROPERTY_INTROSPECTION
911
std::string canonicalTypeTagNameToName_(const std::string &canonicalName)
912
{
913
914
915
916
917
918
919
920
921
    std::string result(canonicalName);
    result.replace(0, strlen("Dumux::Properties::TTag::"), "");
    return result;
}

inline bool getDiagnostic_(const std::string &typeTagName,
                           const std::string &propTagName,
                           std::string &result,
                           const std::string indent)
922
{
923
924
925
926
927
928
929
930
    const PropertyRegistryKey *key = 0;

    const PropertyRegistry::KeyList &keys =
        PropertyRegistry::getKeys(typeTagName);
    PropertyRegistry::KeyList::const_iterator it = keys.begin();
    for (; it != keys.end(); ++it) {
        if (it->second.propertyName() == propTagName) {
            key = &it->second;
931
            break;
932
        }
933
    }
934

935
    if (key) {
936
        std::ostringstream oss;
Andreas Lauser's avatar
Andreas Lauser committed
937
        oss << indent
938
939
940
941
942
            << key->propertyKind() << " "
            << key->propertyName() << " defined on '"
            << canonicalTypeTagNameToName_(key->effTypeTagName()) << "' at "
            << key->fileDefined() << ":" << key->lineDefined() << "\n";
        result = oss.str();
943
        return true;
944
945
    }

946
947
948
949
950
951
952
    // print properties defined on children
    typedef TypeTagRegistry::ChildrenList ChildrenList;
    const ChildrenList &children = TypeTagRegistry::children(typeTagName);
    ChildrenList::const_iterator ttagIt = children.begin();
    std::string newIndent = indent + "  ";
    for (; ttagIt != children.end(); ++ttagIt) {
        if (getDiagnostic_(*ttagIt, propTagName, result, newIndent)) {
953
            result.insert(0, indent + "Inherited from " + canonicalTypeTagNameToName_(typeTagName) + "\n");
954
            return true;
955
956
957
        }
    }

958
959
960
961
962
    return false;
}

template <class TypeTag>
const std::string getDiagnostic(std::string propTagName)
963
{
964
965
966
967
968
969
970
971
    std::string result;

    std::string TypeTagName(Dune::className<TypeTag>());

    propTagName.replace(0, strlen("PTag("), "");
    int n = propTagName.length();
    propTagName.replace(n - 1, 1, "");
    //TypeTagName.replace(0, strlen("Dumux::Properties::TTag::"), "");
972

973
974
975
976
977
978
979
980
981
    if (!getDiagnostic_(TypeTagName, propTagName, result, "")) {
        // check whether the property is a default property
        const PropertyRegistry::KeyList &keys =
            PropertyRegistry::getKeys(Dune::className<TTAG(__Default)>());
        PropertyRegistry::KeyList::const_iterator it = keys.begin();
        for (; it != keys.end(); ++it) {
            const PropertyRegistryKey &key = it->second;
            if (key.propertyName() != propTagName)
                continue; // property already printed
Andreas Lauser's avatar
Andreas Lauser committed
982

983
984
985
986
987
988
            std::ostringstream oss;
            oss << "fallback " << key.propertyName()
                << " defined at " << key.fileDefined()
                << ":" << key.lineDefined()
                <<"\n";
            result = oss.str();
989
        }
990
    }
991
992


993
    return result;
994
}
995

996
997
998
999
1000
inline void print_(const std::string &typeTagName,
                   std::ostream &os,
                   const std::string indent,
                   std::set<std::string> &printedProperties)
{
For faster browsing, not all history is shown. View entire blame