Ocular Engine
StringComposer.hpp
1 
17 #pragma once
18 #ifndef __H__OCULAR_UTILS_STRING_COMPOSER__H__
19 #define __H__OCULAR_UTILS_STRING_COMPOSER__H__
20 
21 #include <string>
22 #include <sstream>
23 #include <mutex>
24 
25 //------------------------------------------------------------------------------------------
26 
31 namespace Ocular
32 {
37  namespace Utils
38  {
59  {
60  public:
61 
63  : m_IncompleteString(), m_Mutex()
64  {
65 
66  }
67 
68  ~StringComposer() { }
69 
70  template<typename T, typename... U>
71  std::string compose(T first, U... args)
72  {
73  std::string result;
74 
75  m_Mutex.lock();
76 
77  m_IncompleteString.str(std::string());
78  m_IncompleteString << first;
79 
80  internalCompose(args...);
81  result = m_IncompleteString.str();
82 
83  m_Mutex.unlock();
84 
85  return result;
86  }
87 
88  protected:
89 
90  private:
91 
92  template<typename T, typename... U>
93  void internalCompose(T first, U... last)
94  {
95  m_IncompleteString << first;
96  internalCompose(last...);
97  }
98 
99  static void internalCompose()
100  {
101  // Empty, but required as it is called when
102  // there are no more arguments.
103  }
104 
105  std::stringstream m_IncompleteString;
106  std::mutex m_Mutex;
107  };
108  }
112 }
117 #define OCULAR_STRING_COMPOSER Ocular::Utils::StringComposer().compose
118 
119 //------------------------------------------------------------------------------------------
120 
121 #endif
Definition: StringComposer.hpp:58
Note: Once this library is made dynamic, this will no longer be needed.
Definition: Common.hpp:70