/* ----------------------------------------------------------------------------- Copyright 2019-2021 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] #ifndef TYPES_HPP_ #define TYPES_HPP_ #include #include #include #include #include #include "logging.hpp" #include "pooled.hpp" #define ALWAYS_PERMANENT(type) \ type(const type&) = delete; \ type(type&&) = delete; \ type& operator =(const type&) = delete; \ type& operator =(type&&) = delete; using CollectionType = int; using PrimInt = std::int64_t; using PrimString = std::string; using PrimChar = char; using PrimCharBuffer = std::string; using PrimFloat = double; template inline PrimString PrimString_FromLiteral(const char(&literal)[S]) { return PrimString(literal, literal + (S - 1)); } template using R = std::unique_ptr; template inline R R_get(T* val) { return R(val); } template using S = std::shared_ptr; template inline S S_get(T* val) { return S(val); } template using W = std::weak_ptr; template inline W W_get(T* val) { return W(val); } template using T = std::tuple; template T T_get(Ts... ts) { return std::make_tuple(ts...); } template using L = std::vector; template inline L L_get(Ts... ts) { return L{ts...}; } template class LazyInit { public: LazyInit(const std::function& create) : initialized_(false), pending_(false), value_(), create_(create) {} const T& Get() { InitValue(); return value_; } LazyInit& operator = (const T& value) { InitValue(); value_ = value; return *this; } private: LazyInit(const LazyInit&) = delete; LazyInit(LazyInit&&) = delete; LazyInit& operator = (const LazyInit&) = delete; LazyInit& operator = (LazyInit&&) = delete; void InitValue() { if (pending_) { FAIL() << "Cycle in lazy initialization"; } if (!initialized_) { pending_ = true; value_ = create_(); pending_ = false; initialized_ = true; } } bool initialized_, pending_; T value_; const std::function create_; }; class TypeCategory; class TypeInstance; class TypeValue; template struct Params { using Type = typename Params&, Ts...>::Type; }; template struct Params<0, Ts...> { using Type = T; }; template struct ParamsKey { using Type = typename ParamsKey::Type; }; template struct ParamsKey<0, Ts...> { using Type = T; }; template struct KeyFromParams { static typename ParamsKey::Type Get(const typename Params::Type& from, Ts... args) { return KeyFromParams::Get(from, args..., std::get(from).get()); } }; template struct KeyFromParams { static typename ParamsKey::Type Get(const typename Params::Type&, Ts... args) { return typename ParamsKey::Type(args...); } }; template typename ParamsKey::Type GetKeyFromParams(const typename Params::Type& from) { return KeyFromParams::Get(from); } class ValueTuple { public: virtual int Size() const = 0; virtual const S& At(int pos) const = 0; virtual const S& Only() const = 0; protected: ValueTuple() = default; virtual ~ValueTuple() = default; private: void* operator new(std::size_t size) = delete; }; template<> class PoolManager> { using Managed = S; using PoolEntry = PoolStorage; template friend class PoolArray; static PoolEntry* Take(int size); static void Return(PoolEntry* storage); static constexpr unsigned int pool_limit_ = 256; }; class ReturnTuple : public ValueTuple { public: ReturnTuple(int size) : size_(size), data_(size_) {} template explicit ReturnTuple(Ts... returns) : size_(sizeof...(Ts)), data_(size_) { data_.Init(std::move(returns)...); } ReturnTuple(ReturnTuple&&) = default; ReturnTuple& operator = (ReturnTuple&&); int Size() const final; S& At(int pos); const S& At(int pos) const final; const S& Only() const final; private: ReturnTuple(const ReturnTuple&) = delete; ReturnTuple& operator =(const ReturnTuple&) = delete; void* operator new(std::size_t size) = delete; int size_; PoolArray> data_; }; template<> class PoolManager*> { using Managed = const S*; using PoolEntry = PoolStorage; template friend class PoolArray; static PoolEntry* Take(int size); static void Return(PoolEntry* storage); static constexpr unsigned int pool_limit_ = 256; }; class ArgTuple : public ValueTuple { public: template explicit ArgTuple(const Ts&... args) : size_(sizeof...(Ts)), data_(size_) { data_.Init(&args...); } int Size() const final; const S& At(int pos) const final; const S& Only() const final; private: ArgTuple(const ArgTuple&) = delete; ArgTuple(ArgTuple&&) = delete; ArgTuple& operator = (const ArgTuple&) = delete; ArgTuple& operator = (ArgTuple&&) = delete; void* operator new(std::size_t size) = delete; int size_; PoolArray*> data_; }; template<> class PoolManager> { using Managed = S; using PoolEntry = PoolStorage; template friend class PoolArray; static PoolEntry* Take(int size); static void Return(PoolEntry* storage); static constexpr unsigned int pool_limit_ = 256; }; class ParamTuple { public: template explicit ParamTuple(const Ts&... params) : size_(sizeof...(Ts)), data_(size_) { data_.Init(std::move(params)...); } ParamTuple(ParamTuple&& other) = default; int Size() const; const S& At(int pos) const; private: ParamTuple(const ParamTuple&) = delete; ParamTuple& operator = (const ParamTuple&) = delete; ParamTuple& operator = (ParamTuple&&) = delete; void* operator new(std::size_t size) = delete; int size_; PoolArray> data_; }; inline ReturnTuple FailWhenNull(ReturnTuple values) { for (int i = 0; i < values.Size(); ++i) { if (values.At(i) == nullptr) { FAIL() << "Value at return tuple position " << i << " is null"; } } return values; } inline const ValueTuple& FailWhenNull(const ValueTuple& values) { for (int i = 0; i < values.Size(); ++i) { if (values.At(i) == nullptr) { FAIL() << "Value at arg tuple position " << i << " is null"; } } return values; } #endif // TYPES_HPP_