是否有更多通用的方法来编写Finalizer
类而不是这个?
#include <functional>
#include <iostream>
template <typename T>
class Finalizer
{
public:
Finalizer(const std::function<T>& f) : _f(f) {}
~Finalizer()
{
_f();
}
private:
std::function<T> _f;
};
int main()
{
Finalizer<void()> finalizer([]() { std::cout << "str" << std::endl; });
}
我想摆脱手工类模板参数规范,以便能够编写如下代码:
Finalizer finalizer([]() { std::cout << "str" << std::endl; });
有可能吗?
答案 0 :(得分:5)
在C ++中,类型演绎仅适用于功能模板,不适用于类模板。
您需要 public class PageIndicatorActivity extends Activity {
Button previous, next;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter adapter = new ViewPagerAdapter(this);
final ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
previous = (Button) findViewById(R.id.previous);
next = (Button) findViewById(R.id.next);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
int pageno = myPager.getCurrentItem();
int total = adapter.getCount();
System.out.println("Current page" + pageno + "Total" + total);
previous.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myPager.setCurrentItem(myPager.getCurrentItem() - 1, true); // getItem(-1) for previous
}
});
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myPager.setCurrentItem(myPager.getCurrentItem() + 1, true); // getItem(+1) for next
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
enter code here
public class ViewPagerAdapter extends PagerAdapter {
Activity activity;
int size=4;
TextView question;
// Hashmap for ListView
String myjsonstring;
LinearLayout layout,layout1;
JSONObject jsonobject;
JSONArray jarray;
public ViewPagerAdapter(Activity act) {
// imageArray = imgArra;
activity = act;
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(activity.getAssets()
.open("questionBank.json"), "iso-8859-1"), 8);
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
myjsonstring = sb.toString();
Log.d("Result", myjsonstring);
try {
jsonobject = new JSONObject(myjsonstring);
jarray = (JSONArray) jsonobject.getJSONArray("question");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//size = jarray.length();
// try {
// JSONObject jsonobject = new JSONObject(myjsonstring);
// JSONArray jarray = (JSONArray) jsonobject.getJSONArray("question");
// for (int i = 0; i < jarray.length(); i++) {
// question = new TextView(activity);
// Log.d("Array Length", i + "");
// JSONObject jb = (JSONObject) jarray.get(i);
// String pno = jb.getString("pgNO");
// String qno = jb.getString("QuestionNo");
// String qtype = jb.getString("type");
// String qdept = jb.getString("QDept");
// String qkpi = jb.getString("QKPI");
// String qrc = jb.getString("QRC");
//
// String qdisplay = jb.getString("QDisplayName");
// String qanswer = jb.getString("Qchildlook");
// Log.d("Values", qno + "\n" + qtype + "\n" + qdept + "\n" + qkpi
// + "\n" + qrc + "\n" + qdisplay + "\n" + qanswer);
// question.setText(qno + "." + qdisplay);
// question.setTextSize(20);
//
// // tmp hashmap for single contact
// HashMap<String, String> contact = new HashMap<String, String>();
//
// // adding each child node to HashMap key => value
// contact.put(TAG_NO, pno);
// contact.put(TAG_ID, qno);
// contact.put(TAG_NAME, qtype);
// contact.put(TAG_EMAIL, qdisplay);
//
// // adding contact to contact list
// contactList.add(contact);
//
// }
// // size = jarray.length();
// } catch (JSONException ex) {
// Log.d("Exception", ex.getMessage());
//
// }
}
public int getCount() {
// return imageArray.length;
return size;
}
public Object instantiateItem(View collection, int position) {
layout1 = new LinearLayout(activity);
layout = new LinearLayout(activity);
layout.setPadding(40, 30, 0, 30);
layout.setOrientation(LinearLayout.VERTICAL);
layout1.setOrientation(LinearLayout.HORIZONTAL);
try {
for(int i=0;i<jarray.length();i++)
{
layout.setPadding(30, 70, 20, 70);
TextView question=new TextView(activity);
Log.d("Array Length",i+"");
JSONObject jb=(JSONObject) jarray.get(i);
String pno=jb.getString("pgNO");
int pagNo=Integer.parseInt(pno);
String qno=jb.getString("QuestionNo");
String qtype=jb.getString("type");
String qdept=jb.getString("QDept");
String qkpi=jb.getString("QKPI");
String qrc=jb.getString("QRC");
String qdisplay=jb.getString("QDisplayName");
String qanswer=jb.getString("Qchildlook");
Log.d("Values",qno+"\n"+qtype+"\n"+qdept+"\n"+qkpi+"\n"+qrc+"\n"+qdisplay+"\n"+qanswer);
if(qanswer.equals("answer"))
{
if(qtype.equals("checkbox") )
{
question.setText(qno+"."+qdisplay);
question.setTextSize(20);
layout.addView(question);
JSONArray jarray1=jb.getJSONArray("Answerword");
for(int j=0;j<jarray1.length();j++)
{
layout.setPadding(20, 40, 0, 20);
JSONObject jb1=(JSONObject) jarray1.get(j);
CheckBox checkBox=new CheckBox(activity);
checkBox.setText(jb1.getString("option"));
checkBox.setMaxHeight(5);
checkBox.setTextColor(Color.BLACK);
layout.addView(checkBox);
Log.d("Values",jb1.getString("option")+""+jb1.getString("id"));
}
}
else
if(qtype.equals("textArea") )
{
question.setText(qno+"."+qdisplay);
question.setTextSize(20);
layout.addView(question);
EditText editText=new EditText(activity);
editText.setHint("Enter your Comments");
editText.setMinHeight(35);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(15);
editText.setFilters(filterArray);
editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(30) });
editText.setHintTextColor(Color.MAGENTA);
layout.addView(editText);
}
else
if(qtype.equals("radio") )
{
question.setText(qno+"."+qdisplay);
question.setTextSize(20);
layout.addView(question);
LinearLayout hlLayout=new LinearLayout(activity);
hlLayout.setOrientation(LinearLayout.HORIZONTAL);
hlLayout.setPadding(35, 10, 0, 0);
JSONObject subObj = jb.getJSONObject("QOptionMatrix");
JSONObject subObj1=subObj.getJSONObject("QOptionHeader");
String header1=subObj1.getString("HdrOption1");
String header2=subObj1.getString("HdrOption2");
String header3=subObj1.getString("HdrOption3");
String header4=subObj1.getString("HdrOption4");
TextView text=new TextView(activity);
text.setText(header1+"\t\t"+header2+"\t\t"+header3+"\t\t"+header4);
//hlLayout.setPadding(60,10, 10, 0);
hlLayout.addView(text);
// JSONObject subObj2=subObj.getJSONObject("QOptions");
// String option1=subObj2.getString("Option1");
// String option2=subObj2.getString("Option2");
// String option3=subObj2.getString("Option3");
// String option4=subObj2.getString("Option4");
System.out.println(subObj);
System.out.println(header1+"\t"+header2+"\t"+header3+"\t"+header4);
JSONArray jsonArray1=subObj.getJSONArray("QOptions1");
JSONArray jsonArray2=subObj.getJSONArray("QOptions2");
JSONArray jsonArray3=subObj.getJSONArray("QOptions3");
layout.addView(hlLayout);
radioMethodH(jsonArray1);
radioMethodH(jsonArray2);
radioMethodH(jsonArray3);
}
JSONObject qvalidation = jb.getJSONObject("QValidation");
String nameValue = qvalidation.getString("QMessages");
Log.d("Message",nameValue);
}
else
{
{
question.setText(qno+"."+qdisplay);
question.setTextSize(20);
layout.addView(question);
JSONObject child = jb.getJSONObject("child");
String questionno = child.getString("QuestionNo");
String quesdisplay=child.getString("QDisplayName");
Log.d("Child",questionno+"\n"+quesdisplay);
TextView childtext=new TextView(activity);
childtext.setText("\t\t"+questionno+"."+quesdisplay);
layout.addView(childtext);
JSONObject firstchild=jb.getJSONObject("firstchild");
String firstchildq=firstchild.getString("QuestionNo");
String firstchildd=firstchild.getString("QDisplayName");
String firstchildtype=firstchild.getString("firstchildtype");
String firstorientation=firstchild.getString("OptionDisplayType");
TextView firstchid=new TextView(activity);
firstchid.setText("\t\t\t"+firstchildq+"."+firstchildd);
System.out.println(firstchildtype+"\t"+firstorientation);
layout.addView(firstchid);
if(firstchildtype.equals("radio"))
{
if(firstorientation.equals("H"))
{
JSONArray jarray1=jb.getJSONArray("firstchildAnswerword");
radioMethodH(jarray1);
}
else
{
JSONArray jarray1=jb.getJSONArray("firstchildAnswerword");
radioMethodV(jarray1);
}
}
JSONObject secondchild=jb.getJSONObject("secondchild");
String secondchildq=secondchild.getString("QuestionNo");
String secondchildd=secondchild.getString("QDisplayName");
String secondchildtype=secondchild.getString("secondchildtype");
String secondorientation=secondchild.getString("OptionDisplayType");
TextView secondchid=new TextView(activity);
secondchid.setText("\t\t\t"+secondchildq+"."+secondchildd);
System.out.println(secondchildtype+"\t"+secondorientation);
layout.addView(secondchid);
if(secondchildtype.equals("radio"))
{
if(secondorientation.equals("H"))
{
JSONArray jarray2=jb.getJSONArray("secondchildAnswerword");
radioMethodH(jarray2);
}
else
{
JSONArray jarray2=jb.getJSONArray("secondchildAnswerword");
radioMethodV(jarray2);
}
}
}
}
}
} catch (JSONException ex) {
Log.d("Exception", ex.getMessage());
}
// for (HashMap<String, String> conString : contactList) {
// Log.d("hsjahfld", conString.get(TAG_NAME)+"");
// Log.d("page No", conString.get(TAG_NO)+"");
// Log.d("Position", position + "");
//
// int pageno =Integer.parseInt(conString.get(TAG_NO));
//
// Log.d("page No1", pageno + "");
//
// if (conString.get(TAG_NAME).equals("textArea")
// && pageno - 1 == position) {
// TextView text = new TextView(activity);
// EditText editText = new EditText(activity);
// text.setText(conString.get(TAG_EMAIL)+"");
// layout.addView(text);
// editText.setHint("Enetr your comments");
// layout.addView(editText);
//
// } else if (conString.get(TAG_NAME).equals("checkbox")
// && pageno - 1 == position) {
// TextView text = new TextView(activity);
// text.setText(conString.get(TAG_EMAIL)+"");
// layout.addView(text);
//
// CheckBox checkBox = new CheckBox(activity);
// checkBox.setMaxHeight(5);
// layout.addView(checkBox);
// }
//
// else if (conString.get(TAG_NAME).equals("radio")
// && pageno - 1 == position) {
// TextView text = new TextView(activity);
// text.setText(conString.get(TAG_EMAIL)+"");
// layout.addView(text);
//
// RadioGroup radioGroup = new RadioGroup(activity);
// radioGroup.setOrientation(RadioGroup.HORIZONTAL);
// for (int i = 0; i < 3; i++) {
// RadioButton radioButton = new RadioButton(activity);
// radioGroup.addView(radioButton);
// }
// layout.addView(radioGroup);
// }
//
// else if (conString.get(TAG_NAME).equals("seekbar")
// && pageno - 1 == position) {
// TextView text = new TextView(activity);
// text.setText(conString.get(TAG_EMAIL)+"");
// layout.addView(text);
//
// LinearLayout liLayout = new LinearLayout(activity);
// liLayout.setOrientation(LinearLayout.HORIZONTAL);
//
// for (int i = 0; i <= 10; i++) {
//
// Button button = new Button(activity);
// if (i == 0 || i == 1 || i == 2 || i == 3 || i == 4
// || i == 5 || i == 6) {
// button.setMaxHeight(35);
// button.setMinWidth(2);
// button.setText(i + "");
// button.setTextColor(Color.BLACK);
// button.setBackgroundColor(Color.RED);
// button.setMinimumWidth(2);
// liLayout.addView(button);
// } else if (i == 7 || i == 8) {
// button.setMaxHeight(35);
// button.setMinWidth(2);
// button.setText(i + "");
// button.setBackgroundColor(Color.YELLOW);
// button.setMinimumWidth(2);
// button.setTextColor(Color.BLACK);
// liLayout.addView(button);
// } else if (i == 9 || i == 10) {
// button.setMaxHeight(35);
// button.setMinWidth(2);
// button.setText(i + "");
// button.setTextColor(Color.BLACK);
// button.setBackgroundColor(Color.GREEN);
// button.setMinimumWidth(2);
// liLayout.addView(button);
// }
// }
// layout.addView(liLayout);
//
// }
// }
((ViewPager) collection).addView(layout);
return layout;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
public void radioMethodH(JSONArray aryy)
{
RadioGroup radioGroup=new RadioGroup(activity);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
for(int i =0;i<aryy.length();i++)
{
try {
JSONObject jb=(JSONObject) aryy.get(i);
radioGroup.setPadding(25,10, 10, 0);
RadioButton radioButton=new RadioButton(activity);
radioButton.setText(jb.getString("option"));
radioButton.setTextColor(Color.BLACK);
if(jb.getString("option").equals(""))
{
radioButton.setVisibility(RadioButton.INVISIBLE);
}
else
{
radioGroup.addView(radioButton);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
layout.addView(radioGroup);
}
public void radioMethodV(JSONArray aryy)
{
RadioGroup radioGroup=new RadioGroup(activity);
radioGroup.setOrientation(RadioGroup.VERTICAL);
for(int i =0;i<aryy.length();i++)
{
try {
JSONObject jb=(JSONObject) aryy.get(i);
radioGroup.setPadding(25,10, 10, 0);
RadioButton radioButton=new RadioButton(activity);
radioButton.setText(jb.getString("option"));
radioButton.setTextColor(Color.BLACK);
if(jb.getString("option").equals(""))
{
radioButton.setVisibility(RadioButton.INVISIBLE);
}
else
{
radioGroup.addView(radioButton);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
layout.addView(radioGroup);
}
}
函数来执行模板参数推断。
此外,您根本不必使用make_finalizer
,无需支付运行时费用,除非您确实希望对其进行类型删除。
std::function
并将其与auto:
一起使用template <typename F>
class Finalizer
{
public:
Finalizer(const F & c) : f_(c) {}
Finalizer(F && c) : f_(std::move(c)) {}
Finalizer(const Finalizer &) = delete;
Finalizer(Finalizer && other) :
valid_(other.valid),
f_(std::move(other.f_))
{
other.valid_ = false;
}
Finalizer& operator=(const Finalizer &) = delete;
Finalizer& operator=(Finalizer && other)
{
Finalizer tmp(std::move(other));
swap(tmp);
return *this;
}
~Finalizer()
{
if ( valid_ )
f_();
}
void swap(Finalizer & other) noexcept
{
using std::swap;
swap(other.valid_, valid_);
swap(other.f_, f_);
}
private:
bool valid_ = true;
F f_;
};
template<class F>
Finalizer< std::remove_reference_t<F> > at_scope_exit(F && x)
{
return Finalizer< std::remove_reference_t<F> >(std::forward<F>(x));
}