BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
object.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 #include <boost/multiprecision/integer.hpp>
27 #include <fc/io/raw.hpp>
28 #include <fc/crypto/city.hpp>
29 
30 #define MAX_NESTING (200)
31 
32 namespace graphene { namespace db {
61  class object
62  {
63  public:
64  object() = default;
65  object( uint8_t space_id, uint8_t type_id ) : id( space_id, type_id, 0 ) {}
66  virtual ~object() = default;
67 
68  // serialized
70 
73  virtual std::unique_ptr<object> clone()const = 0;
74  virtual void move_from( object& obj ) = 0;
75  virtual fc::variant to_variant()const = 0;
76  virtual std::vector<char> pack()const = 0;
78  };
79 
87  template<typename DerivedClass>
89  {
90  public:
91  using object::object; // constructors
92  std::unique_ptr<object> clone()const override
93  {
94  return std::make_unique<DerivedClass>( *static_cast<const DerivedClass*>(this) );
95  }
96 
97  void move_from( object& obj ) override
98  {
99  static_cast<DerivedClass&>(*this) = std::move( static_cast<DerivedClass&>(obj) );
100  }
101  fc::variant to_variant()const override
102  { return fc::variant( static_cast<const DerivedClass&>(*this), MAX_NESTING ); }
103  std::vector<char> pack()const override { return fc::raw::pack( static_cast<const DerivedClass&>(*this) ); }
104  };
105 
106  template<typename DerivedClass, uint8_t SpaceID, uint8_t TypeID>
107  class abstract_object : public base_abstract_object<DerivedClass>
108  {
109  public:
110  static constexpr uint8_t space_id = SpaceID;
111  static constexpr uint8_t type_id = TypeID;
114  };
115 
116  using annotation_map = fc::flat_map<uint8_t, object_id_type>;
117 
122  template<typename DerivedClass>
123  class annotated_object : public base_abstract_object<DerivedClass>
124  {
125  public:
127  object_id_type get_annotation( uint8_t annotation_id_space )const
128  {
129  auto itr = annotations.find(annotation_id_space);
130  if( itr != annotations.end() ) return itr->second;
131  return object_id_type();
132  }
134  {
135  annotations[id.space()] = id;
136  }
137 
143  };
144 
145 } } // graphene::db
146 
147 // Without this, pack(object_id) tries to match the template for
148 // pack(boost::multiprecision::uint128_t). No idea why. :-(
149 namespace boost { namespace multiprecision { namespace detail {
150 template<typename To>
151 struct is_restricted_conversion<graphene::db::object,To> : public mpl::true_ {};
152 }}}
153 
157  (annotations) )
graphene::db::object::id
object_id_type id
Definition: object.hpp:69
FC_REFLECT_DERIVED_TEMPLATE
#define FC_REFLECT_DERIVED_TEMPLATE(TEMPLATE_ARGS, TYPE, INHERITS, MEMBERS)
Definition: reflect.hpp:330
graphene::db::base_abstract_object
Use the Curiously Recurring Template Pattern to automatically add the ability to clone,...
Definition: object.hpp:88
FC_REFLECT_TYPENAME
FC_REFLECT_TYPENAME(fc::log_message)
graphene::db::object::pack
virtual std::vector< char > pack() const =0
graphene::db::abstract_object::type_id
static constexpr uint8_t type_id
Definition: object.hpp:111
graphene::db::object::move_from
virtual void move_from(object &obj)=0
graphene::db::annotated_object::annotations
annotation_map annotations
Definition: object.hpp:142
graphene::db::abstract_object
Definition: object.hpp:107
graphene::db::annotated_object::set_annotation
void set_annotation(object_id_type id)
Definition: object.hpp:133
graphene::db::object::to_variant
virtual fc::variant to_variant() const =0
graphene::db::base_abstract_object::move_from
void move_from(object &obj) override
Definition: object.hpp:97
graphene::db::object::clone
virtual std::unique_ptr< object > clone() const =0
graphene::db::object::object
object()=default
MAX_NESTING
#define MAX_NESTING
Definition: object.hpp:30
graphene::db::base_abstract_object::pack
std::vector< char > pack() const override
Definition: object.hpp:103
object_id.hpp
graphene::db::abstract_object::abstract_object
abstract_object()
Definition: object.hpp:112
graphene::db::annotated_object
An object that is easily extended by providing pointers to other objects, one for each space.
Definition: object.hpp:123
city.hpp
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
graphene::db::object_id_type
Definition: object_id.hpp:30
graphene::db::object_id
Definition: object_id.hpp:103
graphene::db::annotation_map
fc::flat_map< uint8_t, object_id_type > annotation_map
Definition: object.hpp:116
FC_REFLECT
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:388
graphene::db::abstract_object::space_id
static constexpr uint8_t space_id
Definition: object.hpp:110
graphene::db::abstract_object::get_id
object_id< SpaceID, TypeID > get_id() const
Definition: object.hpp:113
graphene::db::annotated_object::get_annotation
object_id_type get_annotation(uint8_t annotation_id_space) const
Definition: object.hpp:127
graphene::db::base_abstract_object::clone
std::unique_ptr< object > clone() const override
Definition: object.hpp:92
graphene
Definition: api.cpp:48
graphene::db::object::~object
virtual ~object()=default
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
graphene::db::base_abstract_object::to_variant
fc::variant to_variant() const override
Definition: object.hpp:101
graphene::db::object
base for all database objects
Definition: object.hpp:61
raw.hpp