Skip to content

Commit faf8bb5

Browse files
committed
1. update Input_Item::default_1()
1 parent 41e57d4 commit faf8bb5

File tree

8 files changed

+85
-31
lines changed

8 files changed

+85
-31
lines changed

source/Makefile.Objects

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ read_dm.o\
248248
read_txt_tools.o\
249249
read_txt_stru.o\
250250
read_txt_input_value.o\
251-
read_txt_input_item.o\
252251
read_txt_input_list.o\
253252
read_txt_input_process.o\
254253
read_txt_input-general.o\

source/src_io/read_txt_input-general.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,34 @@ namespace Read_Txt_Input
1313
{
1414
{
1515
Input_Item item("nspin");
16-
item.default1({"1"});
16+
item.default_1(1);
1717
item.annotation = "1: single spin; 2: up and down spin; 4: noncollinear spin";
1818
item.check_transform = [](Input_Item &self)
1919
{
2020
Input_List::check_value_size(self, 1);
21-
if(!Read_Txt_Tools::in_set(self.value[0].gets(), {"1","2","4"}))
21+
if(!Read_Txt_Tools::in_set(self.values[0].gets(), {"1","2","4"}))
2222
throw std::invalid_argument("nspin must be 1,2,4");
2323
};
2424
item.convert = [](const Input_Item &self)
2525
{
26-
GlobalV::NSPIN = self.value[0].geti();
26+
GlobalV::NSPIN = self.values[0].geti();
2727
};
2828
this->add_item(item);
2929
}
3030

3131
{
3232
Input_Item item("ntype");
33-
item.default1({"0"});
33+
item.default_1(0);
3434
item.annotation = "atom species number";
3535
item.check_transform = [](Input_Item &self)
3636
{
3737
Input_List::check_value_size(self, 1);
38-
if(self.value[0].geti()<=0)
38+
if(self.values[0].geti()<=0)
3939
throw std::invalid_argument("ntype must > 0");
4040
};
4141
item.convert = [](const Input_Item &self)
4242
{
43-
GlobalC::ucell.ntype = self.value[0].geti();
43+
GlobalC::ucell.ntype = self.values[0].geti();
4444
};
4545
this->add_item(item);
4646
}

source/src_io/read_txt_input-pw.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ namespace Read_Txt_Input
1212
{
1313
{
1414
Input_Item item("ecutwfc");
15-
item.default1({"100","eV"});
15+
item.default_1(100,"eV");
1616
item.annotation = "energy cutoff for wave functions";
1717
item.check_transform = [](Input_Item &self)
1818
{
1919
Input_List::check_value_size(self, 1, 2);
20-
if(self.value[1].gets()=="eV"){}
21-
else if(self.value[1].gets()=="Ry")
22-
self.value[0].setd( self.value[0].getd() / ModuleBase::Ry_to_eV );
20+
if(self.values[1].gets()=="eV"){}
21+
else if(self.values[1].gets()=="Ry")
22+
self.values[0].setd( self.values[0].getd() / ModuleBase::Ry_to_eV );
2323
else
24-
throw std::invalid_argument(self.value[1].gets());
25-
self.value[1].sets("eV");
26-
if(self.value[0].getd()<=0)
24+
throw std::invalid_argument(self.values[1].gets());
25+
self.values[1].sets("eV");
26+
if(self.values[0].getd()<=0)
2727
throw std::invalid_argument("ecutwfc must > 0");
2828
};
2929
item.convert = [](const Input_Item &self)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef READ_TXT_INPUT_ITEM_TEMPLATE_H
2+
#define READ_TXT_INPUT_ITEM_TEMPLATE_H
3+
4+
#include "read_txt_input_item.h"
5+
6+
namespace Read_Txt_Input
7+
{
8+
template<typename T>
9+
void Input_Item::default_1(const T &value)
10+
{
11+
set_value(value);
12+
}
13+
14+
template <typename T_head, typename... T_tail>
15+
void Input_Item::default_1( const T_head &value_head, const T_tail... values_tail)
16+
{
17+
set_value(value_head);
18+
default_1(values_tail...);
19+
}
20+
21+
template<>
22+
void Input_Item::set_value(const bool &b)
23+
{
24+
this->values.push_back({});
25+
this->values.back().setb(b);
26+
}
27+
28+
template<>
29+
void Input_Item::set_value(const int &i)
30+
{
31+
this->values.push_back({});
32+
this->values.back().seti(i);
33+
}
34+
35+
template<>
36+
void Input_Item::set_value(const double &d)
37+
{
38+
this->values.push_back({});
39+
this->values.back().setd(d);
40+
}
41+
42+
template<>
43+
void Input_Item::set_value(const std::string &s)
44+
{
45+
this->values.push_back({});
46+
this->values.back().sets(s);
47+
}
48+
49+
template<>
50+
void Input_Item::set_value(const char*const &s)
51+
{
52+
this->values.push_back({});
53+
this->values.back().sets(std::string(s));
54+
}
55+
}
56+
57+
#endif

source/src_io/read_txt_input_item.cpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

source/src_io/read_txt_input_item.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ namespace Read_Txt_Input
1919
public:
2020
// call these functions
2121
Input_Item(const std::string &label_in) :label(label_in) {}
22-
void default1(const std::vector<std::string> &value_in);
22+
template<typename T>
23+
void default_1(const T &value);
24+
template <typename T_head, typename... T_tail>
25+
void default_1( const T_head &value_head, const T_tail... values_tail);
2326

2427
// set these variables and functions
2528
std::string annotation;
@@ -30,14 +33,18 @@ namespace Read_Txt_Input
3033
std::function<void(const Input_Item&)> convert
3134
= [](const Input_Item&item){};
3235

33-
std::vector<Input_Value> value;
36+
std::vector<Input_Value> values;
3437

3538
private:
3639
std::string label;
3740
int value_read_size = -1;
41+
3842
friend class Input_List;
3943
friend class Input_Process;
4044

45+
template<typename T>
46+
void set_value(const T &value);
47+
4148
#ifdef USE_CEREAL_SERIALIZATION
4249
public:
4350
Input_Item() = default;
@@ -48,4 +55,6 @@ namespace Read_Txt_Input
4855
};
4956
}
5057

58+
#include "read_txt_input_item-template.h"
59+
5160
#endif

source/src_io/read_txt_input_process.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ namespace Read_Txt_Input
3535
const auto ptr = input.list.find(input_read.first);
3636
if(ptr==input.list.end())
3737
throw std::out_of_range("input_read.first");
38-
if(input_read.second.size() > ptr->second.value.size())
38+
if(input_read.second.size() > ptr->second.values.size())
3939
throw std::out_of_range("size error");
4040
for(size_t i=0; i<input_read.second.size(); ++i)
41-
ptr->second.value[i].sets(input_read.second[i]);
41+
ptr->second.values[i].sets(input_read.second[i]);
4242
ptr->second.value_read_size = input_read.second.size();
4343
}
4444
}
@@ -61,7 +61,7 @@ namespace Read_Txt_Input
6161
for(const std::string &label : input.add_order)
6262
{
6363
ofs<<label<<"\t";
64-
for(const Input_Value &value : input.list.at(label).value)
64+
for(const Input_Value &value : input.list.at(label).values)
6565
ofs<<value.gets()<<" ";
6666
ofs<<"\t# "<<input.list.at(label).annotation<<std::endl;
6767
}

source/src_lcao/serialization_cereal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ template<class Archive> void Input_Value::serialize( Archive & ar )
5050
template<class Archive> void Input_Item::serialize( Archive & ar )
5151
{
5252
ar( this->annotation );
53-
ar( this->value );
53+
ar( this->values );
5454
ar( this->label );
5555
ar( this->value_read_size );
5656
}

0 commit comments

Comments
 (0)