IOSS  2.0
Ioss_VariableType.h
Go to the documentation of this file.
1 // Copyright(C) 1999-2017 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 //
17 // * Neither the name of NTESS nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 #ifndef IOSS_Ioss_VariableType_h
34 #define IOSS_Ioss_VariableType_h
35 
36 #include <Ioss_CodeTypes.h>
37 #include <cstring> // for strncmp, strncpy
38 #include <functional> // for less
39 #include <map> // for map, map<>::value_compare
40 #include <string> // for string, operator<
41 #include <vector> // for vector
42 
43 namespace Ioss {
44  class VariableType;
45 } // namespace Ioss
46 
47 namespace Ioss {
48  using VariableTypeMap = std::map<std::string, VariableType *, std::less<std::string>>;
49  using VTM_ValuePair = VariableTypeMap::value_type;
50 
51  class Registry
52  {
53  public:
54  void insert(const Ioss::VTM_ValuePair &value, bool delete_me);
55  VariableTypeMap::iterator begin() { return m_registry.begin(); }
56  VariableTypeMap::iterator end() { return m_registry.end(); }
57  VariableTypeMap::iterator find(const std::string &type) { return m_registry.find(type); }
58 
59  ~Registry();
60  std::map<std::string, std::string> customFieldTypes;
61 
62  private:
64  std::vector<Ioss::VariableType *> m_deleteThese;
65  };
66 
67 #define MAX_SUFFIX 8
68  struct Suffix
69  {
70  explicit Suffix(const char new_data[MAX_SUFFIX])
71  {
72  std::strncpy(m_data, new_data, MAX_SUFFIX);
73  m_data[MAX_SUFFIX] = '\0';
74  }
75  explicit Suffix(const std::string &new_data)
76  {
77  std::strncpy(m_data, new_data.c_str(), MAX_SUFFIX);
78  m_data[MAX_SUFFIX] = '\0';
79  }
80  bool operator==(const std::string &str) const
81  {
82  return std::strncmp(m_data, str.c_str(), MAX_SUFFIX) == 0;
83  }
84  bool operator!=(const std::string &str) const
85  {
86  return std::strncmp(m_data, str.c_str(), MAX_SUFFIX) != 0;
87  }
88  char m_data[MAX_SUFFIX + 1]{};
89  };
90 
91  /** \brief A generic variable type
92  */
94  {
95  public:
96  static void alias(const std::string &base, const std::string &syn);
97  static int describe(NameList *names);
98  static bool create_named_suffix_field_type(const std::string & type_name,
99  const std::vector<std::string> &suffices);
100  static bool get_field_type_mapping(const std::string &field, std::string *type);
101  static bool add_field_type_mapping(const std::string &raw_field, const std::string &raw_type);
102 
103  virtual ~VariableType();
104  int component_count() const;
105 
106  // Override this function if the derived class has no suffices
107  // For example, a 'vector_2d' has suffices "x" and "y"
108  // A 'quad4' has no suffices...
109  virtual int suffix_count() const;
110  std::string name() const;
111 
112  static std::string numeric_label(int which, int ncomp, const std::string &name);
113  virtual std::string label(int which, char suffix_sep = '_') const = 0;
114  virtual std::string label_name(const std::string &base, int which, char suffix_sep = '_') const;
115  virtual bool match(const std::vector<Suffix> &suffices) const;
116 
117  static const VariableType *factory(const std::string &raw_name, int copies = 1);
118  static const VariableType *factory(const std::vector<Suffix> &suffices);
119 
120  protected:
121  VariableType(const std::string &type, int comp_count, bool delete_me = false);
122  static Registry &registry();
123 
124  private:
125  const std::string name_;
127 
128  VariableType(const VariableType &) = delete;
129  VariableType &operator=(const VariableType &) = delete;
130 
131  static bool build_variable_type(const std::string &raw_type);
132  };
133 } // namespace Ioss
134 inline std::string Ioss::VariableType::name() const { return name_; }
135 
136 inline int Ioss::VariableType::component_count() const { return componentCount; }
137 
138 inline int Ioss::VariableType::suffix_count() const { return componentCount; }
139 #endif
std::vector< std::string > NameList
Definition: Ioss_CodeTypes.h:44
Definition: Ioss_VariableType.h:51
Suffix(const std::string &new_data)
Definition: Ioss_VariableType.h:75
The main namespace for the Ioss library.
Definition: Iocgns_DatabaseIO.h:50
VariableTypeMap::iterator begin()
Definition: Ioss_VariableType.h:55
virtual int suffix_count() const
Definition: Ioss_VariableType.h:138
void insert(const Ioss::VTM_ValuePair &value, bool delete_me)
Definition: Ioss_VariableType.C:50
int componentCount
Definition: Ioss_VariableType.h:126
std::string name() const
Definition: Ioss_VariableType.h:134
std::map< std::string, std::string > customFieldTypes
Definition: Ioss_VariableType.h:60
VariableTypeMap::iterator end()
Definition: Ioss_VariableType.h:56
#define MAX_SUFFIX
Definition: Ioss_VariableType.h:67
Ioss::VariableTypeMap m_registry
Definition: Ioss_VariableType.h:63
A generic variable type.
Definition: Ioss_VariableType.h:93
~Registry()
Definition: Ioss_VariableType.C:58
bool operator!=(const std::string &str) const
Definition: Ioss_VariableType.h:84
Suffix(const char new_data[MAX_SUFFIX])
Definition: Ioss_VariableType.h:70
int component_count() const
Definition: Ioss_VariableType.h:136
size_t match(const char *name1, const char *name2)
Definition: Ioex_Utils.C:44
Definition: Ioss_VariableType.h:68
VariableTypeMap::value_type VTM_ValuePair
Definition: Ioss_VariableType.h:49
std::vector< Ioss::VariableType * > m_deleteThese
Definition: Ioss_VariableType.h:64
std::string name(Ioss::GroupingEntity *entity)
Definition: io_info.C:71
std::map< std::string, VariableType *, std::less< std::string > > VariableTypeMap
Definition: Ioss_VariableType.h:48
bool operator==(const std::string &str) const
Definition: Ioss_VariableType.h:80
VariableTypeMap::iterator find(const std::string &type)
Definition: Ioss_VariableType.h:57
const std::string name_
Definition: Ioss_VariableType.h:125