Salome HOME
Update copyrights
[modules/geom.git] / src / XAO / XAO_Field.cxx
1 // Copyright (C) 2013-2019  CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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),
39     m_nbComponents(nbComponents), m_components(nbComponents, ""),
40     m_nbElements(nbElements)
41 {
42 }
43
44 Field::~Field()
45 {
46   for (unsigned int i = 0; i < m_steps.size(); ++i)
47     delete m_steps[i];
48 }
49
50 Field* Field::createField(const XAO::Type& type, const XAO::Dimension& dimension,
51         const int& nbElements, const int& nbComponents, const std::string& name)
52 throw (XAO_Exception)
53 {
54     if (type == XAO::BOOLEAN)
55         return new BooleanField(dimension, nbElements, nbComponents, name);
56     if (type == XAO::INTEGER)
57         return new IntegerField(dimension, nbElements, nbComponents, name);
58     if (type == XAO::DOUBLE)
59         return new DoubleField(dimension, nbElements, nbComponents, name);
60     if (type == XAO::STRING)
61         return new StringField(dimension, nbElements, nbComponents, name);
62
63     throw XAO_Exception(MsgBuilder() << "Bad Type: " << type);
64 }
65
66 const std::string Field::getComponentName(const int& index)
67 throw (XAO_Exception)
68 {
69     checkComponent(index);
70     return m_components[index];
71 }
72
73 void Field::setComponentName(const int& index, const std::string& name)
74 throw (XAO_Exception)
75 {
76     checkComponent(index);
77     m_components[index] = name;
78 }
79
80 void Field::setComponentsNames(const std::vector<std::string>& names)
81 throw (XAO_Exception)
82 {
83     for (unsigned int  i = 0; i < names.size(); ++i)
84     {
85       if ((int)i < m_nbComponents)
86             m_components[i] = names[i];
87     }
88 }
89
90 bool Field::removeStep(Step* step)
91 {
92     std::vector<Step*>::iterator it = m_steps.begin();
93     for (; it != m_steps.end(); ++it)
94     {
95         Step* current = *it;
96         if (step == current)
97         {
98             m_steps.erase(it);
99             return true;
100         }
101     }
102
103     return false;
104 }
105
106 bool Field::hasStep(const int& step)
107 {
108     std::vector<Step*>::iterator it = m_steps.begin();
109     for (; it != m_steps.end(); ++it)
110     {
111         Step* current = *it;
112         if (current->getStep() == step)
113             return true;
114     }
115
116     return false;
117 }
118
119 void Field::checkComponent(const int& component)
120 throw (XAO_Exception)
121 {
122     if (component < m_nbComponents && component >= 0)
123         return;
124
125     throw XAO_Exception(MsgBuilder() << "Step index is out of range [0, "
126                                      << m_nbComponents-1 << "]: " << component);
127 }
128
129 void Field::checkStepIndex(const int& step)
130 throw (XAO_Exception)
131 {
132   if (step < (int)m_steps.size() && step >= 0)
133         return;
134
135     throw XAO_Exception(MsgBuilder() << "Step index is out of range [0, "
136                                      << m_steps.size()-1 << "]: " << step);
137 }