Skip to content

Commit 41e57d4

Browse files
committed
1. refactor class Input_Value
1 parent 66cb34b commit 41e57d4

11 files changed

+112
-39
lines changed

source/Makefile.Objects

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ read_cell_pseudopots.o\
247247
read_dm.o\
248248
read_txt_tools.o\
249249
read_txt_stru.o\
250-
read_txt_input.o\
250+
read_txt_input_value.o\
251+
read_txt_input_item.o\
251252
read_txt_input_list.o\
252253
read_txt_input_process.o\
253254
read_txt_input-general.o\

source/src_io/read_txt_input-general.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ namespace Read_Txt_Input
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].s, {"1","2","4"}))
21+
if(!Read_Txt_Tools::in_set(self.value[0].gets(), {"1","2","4"}))
2222
throw std::invalid_argument("nspin must be 1,2,4");
23-
self.value[0].i = std::stoi(self.value[0].s);
2423
};
2524
item.convert = [](const Input_Item &self)
2625
{
27-
GlobalV::NSPIN = self.value[0].i;
26+
GlobalV::NSPIN = self.value[0].geti();
2827
};
2928
this->add_item(item);
3029
}
@@ -36,13 +35,12 @@ namespace Read_Txt_Input
3635
item.check_transform = [](Input_Item &self)
3736
{
3837
Input_List::check_value_size(self, 1);
39-
self.value[0].i = std::stoi(self.value[0].s);
40-
if(self.value[0].i<=0)
38+
if(self.value[0].geti()<=0)
4139
throw std::invalid_argument("ntype must > 0");
4240
};
4341
item.convert = [](const Input_Item &self)
4442
{
45-
GlobalC::ucell.ntype = self.value[0].i;
43+
GlobalC::ucell.ntype = self.value[0].geti();
4644
};
4745
this->add_item(item);
4846
}

source/src_io/read_txt_input-pw.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@ namespace Read_Txt_Input
1717
item.check_transform = [](Input_Item &self)
1818
{
1919
Input_List::check_value_size(self, 1, 2);
20-
if(self.value[1].s=="eV")
21-
self.value[0].d = std::stod(self.value[0].s);
22-
else if(self.value[1].s=="Ry")
23-
{
24-
self.value[0].d = std::stod(self.value[0].s) / ModuleBase::Ry_to_eV;
25-
self.value[0].s = std::to_string(self.value[0].d);
26-
}
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 );
2723
else
28-
throw std::invalid_argument(self.value[1].s);
29-
self.value[1].s = "eV";
30-
if(self.value[0].d<=0)
24+
throw std::invalid_argument(self.value[1].gets());
25+
self.value[1].sets("eV");
26+
if(self.value[0].getd()<=0)
3127
throw std::invalid_argument("ecutwfc must > 0");
3228
};
3329
item.convert = [](const Input_Item &self)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include "read_txt_input.h"
1+
#include "read_txt_input_item.h"
22

33
namespace Read_Txt_Input
44
{
55
void Input_Item::default1(const std::vector<std::string> &value_in)
66
{
77
this->value.resize(value_in.size());
88
for(size_t i=0; i<value_in.size(); ++i)
9-
this->value[i].s = value_in[i];
9+
this->value[i].sets(value_in[i]);
1010
}
1111
}

source/src_io/read_txt_input.h renamed to source/src_io/read_txt_input_item.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#ifndef READ_TXT_INPUT_H
2-
#define READ_TXT_INPUT_H
1+
#ifndef READ_TXT_INPUT_ITEM_H
2+
#define READ_TXT_INPUT_ITEM_H
3+
4+
#include "read_txt_input_value.h"
35

46
#include <vector>
57
#include <map>
@@ -12,14 +14,6 @@
1214

1315
namespace Read_Txt_Input
1416
{
15-
struct Input_Value
16-
{
17-
bool b;
18-
int i;
19-
double d;
20-
std::string s;
21-
};
22-
2317
class Input_Item
2418
{
2519
public:

source/src_io/read_txt_input_list.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef READ_TXT_INIPUT_LIST_H
22
#define READ_TXT_INIPUT_LIST_H
33

4-
#include "read_txt_input.h"
4+
#include "read_txt_input_item.h"
55

66
#include <vector>
77
#include <map>

source/src_io/read_txt_input_process.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace Read_Txt_Input
3838
if(input_read.second.size() > ptr->second.value.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].s = input_read.second[i];
41+
ptr->second.value[i].sets(input_read.second[i]);
4242
ptr->second.value_read_size = input_read.second.size();
4343
}
4444
}
@@ -62,7 +62,7 @@ namespace Read_Txt_Input
6262
{
6363
ofs<<label<<"\t";
6464
for(const Input_Value &value : input.list.at(label).value)
65-
ofs<<value.s<<" ";
65+
ofs<<value.gets()<<" ";
6666
ofs<<"\t# "<<input.list.at(label).annotation<<std::endl;
6767
}
6868
}

source/src_io/read_txt_input_process.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef READ_TXT_INIPUT_PROCESS_H
22
#define READ_TXT_INIPUT_PROCESS_H
33

4-
#include "read_txt_input.h"
54
#include "read_txt_input_list.h"
65

76
namespace Read_Txt_Input
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "read_txt_input_value.h"
2+
3+
#include <stdexcept>
4+
5+
namespace Read_Txt_Input
6+
{
7+
void Input_Value::setb(const bool &b_in)
8+
{
9+
this->s = std::to_string(b_in);
10+
this->d = static_cast<double>(b_in);
11+
this->i = static_cast<int>(b_in);
12+
this->b = b_in;
13+
}
14+
15+
void Input_Value::seti(const int &i_in)
16+
{
17+
this->s = std::to_string(i_in);
18+
this->d = static_cast<double>(i_in);
19+
this->i = i_in;
20+
this->b = static_cast<bool>(i_in);
21+
}
22+
23+
void Input_Value::setd(const double &d_in)
24+
{
25+
this->s = std::to_string(d_in);
26+
this->d = d_in;
27+
this->i = static_cast<int>(d_in);
28+
this->b = static_cast<bool>(d_in);
29+
}
30+
31+
void Input_Value::sets(const std::string &s_in)
32+
{
33+
try
34+
{
35+
this->s = s_in;
36+
this->d = std::stod(s_in);
37+
this->i = std::stoi(s_in);
38+
this->b = static_cast<bool>(std::stoi(s_in));
39+
}
40+
catch(const std::invalid_argument &e)
41+
{
42+
this->s = s_in;
43+
}
44+
}
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef READ_TXT_INPUT_VALUE_H
2+
#define READ_TXT_INPUT_VALUE_H
3+
4+
#include <string>
5+
6+
#ifdef USE_CEREAL_SERIALIZATION
7+
#include <cereal/access.hpp>
8+
#endif
9+
10+
namespace Read_Txt_Input
11+
{
12+
class Input_Value
13+
{
14+
public:
15+
const bool & getb()const{ return b; }
16+
const int & geti()const{ return i; }
17+
const double & getd()const{ return d; }
18+
const std::string & gets()const{ return s; }
19+
20+
void setb(const bool &b_in);
21+
void seti(const int &i_in);
22+
void setd(const double &d_in);
23+
void sets(const std::string &s_in);
24+
25+
private:
26+
bool b;
27+
int i;
28+
double d;
29+
std::string s;
30+
31+
#ifdef USE_CEREAL_SERIALIZATION
32+
private:
33+
friend class cereal::access;
34+
template <class Archive> void serialize( Archive & ar );
35+
#endif
36+
};
37+
}
38+
39+
#endif

0 commit comments

Comments
 (0)