Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterSet.cpp
Go to the documentation of this file.
1 /* *****************************************************************
2  MESQUITE -- The Mesh Quality Improvement Toolkit
3 
4  Copyright 2004 Sandia Corporation and Argonne National
5  Laboratory. Under the terms of Contract DE-AC04-94AL85000
6  with Sandia Corporation, the U.S. Government retains certain
7  rights in this software.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  (lgpl.txt) along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  diachin2@llnl.gov, djmelan@sandia.gov, mbrewer@sandia.gov,
24  pknupp@sandia.gov, tleurent@mcs.anl.gov, tmunson@mcs.anl.gov
25 
26  ***************************************************************** */
27 #include "ParameterSet.hpp"
28 #ifdef MSQ_USE_OLD_C_HEADERS
29 # include <string.h>
30 #else
31 # include <cstring>
32 #endif
33 
34 using namespace Mesquite;
35 
37 {
43 };
44 
46 {
47  int intVal;
48  double dblVal;
49  void* ptrVal;
50  char* strVal;
51  bool boolVal;
52 };
53 
55 {
56  char* name;
59 };
60 
61 
63  : mParameterArray(NULL),
64  mNumParameters(0)
65 {
66 }
67 
69 {
70  while (mNumParameters--)
71  {
73  // If it's a string, free the string value
75  mParameterArray[mNumParameters].value.strVal != NULL)
76  {
78  }
79  }
80 
81  delete mParameterArray;
82 }
83 
84 msq_stdc::size_t ParameterSet::get_parameter_index(const char* name, MsqError &err)
85 {
86  // Search for a parameter with the same name
87  for (size_t i = 0; (i < mNumParameters; ++i)
88  if (!msq_stdc::strcmp(mParameterArray[i].name, name))
89  return i;
90 
92  return mNumParameters;
93 }
94 
95 void ParameterSet::remove_parameter(const char* name, MsqError &err)
96 {
97  // Make sure it exists
98  size_t index = get_parameter_index(name); MSQ_ERRRTN(err);
99 
100  // Free the string holding the name
101  delete [] mParameterArray[index].name;
102 
103  // If it's a string, free the string value
104  if (mParameterArray[index].type == MSQ_STRING &&
105  mParameterArray[index].value.strVal != NULL)
106  {
107  delete [] mParameterArray[index].value.strVal;
108  }
109 
110  // If it isn't at the end, move the last parameter
111  // into the spot this one was using
112  if (index != mNumParameters - 1)
113  {
115  }
116 
117  mNumParameters--;
118 }
119 
120 void ParameterSet::generic_add_parameter(const char* name, MsqError &err)
121 {
122  // Make sure it doesn't already exist
123  if(get_parameter_index(name, err) == mNumParameters) {
124  err.clear();
125  } else {
127  return;
128  }
129 
130  // Make the array big enough
131  ParameterRecord* new_array = new ParameterRecord[mNumParameters + 1];
132  // Copy the old into the new
133  memcpy(new_array, mParameterArray, mNumParameters*sizeof(ParameterRecord));
134  // Toss the old
135  delete [] mParameterArray;
136  // Save the new
137  mNumParameters++;
138  mParameterArray = new_array;
139 
140  // Now add the new parameter, with no initial value
141  mParameterArray[mNumParameters - 1].name = new char[strlen(name) + 1];
142  strcpy(mParameterArray[mNumParameters - 1].name, name);
143 
144  return;
145 }
146 
147 void ParameterSet::add_int_parameter(const char* name,
148  int initial_value, MsqError &err)
149 {
150  generic_add_parameter(name, err);
151  MSQ_ERRRTN(err);
152 
154  mParameterArray[mNumParameters - 1].value.intVal = initial_value;
155 
156  return;
157 }
158 
159 void ParameterSet::set_int_parameter(const char* name,
160  int value, MsqError &err)
161 {
162  // Make sure it exists
163  size_t index = get_parameter_index(name, err);
164  MSQ_ERRRTN(err);
165 
166  // Make sure it's the right type
167  if (mParameterArray[index].type != MSQ_INT) {
169  return;
170  }
171 
172  // Set the value
173  mParameterArray[index].value.intVal = value;
174 }
175 
176 void ParameterSet::get_int_parameter(const char* name,
177  int* value, MsqError &err)
178 {
179  // Make sure it exists
180  size_t index = get_parameter_index(name, err);
181  MSQ_ERRRTN(err);
182 
183  // Make sure it's the right type
184  if (mParameterArray[index].type != MSQ_INT) {
186  return;
187  }
188 
189  // Get the value
190  *value = *reinterpret_cast<int*>(&(mParameterArray[index].value));
191 
192  return;
193 }
void generic_add_parameter(const char *name, MsqError &err)
Used to hold the error state and return it to the application.
msq_stdc::size_t get_parameter_index(const char *name, MsqError &err)
void remove_parameter(const char *name, MsqError &err)
void set_int_parameter(const char *name, int value, MsqError &err)
invalid function argument passed
void add_int_parameter(const char *name, int initial_value, MsqError &err)
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
void get_int_parameter(const char *name, int *value, MsqError &err)
blockLoc i
Definition: read.cpp:79
ParameterType
ParameterValue value
ParameterType type
void clear()
resets error object to non-active state (no error).
#define MSQ_ERRRTN(err)
If passed error is true, return from a void function.