Twitch SDK (Internal)
coreutilities.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #pragma once
10 
11 #include <memory>
12 #include <algorithm>
13 
14 namespace ttv
15 {
16  // <T> in these is intended to be a bitfield.
17  template<typename T>
18  void AddFlag(T& x, T flag)
19  {
20  x = static_cast<T>(x | flag);
21  }
22 
23  template<typename T>
24  void RemoveFlag(T& x, T flag)
25  {
26  x = static_cast<T>(x & (~flag));
27  }
28 
29  template<typename T>
30  void SetFlag(T& x, T flag, bool value)
31  {
32  if (value)
33  {
34  ttv::AddFlag(x, flag);
35  }
36  else
37  {
38  ttv::RemoveFlag(x, flag);
39  }
40  }
41 
42  template<typename T, typename F>
43  bool HasFlag(T& x, F flag)
44  {
45  return static_cast<uint32_t>(static_cast<uint32_t>(x) & static_cast<uint32_t>(flag)) == static_cast<uint32_t>(flag);
46  }
47 
48  template<typename Base, typename Derived>
49  class Cloneable : public Base
50  {
51  virtual std::unique_ptr<Base> Clone() const override
52  {
53  return std::make_unique<Derived>(*static_cast<const Derived*>(this));
54  }
55  };
56 
57  template<typename FunctionType>
59  {
60  public:
61  using LookupId = uint64_t;
62 
63  public:
64  template<typename FunctionRef>
65  void Push(FunctionRef&& callback)
66  {
67  Push(std::forward<FunctionRef>(callback), 0);
68  }
69 
70  template<typename FunctionRef>
71  void Push(FunctionRef&& callback, LookupId lookupId)
72  {
73  if (callback != nullptr)
74  {
75  LookupEntry entry;
76  entry.callback = std::forward<FunctionRef>(callback);
77  entry.lookupId = lookupId;
78 
79  mLookups.push_back(std::move(entry));
80  }
81  }
82 
83  template<typename... Args>
84  void Flush(Args&&... args)
85  {
86  for (const auto& lookup : mLookups)
87  {
88  lookup.callback(std::forward<Args>(args)...);
89  }
90  mLookups.clear();
91  }
92 
93  FunctionType Erase(LookupId token)
94  {
95  auto iter = std::find_if(mLookups.begin(), mLookups.end(), [token](const LookupEntry& x)
96  {
97  return x.lookupId == token;
98  });
99 
100  if (iter != mLookups.end())
101  {
102  FunctionType callback = std::move(iter->callback);
103 
104  mLookups.erase(iter);
105  return std::move(callback);
106  }
107 
108  return nullptr;
109  }
110 
111  bool Empty() const
112  {
113  return mLookups.empty();
114  }
115 
116  private:
117  struct LookupEntry
118  {
119  FunctionType callback;
121  };
122 
123  std::vector<LookupEntry> mLookups;
124  };
125 
131  template<typename T> struct ClientServerValue
132  {
135 
137  {
138  }
139 
140  ClientServerValue(const T& val)
141  {
142  client = val;
143  server = val;
144  }
145 
146  operator T()
147  {
148  return client;
149  }
150 
151  void operator=(const T& val)
152  {
153  client = val;
154  server = val;
155  }
156 
157  bool operator==(const T& other)
158  {
159  return client == other;
160  }
161 
162  bool operator!=(const T& other)
163  {
164  return client != other;
165  }
166  };
167 
168  template <typename Arg>
169  struct MakeVoid
170  {
171  using Type = void;
172  };
173 
174  template <typename Arg>
175  using VoidType = typename MakeVoid<Arg>::Type;
176 }
void Push(FunctionRef &&callback)
Definition: coreutilities.h:65
void Push(FunctionRef &&callback, LookupId lookupId)
Definition: coreutilities.h:71
void SetFlag(T &x, T flag, bool value)
Definition: coreutilities.h:30
uint64_t LookupId
Definition: coreutilities.h:61
Definition: coreutilities.h:131
void operator=(const T &val)
Definition: coreutilities.h:151
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
bool HasFlag(T &x, F flag)
Definition: coreutilities.h:43
T server
Definition: coreutilities.h:134
ClientServerValue()
Definition: coreutilities.h:136
Definition: coreutilities.h:58
T client
Definition: coreutilities.h:133
virtual std::unique_ptr< Base > Clone() const override
Definition: coreutilities.h:51
Definition: coreutilities.h:169
bool Empty() const
Definition: coreutilities.h:111
Definition: coreutilities.h:49
void RemoveFlag(T &x, T flag)
Definition: coreutilities.h:24
void Type
Definition: coreutilities.h:171
void AddFlag(T &x, T flag)
Definition: coreutilities.h:18
std::vector< LookupEntry > mLookups
Definition: coreutilities.h:123
LookupId lookupId
Definition: coreutilities.h:120
bool operator==(const T &other)
Definition: coreutilities.h:157
Definition: coreutilities.h:117
typename MakeVoid< Arg >::Type VoidType
Definition: coreutilities.h:175
ClientServerValue(const T &val)
Definition: coreutilities.h:140
FunctionType callback
Definition: coreutilities.h:119
bool operator!=(const T &other)
Definition: coreutilities.h:162
FunctionType Erase(LookupId token)
Definition: coreutilities.h:93
void Flush(Args &&... args)
Definition: coreutilities.h:84