neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
i_custom_type.hpp
Go to the documentation of this file.
1// i_custom_type.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <string>
42
43namespace neolib
44{
46 {
47 public:
49 // exceptions
50 public:
51 struct no_instance : std::logic_error { no_instance() : std::logic_error("i_custom_type::no_instance") {} };
52 // construction/assignment
53 public:
54 virtual i_custom_type* clone() const = 0;
55 virtual i_custom_type& assign(const i_custom_type& aRhs) = 0;
57 {
58 assign(aRhs);
59 return *this;
60 }
61 // comparison
62 public:
63 virtual bool operator==(const i_custom_type&) const = 0;
64 virtual bool operator<(const i_custom_type&) const = 0;
65 std::strong_ordering operator<=>(const i_custom_type& that) const
66 {
67 if (*this == that)
68 return std::strong_ordering::equal;
69 else if (*this < that)
70 return std::strong_ordering::less;
71 else
72 return std::strong_ordering::greater;
73 }
74 // state
75 public:
76 bool has_instance() const { return instance_ptr() != nullptr; }
77 template <typename T>
78 const T& instance_as() const { if (!has_instance()) throw no_instance(); return *static_cast<const T*>(instance_ptr()); }
79 template <typename T>
80 T& instance_as() { if (!has_instance()) throw no_instance(); return *static_cast<T*>(instance_ptr()); }
81 virtual const void* instance_ptr() const = 0;
82 virtual void* instance_ptr() = 0;
83 // meta
84 public:
85 virtual void name(i_string& aName) const = 0;
86 virtual void to_string(i_string& aString) const = 0;
87 std::string name() const { string n; name(n); return n.to_std_string(); }
88 std::string to_string() const { string s; to_string(s); return s.to_std_string(); }
89 };
90
91 inline bool operator!=(const i_custom_type& lhs, const i_custom_type& rhs)
92 {
93 return !(lhs == rhs);
94 }
95}
virtual const void * instance_ptr() const =0
const T & instance_as() const
virtual i_custom_type * clone() const =0
std::string name() const
virtual void * instance_ptr()=0
i_custom_type & operator=(const i_custom_type &aRhs)
virtual i_custom_type & assign(const i_custom_type &aRhs)=0
virtual void to_string(i_string &aString) const =0
i_custom_type abstract_type
std::string to_string() const
virtual bool operator<(const i_custom_type &) const =0
virtual void name(i_string &aName) const =0
virtual bool operator==(const i_custom_type &) const =0
std::strong_ordering operator<=>(const i_custom_type &that) const
std::string to_std_string() const
Definition string.hpp:85