Empirical
DynamicString.h
Go to the documentation of this file.
1 
12 #ifndef EMP_DYNAMIC_STRING_H
13 #define EMP_DYNAMIC_STRING_H
14 
15 #include <functional>
16 #include <string>
17 
18 #include "../base/vector.h"
19 
20 namespace emp {
21 
24  class DynamicString {
25  public:
26  using value_t = std::function<std::string()>;
27  private:
28  emp::vector<value_t> fun_set; // Functions to update strings.
29 
30  public:
31  DynamicString() { ; }
32  DynamicString(const DynamicString &) = default;
33 
35  size_t GetSize() const { return fun_set.size(); }
36 
39  std::string operator[](size_t id) const { return fun_set[id](); }
40 
43  const value_t & GetFunction(size_t id) const { return fun_set[id]; }
44 
46  DynamicString & Clear() { fun_set.resize(0); return *this; }
47 
49  std::string str() {
50  std::stringstream ss;
51  for (auto & cur_fun : fun_set) ss << cur_fun();
52  return ss.str();
53  }
54 
56  DynamicString & Set(size_t id, const value_t & in_fun) {
57  fun_set[id] = in_fun;
58  return *this;
59  }
60 
62  DynamicString & Set(size_t id, const std::string & in_text) {
63  return Set( id, [in_text](){ return in_text; } );
64  }
65 
67  DynamicString & Append(const value_t & in_fun) {
68  fun_set.push_back(in_fun);
69  return *this;
70  }
71 
73  // (automatically create a function that just returns that string.)
74  DynamicString & Append(const std::string & in_text) {
75  return Append( [in_text](){ return in_text; } );
76  }
77 
79  template <typename IN_TYPE>
80  DynamicString & operator<<(IN_TYPE && _in) { return Append(_in); }
81 
82  };
83 
84 }
85 
86 namespace std {
88  std::ostream & operator<<( std::ostream & os, const emp::DynamicString & strings )
89  {
90  for (size_t i = 0; i < strings.GetSize(); ++i) {
91  os << strings[i];
92  }
93  return os;
94  }
95 }
96 
97 #endif
DynamicString & Set(size_t id, const value_t &in_fun)
Set the value of a specified component to the provided function.
Definition: DynamicString.h:56
DynamicString & Clear()
Remove all contents on this DynamicString.
Definition: DynamicString.h:46
DynamicString()
Definition: DynamicString.h:31
size_t GetSize() const
How many string components (funcations or continuous substrings) are in this DynamicString?
Definition: DynamicString.h:35
DynamicString & Append(const value_t &in_fun)
Add a new function to the end of the DynamicString.
Definition: DynamicString.h:67
std::string operator[](size_t id) const
Definition: DynamicString.h:39
Definition: BitVector.h:785
void push_back(PB_Ts &&...args)
Definition: vector.h:189
std::function< std::string()> value_t
Definition: DynamicString.h:26
const value_t & GetFunction(size_t id) const
Definition: DynamicString.h:43
size_t size() const
Definition: vector.h:151
DynamicString & Append(const std::string &in_text)
Add new std::string text to the end of the DynamicString.
Definition: DynamicString.h:74
void resize(size_t new_size)
Definition: vector.h:161
DynamicString & Set(size_t id, const std::string &in_text)
Set the value of a specified component to the provided std::string text.
Definition: DynamicString.h:62
If we are in emscripten, make sure to include the header.
Definition: array.h:37
DynamicString & operator<<(IN_TYPE &&_in)
Allow operator<< to append to the back of a DynamicString.
Definition: DynamicString.h:80
std::string str()
Convert to an std::string.
Definition: DynamicString.h:49
Definition: DynamicString.h:24