File: MultiComponent.h

package info (click to toggle)
sofa-framework 1.0~beta4-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88,688 kB
  • ctags: 27,205
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 67
file content (145 lines) | stat: -rw-r--r-- 5,508 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
                               MultiComponent.h
                             -------------------
    begin             : Wed Aug 8 2001
    copyright         : (C) 2001 TIMC (Emmanuel Promayon, Matthieu Chabanas)
    email             : Emmanuel.Promayon@imag.fr
    Date              : $Date: 2006/10/17 14:33:21 $
    Version           : $Revision: 1.16 $
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

// Allow to compile the stl types without warnings on MSVC
// this line should be BEFORE the #ifndef
//#ifdef WIN32
//#pragma warning(disable:4786) // disable C4786 warning
//#endif


#ifndef MULTICOMPONENT_H
#define MULTICOMPONENT_H

#include <vector>
#include <algorithm>  
#include <cstring>

//using namespace std;

#include "Component.h"

/**
 * A multi-component stores other components, hence providing a way to have
 * an tree representation of components. Isn't that tricky?
 *
 * <b>NOTE: </b> To delete and free the memory of all the sub components, you have to
 * call the deleteAllSubComponents() method.
 *
 *@author Emmanuel Promayon
 * $Revision: 1.16 $
 */
class MultiComponent : public Component {
public:
    /** Default Constructor */
    MultiComponent(PhysicalModel *);
    /// constructor that allows to name the structure (provides a name)
    MultiComponent(PhysicalModel *, std::string);
    /// delete all the subcomponents (call the deleteAllSubComponents method)
    ~MultiComponent();

    unsigned int getNumberOfSubComponents() const;
    Component * getSubComponent(const unsigned int) const;
    void addSubComponent(Component *);
    /**
     * Remove a component from the list.
     * Becareful: this method DOES NOT delete the object and/or free the memory.
      * This method ask the component c to remove this multicomponent from the list of its parent component
     * @param c the ptr to the structure to remove
     * @see removeAllSubComponent()
     */
    void removeSubComponent(Component *c);

    /**
     * this method free all the sub-components (i.e. delete all the sub component
     *  and clear the list).
     * After this methode getNumberOfSubComponents should return 0
     */
    void deleteAllSubComponents();

    /** return true only if the parameter is equal to "MultiComponent" */
    virtual bool isInstanceOf(const char *) const;

    /** print to an output stream in "pseaudo" XML format (do nothing if there are no sub components).
      */
    void xmlPrint(std::ostream &) const;

    /// get the total nr of cell of the component
    unsigned int getNumberOfCells() const;

    /// get cell by order number (not cell index)
    Cell * getCell(unsigned int) const;

    /// conveniant method to get the sub component of the name given in parameter
    Component * getComponentByName(const std::string);
    
    /// return the state of a visibility mode in all the sub component (if at least one sub component is visible for this mode, it will return true; if none are visible it will return false).
    virtual bool isVisible(const RenderingMode::Mode mode) const;

    /// set the state of a visibility mode in all the sub component.
    virtual void setVisible(const RenderingMode::Mode mode, const bool b);

protected:
    /**
     * List of sub component
     */
    std::vector <Component *> components;
};

// -------------------- inline methods -------------------
inline 	unsigned int MultiComponent::getNumberOfSubComponents() const {
    return components.size();
}
inline Component * MultiComponent::getSubComponent(const unsigned int i) const {
    if (i<components.size())
        return components[i];
    else
        return NULL;
}
inline void MultiComponent::addSubComponent(Component * c) {
    // add c in the list
    components.push_back(c);
    // add this in the list of c's composing component
    c->addParentMultiComponent(this);
}
inline void MultiComponent::removeSubComponent(Component *c) {
    std::vector <Component *>::iterator it = std::find(components.begin(), components.end(), c);
    if (it != components.end()) {
        components.erase(it);
        c->removeParentMultiComponent(this);
    }
}
inline Component * MultiComponent::getComponentByName(const std::string n) {
    std::vector <Component *>::iterator it=components.begin();
    Component *foundC=NULL;
    while (it!=components.end() && !foundC) {
        foundC = ((*it)->getName()==n)?(*it):NULL;
        // look inside the component if it is a MultiComponent
        if (!foundC && (*it)->isInstanceOf("MultiComponent")) {
            foundC = ((MultiComponent *) (*it))->getComponentByName(n);
        }
        it++;
    }
    return foundC;
}
inline bool MultiComponent::isInstanceOf(const char *className) const {
    return (strcmp(className, "MultiComponent")==0);
}

#endif //MULTICOMPONENT_H