Jump to content
  • Advertisement

BitMaster

Member
  • Content Count

    1829
  • Joined

  • Last visited

Community Reputation

8652 Excellent

About BitMaster

  • Rank
    Contributor

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. BitMaster

    Quick Opaque Pointer Example

    Well, the only thing that really bugged me was the missing alignment check. Mostly because if not for that, I would have used one of the types I used with a bad alignment myself. And then I thought: why not take the opportunity to add your own attempt to the discussion, things like that can always use an extra pair of eyes to look through it. ;)
  2. BitMaster

    Quick Opaque Pointer Example

    I'm not really happy with that. Granted, you static_assert the size but you should also really static_assert the alignment. Personally I also dislike doing that work in your concrete class. Right now, adding a constructor has a relatively high risk of forgetting to initialize the opaque pointer. I have lately been toying around with something like this: template <typename T, std::size_t Length, std::size_t Align = alignof(void*)> class opaque_data { public: /** * The underlying type. */ using type = T; /** * Initializes the underlying data. * @param args any arguments to be passed to the underlying type's * constructor. */ template <typename ... Args> opaque_data(Args&&... args) { static_assert(sizeof(type) <= Length, "not enough storage available"); static_assert(alignof(type) <= Align , "alignment requirements not met"); new (reinterpret_cast<type*>(&storage)) type{std::forward<Args>(args)...}; } /** * Initializes the underlying data using a subclass. * @param args any arguments to be passed to the subclass' constructor. */ template <typename S, typename ... Args> opaque_data(tag<S>, Args&&... args) { static_assert(sizeof(S) <= Length , "not enough storage available"); static_assert(alignof(S) <= Align , "alignment requirements not met"); static_assert(std::is_base_of<type, S>::value , "the specified type is not a subclass of the underlying type"); static_assert(std::has_virtual_destructor<type>::value, "inheritance is used and base underlying type has no virtual destructor"); new (reinterpret_cast<S*>(&storage)) S(std::forward<Args>(args)...); } /** * Destroys the underlying data. */ ~opaque_data() { reinterpret_cast<type*>(&storage)->~type(); } /** * Returns a reference to the underlying data. * @return a reference to the underlying data. */ type& data() { return *reinterpret_cast<type*>(&storage); } /** * Returns a reference to the underlying data. * @return a reference to the underlying data. */ type const& data() const { return *reinterpret_cast<type const*>(&storage); } /** * Swaps the underlying data with another instance. * @param other another instance. */ void swap(opaque_data& other) { using std::swap; swap(data(), other.data()); } /** * Allows access to the underlying type's data members. * @return a pointer to the underlying type. */ type* operator -> () { return reinterpret_cast<type*>(&storage); } /** * Allows access to the underlying type's data members. * @return a pointer to the underlying type. */ type const* operator -> () const { return reinterpret_cast<type const*>(&storage); } /** * Returns a reference to the underlying data. * @return a reference to the underlying data. */ type& operator * () { return data(); } /** * Returns a reference to the underlying data. * @return a reference to the underlying data. */ type const& operator * () const { return data(); } /// @name Deleted Constructors and Operators //@{ opaque_data(opaque_data const&) = delete; opaque_data(opaque_data&&) = delete; opaque_data& operator = (opaque_data const&) = delete; opaque_data& operator = (opaque_data&&) = delete; //@} /** * Swaps the underlying data of two instances. * @param lhs the first participant of the swap. * @param rhs the second participant of the swap. */ friend void swap(opaque_data& lhs, opaque_data& rhs) { lhs.swap(rhs); } private: /** * The storage for the underlying data. */ std::aligned_storage_t<Length, Align> storage; };
  3. BitMaster

    Java - Getting The Path Of The Jar File

    I strongly object to calling an URI "HTML format" or anything similar. The URI scheme is completely unrelated to HTML and used well beyond that domain.
  4. BitMaster

    C++: Custom memory allocation

    If this is implemented via actual polymorphism as shown so far, then ~Allocator must be declared virtual. Aside from that, a lot of the 'work' done in the allocator destructors seems useless. Setting values to 0/nullptr would be at best useful as a debugging help - but the MSVC debug runtime for example would already overwrite the memory immediately afterwards anyway.
  5. I'm not a guy.... >.<
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!