mageec  0.1.0
MAchine Guided Energy Efficient Compilation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
features.cpp
Go to the documentation of this file.
1 /* MAGEEC Feature Implementation
2  Copyright (C) 2013, 2014 Embecosm Limited and University of Bristol
3 
4  This file is part of MAGEEC.
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 
20 #include "mageec/mageec-features.h"
21 
22 using namespace mageec;
23 
24 static std::string pad_string(std::string input, unsigned long length, bool left)
25 {
26  if (input.length() == length)
27  return input;
28  std::string pad(length-input.length(), ' ');
29  if (left)
30  return pad + input;
31  return input + pad;
32 }
33 
34 void mageec_feature::dump_vector(std::vector<mageec_feature*> features,
35  std::ostream &OS, bool json)
36 {
37  if (features.size() == 0)
38  return;
39 
40  if (json)
41  {
42  OS << "[";
43  for (unsigned long i = 0, size = features.size(); i < size; i++)
44  {
45  OS << "{\"name\":\"" << features[i]->name() << "\",";
46  OS << "\"desc\":\"" << features[i]->desc() << "\",";
47  OS << "\"value\":" << features[i]->get_feature() << "}";
48  if (i+1 != size)
49  OS << ',';
50  }
51  OS << "]\n";
52  } else {
53  unsigned long maxname = 0;
54  unsigned long maxdesc = 0;
55  // Calculate the length of the longest name and description
56  for (unsigned long i = 0, size = features.size(); i < size; i++)
57  {
58  if (maxname < features[i]->name().length())
59  maxname = features[i]->name().length();
60  if (maxdesc < features[i]->desc().length())
61  maxdesc = features[i]->desc().length();
62  }
63  for (unsigned long i = 0, size = features.size(); i < size; i++)
64  {
65  OS << " (";
66  OS << pad_string(features[i]->name(), maxname, true);
67  OS << ") ";
68  OS << pad_string(features[i]->desc(), maxdesc, false);
69  OS << ": ";
70  OS << features[i]->get_feature();
71  OS << '\n';
72  }
73  }
74 }
75 
76 // Define default destructor to avoid weak vtables
78 {
79 }
80 
81 basic_feature::basic_feature(std::string feat_name)
82 {
83  feature_name = feat_name;
84 }
85 
86 basic_feature::basic_feature(std::string feat_name, int value)
87 {
88  feature_name = feat_name;
89  feature_desc = "";
90  feature_value = value;
91 }
92 
93 basic_feature::basic_feature(std::string feat_name, std::string feat_desc,
94  int value)
95 {
96  feature_name = feat_name;
97  feature_desc = feat_desc;
98  feature_value = value;
99 }
100 
101 std::string basic_feature::name()
102 {
103  return feature_name;
104 }
105 
106 std::string basic_feature::desc()
107 {
108  return feature_desc;
109 }
110 
112 {
113  return feature_value;
114 }