1 // Copyright(C) 1999-2020 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 // See packages/seacas/LICENSE for details
6 
7 #include <Ioss_CompositeVariableType.h>
8 #include <Ioss_Utils.h>
9 #include <Ioss_VariableType.h>
10 #include <cassert>
11 #include <cstdio>
12 #include <map>
13 #include <string>
14 #include <utility>
15 
16 namespace Ioss {
composite_name(const std::string & base,int copies)17   std::string CompositeVariableType::composite_name(const std::string &base, int copies)
18   {
19     static std::string SEPARATOR("*");
20     std::string        name = base;
21     name += SEPARATOR;
22     name += std::to_string(copies);
23     return name;
24   }
25 
composite_variable_type(const VariableType * inst,int copies)26   VariableType *CompositeVariableType::composite_variable_type(const VariableType *inst, int copies)
27   {
28     VariableType *comp_inst = nullptr;
29 
30     // See if we already constructed this composite type...
31     std::string composite_type = CompositeVariableType::composite_name(inst->name(), copies);
32 
33     auto iter = registry().find(composite_type);
34     if (iter == registry().end()) {
35       // Not found, construct new type...
36       comp_inst = new CompositeVariableType(inst, copies, true);
37     }
38     else {
39       comp_inst = (*iter).second;
40     }
41     return comp_inst;
42   }
43 
CompositeVariableType(const VariableType * base_type,int copies,bool delete_me)44   CompositeVariableType::CompositeVariableType(const VariableType *base_type, int copies,
45                                                bool delete_me)
46       : VariableType(composite_name(base_type->name(), copies),
47                      base_type->component_count() * copies, delete_me),
48         baseType(base_type), copies_(copies)
49   {
50   }
51 
CompositeVariableType(const std::string & my_name,int number_components,bool delete_me)52   CompositeVariableType::CompositeVariableType(const std::string &my_name, int number_components,
53                                                bool delete_me)
54       : VariableType(my_name, number_components, delete_me), baseType(nullptr), copies_(0)
55   {
56   }
57 
label(int which,const char suffix_sep)58   std::string CompositeVariableType::label(int which, const char suffix_sep) const
59   {
60     static char tmp_sep[2];
61 
62     // NOTE: 'which' is 1-based
63     assert(which > 0 && which <= component_count());
64 
65     int base_comp      = baseType->component_count();
66     int which_instance = (which - 1) / base_comp;
67     int which_base     = (which - 1) % base_comp;
68 
69     std::string my_label = baseType->label(which_base + 1, suffix_sep);
70     if (suffix_sep != 0 && base_comp > 1) {
71       tmp_sep[0] = suffix_sep;
72       my_label += tmp_sep;
73     }
74     my_label += VariableType::numeric_label(which_instance + 1, copies_, name());
75     return my_label;
76   }
77 
GetBaseType()78   const VariableType *CompositeVariableType::GetBaseType() const { return baseType; }
79 
GetNumCopies()80   int CompositeVariableType::GetNumCopies() const { return copies_; }
81 
82 } // namespace Ioss
83