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 <Ioss_Utils.h>
38 #include <cstring> // for strncmp
39 #include <functional> // for less
40 #include <map> // for map, map<>::value_compare
41 #include <string> // for string, operator<
42 #include <vector> // for vector
43
44 namespace Ioss {
45 class VariableType;
46 } // namespace Ioss
47
48 namespace Ioss {
49 using VariableTypeMap = std::map<std::string, VariableType *, std::less<std::string>>;
50 using VTM_ValuePair = VariableTypeMap::value_type;
51
52 class Registry
53 {
54 public:
55 void insert(const Ioss::VTM_ValuePair &value, bool delete_me);
56 VariableTypeMap::iterator begin() { return m_registry.begin(); }
57 VariableTypeMap::iterator end() { return m_registry.end(); }
58 VariableTypeMap::iterator find(const std::string &type) { return m_registry.find(type); }
59
60 ~Registry();
61 std::map<std::string, std::string> customFieldTypes;
62
63 private:
64 Ioss::VariableTypeMap m_registry;
65 std::vector<Ioss::VariableType *> m_deleteThese;
66 };
67
68 #define MAX_SUFFIX 8
69 struct Suffix
70 {
71 explicit Suffix(const char new_data[MAX_SUFFIX]) { Ioss::Utils::copy_string(m_data, new_data); }
72 explicit Suffix(const std::string &new_data) { Ioss::Utils::copy_string(m_data, new_data); }
73 bool operator==(const std::string &str) const
74 {
75 return std::strncmp(m_data, str.c_str(), MAX_SUFFIX) == 0;
76 }
77 bool operator!=(const std::string &str) const
78 {
79 return std::strncmp(m_data, str.c_str(), MAX_SUFFIX) != 0;
80 }
81 char m_data[MAX_SUFFIX + 1]{};
82 };
83
84 /** \brief A generic variable type
85 */
86 class VariableType
87 {
88 public:
89 static void alias(const std::string &base, const std::string &syn);
90 static int describe(NameList *names);
91 static bool create_named_suffix_field_type(const std::string & type_name,
92 const std::vector<std::string> &suffices);
93 static bool get_field_type_mapping(const std::string &field, std::string *type);
94 static bool add_field_type_mapping(const std::string &raw_field, const std::string &raw_type);
95
96 virtual ~VariableType();
97 int component_count() const;
98
99 // Override this function if the derived class has no suffices
100 // For example, a 'vector_2d' has suffices "x" and "y"
101 // A 'quad4' has no suffices...
102 virtual int suffix_count() const;
103 std::string name() const;
104
105 static std::string numeric_label(int which, int ncomp, const std::string &name);
106 virtual std::string label(int which, char suffix_sep = '_') const = 0;
107 virtual std::string label_name(const std::string &base, int which, char suffix_sep = '_') const;
108 virtual bool match(const std::vector<Suffix> &suffices) const;
109
110 static const VariableType *factory(const std::string &raw_name, int copies = 1);
111 static const VariableType *factory(const std::vector<Suffix> &suffices);
112
113 protected:
114 VariableType(const std::string &type, int comp_count, bool delete_me = false);
115 static Registry &registry();
116
117 private:
118 const std::string name_;
119 int componentCount;
120
121 VariableType(const VariableType &) = delete;
122 VariableType &operator=(const VariableType &) = delete;
123
124 static bool build_variable_type(const std::string &raw_type);
125 };
126 } // namespace Ioss
127 inline std::string Ioss::VariableType::name() const { return name_; }
128
129 inline int Ioss::VariableType::component_count() const { return componentCount; }
130
131 inline int Ioss::VariableType::suffix_count() const { return componentCount; }
132 #endif
Ioss::Suffix::Suffix
Suffix(const char new_data[MAX_SUFFIX])
Definition: Ioss_VariableType.h:71
Ioss::NameList
std::vector< std::string > NameList
Definition: Ioss_CodeTypes.h:53
Ioss::VariableType::label
virtual std::string label(int which, char suffix_sep='_') const =0
Ioss::VariableType
A generic variable type.
Definition: Ioss_VariableType.h:86
Ioss::Registry::end
VariableTypeMap::iterator end()
Definition: Ioss_VariableType.h:57
Ioss::Utils::copy_string
static void copy_string(char *dest, char const *source, size_t elements)
Definition: Ioss_Utils.C:1368
Ioss::VariableTypeMap
std::map< std::string, VariableType *, std::less< std::string > > VariableTypeMap
Definition: Ioss_VariableType.h:49
Ioss::Registry::customFieldTypes
std::map< std::string, std::string > customFieldTypes
Definition: Ioss_VariableType.h:61
MAX_SUFFIX
#define MAX_SUFFIX
Definition: Ioss_VariableType.h:68
Ioss::VariableType::add_field_type_mapping
static bool add_field_type_mapping(const std::string &raw_field, const std::string &raw_type)
Definition: Ioss_VariableType.C:111
Ioss
The main namespace for the Ioss library.
Definition: Ioad_DatabaseIO.C:66
Ioss::Suffix::operator==
bool operator==(const std::string &str) const
Definition: Ioss_VariableType.h:73
Ioss::VariableType::suffix_count
virtual int suffix_count() const
Definition: Ioss_VariableType.h:131
Ioss::Registry::~Registry
~Registry()
Definition: Ioss_VariableType.C:61
Ioss::Registry::m_deleteThese
std::vector< Ioss::VariableType * > m_deleteThese
Definition: Ioss_VariableType.h:65
Ioss::VariableType::name
std::string name() const
Definition: Ioss_VariableType.h:127
Ioss::VariableType::name_
const std::string name_
Definition: Ioss_VariableType.h:118
Ioss::VTM_ValuePair
VariableTypeMap::value_type VTM_ValuePair
Definition: Ioss_VariableType.h:50
Ioss::VariableType::numeric_label
static std::string numeric_label(int which, int ncomp, const std::string &name)
Definition: Ioss_VariableType.C:324
Ioss::VariableType::get_field_type_mapping
static bool get_field_type_mapping(const std::string &field, std::string *type)
Definition: Ioss_VariableType.C:149
Ioss::Suffix
Definition: Ioss_VariableType.h:69
Ioss_Utils.h
Ioss::VariableType::~VariableType
virtual ~VariableType()
Ioss::VariableType::label_name
virtual std::string label_name(const std::string &base, int which, char suffix_sep='_') const
Definition: Ioss_VariableType.C:259
Ioss::Registry::find
VariableTypeMap::iterator find(const std::string &type)
Definition: Ioss_VariableType.h:58
Ioss::VariableType::operator=
VariableType & operator=(const VariableType &)=delete
Ioss::Suffix::m_data
char m_data[MAX_SUFFIX+1]
Definition: Ioss_VariableType.h:81
Ioss::VariableType::componentCount
int componentCount
Definition: Ioss_VariableType.h:119
Ioss::Registry
Definition: Ioss_VariableType.h:52
Ioss::Suffix::Suffix
Suffix(const std::string &new_data)
Definition: Ioss_VariableType.h:72
Ioss::VariableType::create_named_suffix_field_type
static bool create_named_suffix_field_type(const std::string &type_name, const std::vector< std::string > &suffices)
Definition: Ioss_VariableType.C:125
Ioss::VariableType::registry
static Registry & registry()
Definition: Ioss_VariableType.C:90
Ioss::Registry::m_registry
Ioss::VariableTypeMap m_registry
Definition: Ioss_VariableType.h:64
Ioss::Registry::begin
VariableTypeMap::iterator begin()
Definition: Ioss_VariableType.h:56
Ioss::VariableType::match
virtual bool match(const std::vector< Suffix > &suffices) const
Definition: Ioss_VariableType.C:242
Ioss::VariableType::build_variable_type
static bool build_variable_type(const std::string &raw_type)
Definition: Ioss_VariableType.C:273
Ioss::VariableType::alias
static void alias(const std::string &base, const std::string &syn)
Definition: Ioss_VariableType.C:81
Ioss::Suffix::operator!=
bool operator!=(const std::string &str) const
Definition: Ioss_VariableType.h:77
Ioss::Registry::insert
void insert(const Ioss::VTM_ValuePair &value, bool delete_me)
Definition: Ioss_VariableType.C:53
Ioss::VariableType::factory
static const VariableType * factory(const std::string &raw_name, int copies=1)
Definition: Ioss_VariableType.C:163
Ioss_CodeTypes.h
Ioss::VariableType::describe
static int describe(NameList *names)
Get the names of variable types known to IOSS.
Definition: Ioss_VariableType.C:101
Ioss::VariableType::VariableType
VariableType(const std::string &type, int comp_count, bool delete_me=false)
Definition: Ioss_VariableType.C:70
Ioss::VariableType::component_count
int component_count() const
Definition: Ioss_VariableType.h:129