BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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 {
33 
62  class object
63  {
64  public:
65  object(){}
66  virtual ~object(){}
67 
68  static const uint8_t space_id = 0;
69  static const uint8_t type_id = 0;
70 
71 
72  // serialized
74 
76  virtual unique_ptr<object> clone()const = 0;
77  virtual void move_from( object& obj ) = 0;
78  virtual variant to_variant()const = 0;
79  virtual vector<char> pack()const = 0;
80  };
81 
89  template<typename DerivedClass>
90  class abstract_object : public object
91  {
92  public:
93  virtual unique_ptr<object> clone()const
94  {
95  return unique_ptr<object>(new DerivedClass( *static_cast<const DerivedClass*>(this) ));
96  }
97 
98  virtual void move_from( object& obj )
99  {
100  static_cast<DerivedClass&>(*this) = std::move( static_cast<DerivedClass&>(obj) );
101  }
102  virtual variant to_variant()const { return variant( static_cast<const DerivedClass&>(*this), MAX_NESTING ); }
103  virtual vector<char> pack()const { return fc::raw::pack( static_cast<const DerivedClass&>(*this) ); }
104  };
105 
106  typedef flat_map<uint8_t, object_id_type> annotation_map;
107 
112  template<typename DerivedClass>
113  class annotated_object : public abstract_object<DerivedClass>
114  {
115  public:
117  object_id_type get_annotation( uint8_t annotation_id_space )const
118  {
119  auto itr = annotations.find(annotation_id_space);
120  if( itr != annotations.end() ) return itr->second;
121  return object_id_type();
122  }
124  {
125  annotations[id.space()] = id;
126  }
127 
132  annotation_map annotations;
133  };
134 
135 } } // graphene::db
136 
137 // Without this, pack(object_id) tries to match the template for
138 // pack(boost::multiprecision::uint128_t). No idea why. :-(
139 namespace boost { namespace multiprecision { namespace detail {
140 template<typename To>
141 struct is_restricted_conversion<graphene::db::object,To> : public mpl::true_ {};
142 }}}
143 
virtual unique_ptr< object > clone() const =0
these methods are implemented for derived classes by inheriting abstract_object<DerivedClass> ...
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:386
Definition: api.cpp:56
virtual vector< char > pack() const
Definition: object.hpp:103
virtual vector< char > pack() const =0
annotation_map annotations
Definition: object.hpp:132
virtual void move_from(object &obj)
Definition: object.hpp:98
FC_REFLECT_TYPENAME(fc::log_message)
object_id_type id
Definition: object.hpp:73
virtual unique_ptr< object > clone() const
these methods are implemented for derived classes by inheriting abstract_object<DerivedClass> ...
Definition: object.hpp:93
void set_annotation(object_id_type id)
Definition: object.hpp:123
static const uint8_t type_id
Definition: object.hpp:69
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
Use the Curiously Recurring Template Pattern to automatically add the ability to clone, serialize, and move objects polymorphically.
Definition: object.hpp:90
object_id_type get_annotation(uint8_t annotation_id_space) const
Definition: object.hpp:117
virtual variant to_variant() const =0
static const uint8_t space_id
Definition: object.hpp:68
#define MAX_NESTING
Definition: object.hpp:30
An object that is easily extended by providing pointers to other objects, one for each space...
Definition: object.hpp:113
virtual void move_from(object &obj)=0
virtual ~object()
Definition: object.hpp:66
#define FC_REFLECT_DERIVED_TEMPLATE(TEMPLATE_ARGS, TYPE, INHERITS, MEMBERS)
Definition: reflect.hpp:329
base for all database objects
Definition: object.hpp:62
virtual variant to_variant() const
Definition: object.hpp:102
flat_map< uint8_t, object_id_type > annotation_map
Definition: object.hpp:106