Salome HOME
1e5f27ace55dbb1eeb27f900f69084b717ee7169
[modules/geom.git] / src / XAO / XAO_Field.cxx
1 // Copyright (C) 2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Frederic Pons (OpenCascade)
20
21 #include <string>
22 #include <iostream>
23
24 #include "XAO_Xao.hxx"
25 #include "XAO_Field.hxx"
26 #include "XAO_BooleanField.hxx"
27 #include "XAO_IntegerField.hxx"
28 #include "XAO_DoubleField.hxx"
29 #include "XAO_StringField.hxx"
30 #include "XAO_XaoUtils.hxx"
31
32 using namespace XAO;
33
34 // -------------------------------------------------------
35
36 Field::Field(const XAO::Dimension& dimension,
37         const int& nbElements, const int& nbComponents, const std::string& name)
38     : m_name(name), m_dimension(dimension), m_nbElements(nbElements), m_nbComponents(nbComponents)
39 {
40     m_components.reserve(nbComponents);
41     for (int i = 0; i < nbComponents; ++i)
42         m_components.push_back("");
43 }
44
45 Field::~Field()
46 {
47     for (unsigned int i = 0; i < m_steps.size(); ++i)
48         delete m_steps[i];
49 }
50
51 Field* Field::createField(const XAO::Type& type, const XAO::Dimension& dimension,
52         const int& nbElements, const int& nbComponents, const std::string& name)
53 throw (XAO_Exception)
54 {
55     if (type == XAO::BOOLEAN)
56         return new BooleanField(dimension, nbElements, nbComponents, name);
57     if (type == XAO::INTEGER)
58         return new IntegerField(dimension, nbElements, nbComponents, name);
59     if (type == XAO::DOUBLE)
60         return new DoubleField(dimension, nbElements, nbComponents, name);
61     if (type == XAO::STRING)
62         return new StringField(dimension, nbElements, nbComponents, name);
63
64     throw XAO_Exception(MsgBuilder() << "Bad Type: " << type);
65 }
66
67 const std::string Field::getComponentName(const int& index)
68 throw (XAO_Exception)
69 {
70     checkComponent(index);
71     return m_components[index];
72 }
73
74 void Field::setComponentName(const int& index, const std::string& name)
75 throw (XAO_Exception)
76 {
77     checkComponent(index);
78     m_components[index] = name;
79 }
80
81 void Field::setComponentsNames(const std::vector<std::string>& names)
82 throw (XAO_Exception)
83 {
84     for (unsigned int  i = 0; i < names.size(); ++i)
85     {
86         if (i < m_nbComponents)
87             m_components[i] = names[i];
88     }
89 }
90
91 bool Field::removeStep(Step* step)
92 {
93     std::vector<Step*>::iterator it = m_steps.begin();
94     for (; it != m_steps.end(); ++it)
95     {
96         Step* current = *it;
97         if (step == current)
98         {
99             m_steps.erase(it);
100             return true;
101         }
102     }
103
104     return false;
105 }
106
107 bool Field::hasStep(const int& step)
108 {
109     std::vector<Step*>::iterator it = m_steps.begin();
110     for (; it != m_steps.end(); ++it)
111     {
112         Step* current = *it;
113         if (current->getStep() == step)
114             return true;
115     }
116
117     return false;
118 }
119
120 void Field::checkComponent(const int& component)
121 throw (XAO_Exception)
122 {
123     if (component < m_nbComponents && component >= 0)
124         return;
125
126     throw XAO_Exception(MsgBuilder() << "Step index is out of range [0, "
127                                      << m_nbComponents-1 << "]: " << component);
128 }
129
130 void Field::checkStepIndex(const int& step)
131 throw (XAO_Exception)
132 {
133     if (step < m_steps.size() && step >= 0)
134         return;
135
136     throw XAO_Exception(MsgBuilder() << "Step index is out of range [0, "
137                                      << m_steps.size()-1 << "]: " << step);
138 }