presage  0.9.2~beta
variable.cpp
Go to the documentation of this file.
1 
2 /******************************************************
3  * Presage, an extensible predictive text entry system
4  * ---------------------------------------------------
5  *
6  * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License along
19  with this program; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  **********(*)*/
23 
24 
25 #include "core/variable.h"
26 
27 #include <iostream>
28 
29 Variable::Variable(const char* name)
30 {
31  m_name = name;
33 }
34 
35 Variable::Variable(const std::string& name)
36 {
37  m_name = name;
39 }
40 
41 Variable::Variable(const std::vector<std::string>& name)
42 {
43  m_name = vector_to_string (name);
44  m_name_vector = name;
45 }
46 
48 {
49  // nothing to do
50 }
51 
52 std::string Variable::get_name () const
53 {
54  return m_name;
55 }
56 
57 std::vector<std::string> Variable::get_name_vector () const
58 {
59  return m_name_vector;
60 }
61 
62 std::string Variable::get_value () const
63 {
64  return m_value;
65 }
66 
67 void Variable::set_value (std::string value)
68 {
69  m_value = value;
70 
71  notify (); // notify all observers
72 }
73 
82 std::vector<std::string> Variable::string_to_vector(const std::string& str)
83 {
84  const char SEPARATOR = '.';
85 
86  std::vector<std::string> result;
87 
88  size_t length = str.size();
89  size_t i = 0;
90  std::string acc;
91  while (i < length) {
92  if (str[i] == SEPARATOR) {
93  result.push_back(acc);
94  acc.clear();
95  } else {
96  acc += str[i];
97  }
98  i++;
99  }
100  if (!acc.empty()) {
101  result.push_back(acc);
102  }
103 
104  return result;
105 }
106 
107 std::string Variable::vector_to_string(const std::vector<std::string>& variable)
108 {
109  std::string result;
110  for (size_t i = 0; i < variable.size(); i++) {
111  result += variable[i];
112  if (i < variable.size() - 1) {
113  result += '.';
114  }
115  }
116 
117  return result;
118 }
static std::vector< std::string > string_to_vector(const std::string &str)
Definition: variable.cpp:82
std::string m_name
Definition: variable.h:55
Variable(const char *variable)
Definition: variable.cpp:29
~Variable()
Definition: variable.cpp:47
virtual void notify()
Definition: observable.cpp:44
std::vector< std::string > get_name_vector() const
Definition: variable.cpp:57
static std::string vector_to_string(const std::vector< std::string > &var)
Definition: variable.cpp:107
void set_value(std::string value)
Definition: variable.cpp:67
std::vector< std::string > m_name_vector
Definition: variable.h:56
std::string m_value
Definition: variable.h:57
std::string get_value() const
Definition: variable.cpp:62
std::string get_name() const
Definition: variable.cpp:52