mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-08-31 06:26:18 +00:00
New profile buttons started, not performing actions yet.
New system of Observers. Subscriptions on PeerData updates.
This commit is contained in:
@@ -44,7 +44,6 @@ uint64 _SharedMemoryLocation[4] = { 0x00, 0x01, 0x02, 0x03 };
|
||||
#include <openssl/rand.h>
|
||||
|
||||
// Base types compile-time check
|
||||
|
||||
static_assert(sizeof(char) == 1, "Basic types size check failed");
|
||||
static_assert(sizeof(uchar) == 1, "Basic types size check failed");
|
||||
static_assert(sizeof(int16) == 2, "Basic types size check failed");
|
||||
|
@@ -421,6 +421,24 @@ inline bool operator!=(std::nullptr_t a, const unique_ptr<T> &b) noexcept {
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
using _yes = char(&)[1];
|
||||
using _no = char(&)[2];
|
||||
|
||||
template <typename Base, typename Derived>
|
||||
struct _host {
|
||||
operator Base*() const;
|
||||
operator Derived*();
|
||||
};
|
||||
|
||||
template <typename Base, typename Derived>
|
||||
struct is_base_of {
|
||||
template <typename T>
|
||||
static _yes check(Derived*, T);
|
||||
static _no check(Base*, int);
|
||||
|
||||
static constexpr bool value = sizeof(check(_host<Base, Derived>(), int())) == sizeof(_yes);
|
||||
};
|
||||
|
||||
} // namespace std_
|
||||
|
||||
#include "logs.h"
|
||||
@@ -1182,18 +1200,22 @@ NullFunctionImplementation<R, Args...> NullFunctionImplementation<R, Args...>::S
|
||||
template <typename R, typename... Args>
|
||||
class Function {
|
||||
public:
|
||||
Function() : _implementation(&NullFunctionImplementation<R, Args...>::SharedInstance) {}
|
||||
Function() : _implementation(nullImpl()) {}
|
||||
Function(FunctionImplementation<R, Args...> *implementation) : _implementation(implementation) {}
|
||||
Function(const Function<R, Args...> &other) = delete;
|
||||
Function<R, Args...> &operator=(const Function<R, Args...> &other) = delete;
|
||||
Function(Function<R, Args...> &&other) : _implementation(other._implementation) {
|
||||
other._implementation = &NullFunctionImplementation<R, Args...>::SharedInstance;
|
||||
other._implementation = nullImpl();
|
||||
}
|
||||
Function<R, Args...> &operator=(Function<R, Args...> &&other) {
|
||||
std::swap(_implementation, other._implementation);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool isNull() const {
|
||||
return (_implementation == nullImpl());
|
||||
}
|
||||
|
||||
R call(Args... args) { return _implementation->call(args...); }
|
||||
~Function() {
|
||||
if (_implementation) {
|
||||
@@ -1204,6 +1226,10 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
static FunctionImplementation<R, Args...> *nullImpl() {
|
||||
return &NullFunctionImplementation<R, Args...>::SharedInstance;
|
||||
}
|
||||
|
||||
FunctionImplementation<R, Args...> *_implementation;
|
||||
|
||||
};
|
||||
|
62
Telegram/SourceFiles/core/observer.cpp
Normal file
62
Telegram/SourceFiles/core/observer.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
This file is part of Telegram Desktop,
|
||||
the official desktop version of Telegram messaging app, see https://telegram.org
|
||||
|
||||
Telegram Desktop is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
It is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
In addition, as a special exception, the copyright holders give permission
|
||||
to link the code of portions of this program with the OpenSSL library.
|
||||
|
||||
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
||||
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
||||
*/
|
||||
#include "stdafx.h"
|
||||
#include "core/observer.h"
|
||||
|
||||
namespace Notify {
|
||||
namespace {
|
||||
|
||||
UnregisterObserverCallback UnregisterCallbacks[256];
|
||||
|
||||
} // namespace
|
||||
|
||||
// Observer base interface.
|
||||
Observer::~Observer() {
|
||||
for_const (auto connection, _connections) {
|
||||
unregisterObserver(connection);
|
||||
}
|
||||
}
|
||||
|
||||
void Observer::observerRegistered(ConnectionId connection) {
|
||||
_connections.push_back(connection);
|
||||
}
|
||||
|
||||
void unregisterObserver(ConnectionId connection) {
|
||||
auto event = static_cast<ObservedEvent>(connection >> 24);
|
||||
auto connectionIndex = int(connection & 0x00FFFFFFU) - 1;
|
||||
if (connectionIndex >= 0 && UnregisterCallbacks[event]) {
|
||||
UnregisterCallbacks[event](connectionIndex);
|
||||
}
|
||||
}
|
||||
|
||||
UnregisterObserverCallbackCreator::UnregisterObserverCallbackCreator(ObservedEvent event, UnregisterObserverCallback callback) {
|
||||
UnregisterCallbacks[event] = callback;
|
||||
}
|
||||
|
||||
namespace internal {
|
||||
|
||||
void observerRegisteredDefault(Observer *observer, ConnectionId connection) {
|
||||
observer->observerRegistered(connection);
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
} // namespace Notify
|
134
Telegram/SourceFiles/core/observer.h
Normal file
134
Telegram/SourceFiles/core/observer.h
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
This file is part of Telegram Desktop,
|
||||
the official desktop version of Telegram messaging app, see https://telegram.org
|
||||
|
||||
Telegram Desktop is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
It is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
In addition, as a special exception, the copyright holders give permission
|
||||
to link the code of portions of this program with the OpenSSL library.
|
||||
|
||||
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
||||
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "core/vector_of_moveable.h"
|
||||
|
||||
namespace Notify {
|
||||
|
||||
class Observer;
|
||||
using ConnectionId = uint32;
|
||||
|
||||
// Each observer type should have observerRegistered(Notify::ConnectionId connection) method.
|
||||
// Usually it is done by deriving the type from the Notify::Observer base class.
|
||||
// In destructor it should call Notify::unregisterObserver(connection) for all the connections.
|
||||
|
||||
namespace internal {
|
||||
void observerRegisteredDefault(Observer *observer, ConnectionId connection);
|
||||
} // namespace internal
|
||||
|
||||
void unregisterObserver(ConnectionId connection);
|
||||
|
||||
class Observer {
|
||||
public:
|
||||
virtual ~Observer() = 0;
|
||||
|
||||
private:
|
||||
void observerRegistered(ConnectionId connection);
|
||||
friend void internal::observerRegisteredDefault(Observer *observer, ConnectionId connection);
|
||||
|
||||
QVector<ConnectionId> _connections;
|
||||
|
||||
};
|
||||
|
||||
using ObservedEvent = uchar;
|
||||
inline ConnectionId observerConnectionId(ObservedEvent event, int connectionIndex) {
|
||||
t_assert(connectionIndex >= 0 && connectionIndex < 0x01000000);
|
||||
return (static_cast<uint32>(event) << 24) | (connectionIndex + 1);
|
||||
}
|
||||
|
||||
using UnregisterObserverCallback = void(*)(int connectionIndex);
|
||||
|
||||
// Usage: UnregisterObserverCallbackCreator creator(myEvent, myCallback); in global scope.
|
||||
class UnregisterObserverCallbackCreator {
|
||||
public:
|
||||
UnregisterObserverCallbackCreator(ObservedEvent event, UnregisterObserverCallback callback);
|
||||
|
||||
};
|
||||
|
||||
// Handler is one of Function<> instantiations.
|
||||
template <typename Flags, typename Handler>
|
||||
struct ObserversList {
|
||||
struct Entry {
|
||||
Flags flags;
|
||||
Handler handler;
|
||||
};
|
||||
std_::vector_of_moveable<Entry> entries;
|
||||
QVector<int> freeIndices;
|
||||
};
|
||||
|
||||
template <typename Flags, typename Handler>
|
||||
int registerObserver(ObserversList<Flags, Handler> &list, Flags flags, Handler &&handler) {
|
||||
while (!list.freeIndices.isEmpty()) {
|
||||
auto freeIndex = list.freeIndices.back();
|
||||
list.freeIndices.pop_back();
|
||||
|
||||
if (freeIndex < list.entries.size()) {
|
||||
list.entries[freeIndex] = { flags, std_::move(handler) };
|
||||
return freeIndex;
|
||||
}
|
||||
}
|
||||
list.entries.push_back({ flags, std_::move(handler) });
|
||||
return list.entries.size() - 1;
|
||||
}
|
||||
|
||||
template <typename Flags, typename Handler>
|
||||
void unregisterObserver(ObserversList<Flags, Handler> &list, int connectionIndex) {
|
||||
auto &entries(list.entries);
|
||||
if (entries.size() <= connectionIndex) return;
|
||||
|
||||
if (entries.size() == connectionIndex + 1) {
|
||||
for (entries.pop_back(); !entries.isEmpty() && entries.back().handler.isNull();) {
|
||||
entries.pop_back();
|
||||
}
|
||||
} else {
|
||||
entries[connectionIndex].handler = Handler();
|
||||
list.freeIndices.push_back(connectionIndex);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Flags, typename Handler, typename... Args>
|
||||
void notifyObservers(ObserversList<Flags, Handler> &list, Flags flags, Args&&... args) {
|
||||
for (auto &entry : list.entries) {
|
||||
if (!entry.handler.isNull() && (flags & entry.flags)) {
|
||||
entry.handler.call(std_::forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace internal {
|
||||
|
||||
template <typename ObserverType, int>
|
||||
struct ObserverRegisteredGeneric {
|
||||
static void call(ObserverType *observer, ConnectionId connection) {
|
||||
observer->observerRegistered(connection);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename ObserverType>
|
||||
struct ObserverRegisteredGeneric<ObserverType, true> {
|
||||
static void call(ObserverType *observer, ConnectionId connection) {
|
||||
observerRegisteredDefault(observer, connection);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace Notify
|
153
Telegram/SourceFiles/core/vector_of_moveable.h
Normal file
153
Telegram/SourceFiles/core/vector_of_moveable.h
Normal file
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
This file is part of Telegram Desktop,
|
||||
the official desktop version of Telegram messaging app, see https://telegram.org
|
||||
|
||||
Telegram Desktop is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
It is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
In addition, as a special exception, the copyright holders give permission
|
||||
to link the code of portions of this program with the OpenSSL library.
|
||||
|
||||
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
||||
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
// some minimal implementation of std::vector() for moveable (but not copiable) types.
|
||||
namespace std_ {
|
||||
|
||||
template <typename T>
|
||||
class vector_of_moveable {
|
||||
typedef vector_of_moveable<T> Self;
|
||||
int _size = 0, _capacity = 0;
|
||||
void *_plaindata = nullptr;
|
||||
|
||||
public:
|
||||
inline T *data() {
|
||||
return reinterpret_cast<T*>(_plaindata);
|
||||
}
|
||||
inline const T *data() const {
|
||||
return reinterpret_cast<const T*>(_plaindata);
|
||||
}
|
||||
|
||||
inline bool operator==(const Self &other) const {
|
||||
if (this == &other) return true;
|
||||
if (_size != other._size) return false;
|
||||
for (int i = 0; i < _size; ++i) {
|
||||
if (data()[i] != other.data()[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
inline bool operator!=(const Self &other) const { return !(*this == other); }
|
||||
inline int size() const { return _size; }
|
||||
inline bool isEmpty() const { return _size == 0; }
|
||||
inline void clear() {
|
||||
for (int i = 0; i < _size; ++i) {
|
||||
data()[i].~T();
|
||||
}
|
||||
_size = 0;
|
||||
|
||||
operator delete[](_plaindata);
|
||||
_plaindata = nullptr;
|
||||
_capacity = 0;
|
||||
}
|
||||
|
||||
typedef T *iterator;
|
||||
typedef const T *const_iterator;
|
||||
|
||||
// STL style
|
||||
inline iterator begin() { return data(); }
|
||||
inline const_iterator begin() const { return data(); }
|
||||
inline const_iterator cbegin() const { return data(); }
|
||||
inline iterator end() { return data() + _size; }
|
||||
inline const_iterator end() const { return data() + _size; }
|
||||
inline const_iterator cend() const { return data() + _size; }
|
||||
inline iterator erase(iterator it) {
|
||||
T tmp = std_::move(*it);
|
||||
for (auto next = it + 1, e = end(); next != e; ++next) {
|
||||
auto prev = next - 1;
|
||||
*prev = std_::move(*next);
|
||||
}
|
||||
--_size;
|
||||
return it;
|
||||
}
|
||||
|
||||
inline iterator insert(const_iterator pos, T &&value) {
|
||||
int insertAtIndex = pos - begin();
|
||||
if (_size + 1 > _capacity) {
|
||||
reallocate(_capacity + (_capacity > 1 ? _capacity / 2 : 1));
|
||||
}
|
||||
auto insertAt = begin() + insertAtIndex, e = end();
|
||||
if (insertAt == e) {
|
||||
new (&(*insertAt)) T(std_::move(value));
|
||||
} else {
|
||||
auto prev = e - 1;
|
||||
new (&(*e)) T(std_::move(*prev));
|
||||
for (auto it = prev; it != insertAt; --it) {
|
||||
*it = std_::move(*--prev);
|
||||
}
|
||||
*insertAt = std_::move(value);
|
||||
}
|
||||
++_size;
|
||||
return insertAt;
|
||||
}
|
||||
inline void push_back(T &&value) {
|
||||
insert(end(), std_::forward<T>(value));
|
||||
}
|
||||
inline void pop_back() {
|
||||
erase(end() - 1);
|
||||
}
|
||||
inline T &front() {
|
||||
return *begin();
|
||||
}
|
||||
inline const T &front() const {
|
||||
return *begin();
|
||||
}
|
||||
inline T &back() {
|
||||
return *(end() - 1);
|
||||
}
|
||||
inline const T &back() const {
|
||||
return *(end() - 1);
|
||||
}
|
||||
inline bool empty() const { return _size == 0; }
|
||||
|
||||
inline T &operator[](int index) {
|
||||
return data()[index];
|
||||
}
|
||||
inline const T &operator[](int index) const {
|
||||
return data()[index];
|
||||
}
|
||||
inline const T &at(int index) const {
|
||||
if (index < 0 || index >= _size) {
|
||||
throw std::out_of_range("");
|
||||
}
|
||||
return data()[index];
|
||||
}
|
||||
|
||||
inline ~vector_of_moveable() {
|
||||
clear();
|
||||
}
|
||||
|
||||
private:
|
||||
void reallocate(int newCapacity) {
|
||||
auto newPlainData = operator new[](newCapacity * sizeof(T));
|
||||
for (int i = 0; i < _size; ++i) {
|
||||
*(reinterpret_cast<T*>(newPlainData) + i) = std_::move(*(data() + i));
|
||||
}
|
||||
std::swap(_plaindata, newPlainData);
|
||||
_capacity = newCapacity;
|
||||
operator delete[](newPlainData);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
} // namespace std_
|
Reference in New Issue
Block a user