1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkInformationStringVectorKey.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkInformationStringVectorKey.h"
16 
17 #include "vtkInformation.h" // For vtkErrorWithObjectMacro
18 #include "vtkStdString.h"
19 
20 #include <algorithm>
21 #include <vector>
22 
23 
24 //----------------------------------------------------------------------------
25 vtkInformationStringVectorKey
vtkInformationStringVectorKey(const char * name,const char * location,int length)26 ::vtkInformationStringVectorKey(const char* name, const char* location,
27                                  int length):
28   vtkInformationKey(name, location), RequiredLength(length)
29 {
30   vtkCommonInformationKeyManager::Register(this);
31 }
32 
33 //----------------------------------------------------------------------------
34 vtkInformationStringVectorKey::~vtkInformationStringVectorKey() = default;
35 
36 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)37 void vtkInformationStringVectorKey::PrintSelf(ostream& os, vtkIndent indent)
38 {
39   this->Superclass::PrintSelf(os, indent);
40 }
41 
42 //----------------------------------------------------------------------------
43 class vtkInformationStringVectorValue: public vtkObjectBase
44 {
45 public:
46   vtkBaseTypeMacro(vtkInformationStringVectorValue, vtkObjectBase);
47   std::vector<std::string> Value;
48 };
49 
50 //----------------------------------------------------------------------------
Append(vtkInformation * info,const char * value)51 void vtkInformationStringVectorKey::Append(vtkInformation* info, const char* value)
52 {
53   vtkInformationStringVectorValue* v =
54     static_cast<vtkInformationStringVectorValue *>
55     (this->GetAsObjectBase(info));
56   if(v)
57   {
58     v->Value.push_back(value);
59   }
60   else
61   {
62     this->Set(info, value, 0);
63   }
64 }
65 
66 //----------------------------------------------------------------------------
Set(vtkInformation * info,const char * value,int index)67 void vtkInformationStringVectorKey::Set(vtkInformation* info, const char* value,
68                                         int index)
69 {
70   vtkInformationStringVectorValue* oldv =
71     static_cast<vtkInformationStringVectorValue *>
72     (this->GetAsObjectBase(info));
73   if(oldv)
74   {
75     if (   (static_cast<int>(oldv->Value.size()) <= index)
76         || (oldv->Value[index] != value))
77     {
78       while(static_cast<int>(oldv->Value.size()) <= index)
79       {
80         oldv->Value.push_back("");
81       }
82       oldv->Value[index] = value;
83       // Since this sets a value without call SetAsObjectBase(),
84       // the info has to be modified here (instead of
85       // vtkInformation::SetAsObjectBase()
86       info->Modified(this);
87     }
88   }
89   else
90   {
91     vtkInformationStringVectorValue* v =
92       new vtkInformationStringVectorValue;
93     v->InitializeObjectBase();
94     while(static_cast<int>(v->Value.size()) <= index)
95     {
96       v->Value.push_back("");
97     }
98     v->Value[index] = value;
99     this->SetAsObjectBase(info, v);
100     v->Delete();
101   }
102 }
103 
104 //----------------------------------------------------------------------------
Append(vtkInformation * info,const std::string & value)105 void vtkInformationStringVectorKey::Append(vtkInformation *info,
106                                            const std::string &value)
107 {
108   this->Append(info, value.c_str());
109 }
110 
111 //----------------------------------------------------------------------------
Set(vtkInformation * info,const std::string & value,int idx)112 void vtkInformationStringVectorKey::Set(vtkInformation *info,
113                                         const std::string &value, int idx)
114 {
115   this->Set(info, value.c_str(), idx);
116 }
117 
118 //----------------------------------------------------------------------------
Get(vtkInformation * info,int idx)119 const char* vtkInformationStringVectorKey::Get(vtkInformation* info, int idx)
120 {
121   if (idx < 0 || idx >= this->Length(info))
122   {
123     return nullptr;
124   }
125   vtkInformationStringVectorValue* v =
126     static_cast<vtkInformationStringVectorValue *>
127     (this->GetAsObjectBase(info));
128   return v->Value[idx].c_str();
129 }
130 
131 //----------------------------------------------------------------------------
Length(vtkInformation * info)132 int vtkInformationStringVectorKey::Length(vtkInformation* info)
133 {
134   vtkInformationStringVectorValue* v =
135     static_cast<vtkInformationStringVectorValue *>
136     (this->GetAsObjectBase(info));
137   return v?static_cast<int>(v->Value.size()):0;
138 }
139 
140 //----------------------------------------------------------------------------
ShallowCopy(vtkInformation * from,vtkInformation * to)141 void vtkInformationStringVectorKey::ShallowCopy(vtkInformation* from,
142                                                 vtkInformation* to)
143 {
144   int length = this->Length(from);
145   for(int i = 0; i < length; ++i)
146   {
147     this->Set(to, this->Get(from, i), i);
148   }
149 }
150 
151 //----------------------------------------------------------------------------
Print(ostream & os,vtkInformation * info)152 void vtkInformationStringVectorKey::Print(ostream& os, vtkInformation* info)
153 {
154   // Print the value.
155   if(this->Has(info))
156   {
157     int length = this->Length(info);
158     const char* sep = "";
159     for(int i=0; i < length; ++i)
160     {
161       os << sep << this->Get(info, i);
162       sep = " ";
163     }
164   }
165 }
166