1 #include <sstream>
2 #include "property.hh"
3 #include "operation.hh"
4
5
operator >>(std::istream & str,PF::PropertyBase & p)6 std::istream& operator >>(std::istream& str, PF::PropertyBase& p)
7 {
8 p.from_stream(str);
9 return str;
10 }
operator <<(std::ostream & str,PF::PropertyBase & p)11 std::ostream& operator <<(std::ostream& str, PF::PropertyBase& p)
12 {
13 p.to_stream(str);
14 return str;
15 }
16
17
PropertyBase(std::string n,OpParBase * par)18 PF::PropertyBase::PropertyBase(std::string n, OpParBase* par):
19 name(n), internal(false), passive(false), persistent(true), modified_flag(true)
20 {
21 par->add_property(this);
22 //std::cout<<std::endl<<std::endl<<std::endl<<"=========================="
23 // <<std::endl<<"Property \""<<n<<"\" initialized without value"<<std::endl
24 // <<"=========================="<<std::endl<<std::endl<<std::endl;
25 }
26
27
PropertyBase(std::string n,OpParBase * par,int val,std::string strval,std::string valname)28 PF::PropertyBase::PropertyBase(std::string n, OpParBase* par,
29 int val, std::string strval,
30 std::string valname):
31 name(n), internal(false), passive(false), persistent(true), modified_flag(true)
32 {
33 par->add_property(this);
34 add_enum_value( val, strval, valname );
35 enum_value.first = val;
36 enum_value.second.first = strval;
37 enum_value.second.second = valname;
38
39 default_enum_value = enum_value;
40 }
41
42
set_str(const std::string & val)43 void PF::PropertyBase::set_str(const std::string& val)
44 {
45 #ifndef NDEBUG
46 std::cout<<"PF::PropertyBase::set_str(): setting property \""<<name<<"\" to value \""<<val<<"\""<<std::endl;
47 #endif
48 std::istringstream str(val);
49 from_stream(str);
50 }
51
get_str()52 std::string PF::PropertyBase::get_str()
53 {
54 std::ostringstream str;
55 to_stream(str);
56 return str.str();
57 }
58
59
get_enum_value_str()60 std::string PF::PropertyBase::get_enum_value_str()
61 {
62 std::ostringstream str;
63 if( is_enum() && !enum_value.second.first.empty() ) {
64 str<<enum_value.first;
65 }
66 return str.str();
67 }
68
69
from_stream(std::istream & str)70 void PF::PropertyBase::from_stream(std::istream& str)
71 {
72 #ifndef NDEBUG
73 std::cout<<"PF::PropertyBase::from_stream(): is_enum()="<<is_enum()<<std::endl;
74 #endif
75 if( !is_enum() ) return;
76 std::string s;
77 str>>s;
78 std::map< int, std::pair<std::string,std::string> >::iterator iter;
79 for( iter = enum_values.begin(); iter != enum_values.end(); iter++ ) {
80 if( (*iter).second.first != s )
81 continue;
82 if(enum_value.first != (*iter).first)
83 modified();
84 enum_value = (*iter);
85 break;
86 }
87 }
88
89
to_stream(std::ostream & str)90 void PF::PropertyBase::to_stream(std::ostream& str)
91 {
92 if( !is_enum() ) return;
93 if( enum_value.second.first.empty() ) return;
94 str<<enum_value.second.first;
95 }
96
97
98
set_gobject(gpointer object)99 void PF::PropertyBase::set_gobject(gpointer object)
100 {
101 if( !is_enum() ) return;
102 if( enum_value.second.first.empty() ) return;
103 g_object_set( object, get_name().c_str(), enum_value.first, NULL );
104 }
105
106
107
108 template<>
set_gobject_property(gpointer object,const std::string name,const std::string & value)109 void PF::set_gobject_property<std::string>(gpointer object, const std::string name, const std::string& value)
110 {
111 g_object_set( object, name.c_str(), value.c_str(), NULL );
112 }
113
114
115
import(PF::PropertyBase * pin)116 bool PF::PropertyBase::import(PF::PropertyBase* pin)
117 {
118 #ifndef NDEBUG
119 //if( name == "out_profile_mode" )
120 std::cout<<"PropertyBase::import(): importing property \""<<name<<"\""<<std::endl;
121 #endif
122 if( !pin ) {
123 std::cout<<"PropertyBase::import(): pin = NULL"<<std::endl;
124 return false;
125 }
126
127 if( is_enum() ) {
128 std::pair< int, std::pair<std::string,std::string> > val = pin->get_enum_value();
129 std::map< int, std::pair<std::string,std::string> >::iterator mi =
130 enum_values.find( val.first );
131 if( mi == enum_values.end() ) {
132 std::cout<<"PropertyBase::import(): enum value "<<val.first<<" not found when importing property \""<<name<<"\""<<std::endl;
133 return false;
134 }
135 std::pair< int, std::pair<std::string,std::string> > val2 = *mi;
136 if( (val.second.first == val2.second.first) ) { //&&
137 //(val.second.second == val2.second.second) ) {
138 if(enum_value.first != val.first)
139 modified();
140 enum_value = val;
141 #ifndef NDEBUG
142 std::cout<<"PropertyBase::import(): property \""<<name<<"\" imported"<<std::endl;
143 #endif
144 return true;
145 } else {
146 std::cout<<"PropertyBase::import(): enum value mismatch when importing property \""<<name<<"\""<<std::endl;
147 std::cout<<" val ="<<val.first<<" "<<val.second.first<<" "<<val.second.second<<std::endl;
148 std::cout<<" val2="<<val2.first<<" "<<val2.second.first<<" "<<val2.second.second<<std::endl;
149 return false;
150 }
151 } else {
152 set_str( pin->get_str() );
153 #ifndef NDEBUG
154 std::cout<<"PropertyBase::import(): property \""<<name<<"\" imported"<<std::endl;
155 #endif
156 return true;
157 }
158 }
159