Ethereum  PoC-8
The C++ Implementation of Ethereum
Precompiled.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #pragma once
23 
24 #include <unordered_map>
25 #include <functional>
26 #include <libdevcore/CommonData.h>
27 #include <libdevcore/Exceptions.h>
28 
29 namespace dev
30 {
31 namespace eth
32 {
33 
34 using PrecompiledExecutor = std::function<std::pair<bool, bytes>(bytesConstRef _in)>;
35 using PrecompiledPricer = std::function<bigint(bytesConstRef _in)>;
36 
37 DEV_SIMPLE_EXCEPTION(ExecutorNotFound);
38 DEV_SIMPLE_EXCEPTION(PricerNotFound);
39 
41 {
42 public:
44  static PrecompiledExecutor const& executor(std::string const& _name);
45 
47  static PrecompiledPricer const& pricer(std::string const& _name);
48 
50  static PrecompiledExecutor registerExecutor(std::string const& _name, PrecompiledExecutor const& _exec) { return (get()->m_execs[_name] = _exec); }
52  static void unregisterExecutor(std::string const& _name) { get()->m_execs.erase(_name); }
53 
55  static PrecompiledPricer registerPricer(std::string const& _name, PrecompiledPricer const& _exec) { return (get()->m_pricers[_name] = _exec); }
57  static void unregisterPricer(std::string const& _name) { get()->m_pricers.erase(_name); }
58 
59 private:
60  static PrecompiledRegistrar* get() { if (!s_this) s_this = new PrecompiledRegistrar; return s_this; }
61 
62  std::unordered_map<std::string, PrecompiledExecutor> m_execs;
63  std::unordered_map<std::string, PrecompiledPricer> m_pricers;
64  static PrecompiledRegistrar* s_this;
65 };
66 
67 // TODO: unregister on unload with a static object.
68 #define ETH_REGISTER_PRECOMPILED(Name) static std::pair<bool, bytes> __eth_registerPrecompiledFunction ## Name(bytesConstRef _in); static PrecompiledExecutor __eth_registerPrecompiledFactory ## Name = ::dev::eth::PrecompiledRegistrar::registerExecutor(#Name, &__eth_registerPrecompiledFunction ## Name); static std::pair<bool, bytes> __eth_registerPrecompiledFunction ## Name
69 #define ETH_REGISTER_PRECOMPILED_PRICER(Name) static bigint __eth_registerPricerFunction ## Name(bytesConstRef _in); static PrecompiledPricer __eth_registerPricerFactory ## Name = ::dev::eth::PrecompiledRegistrar::registerPricer(#Name, &__eth_registerPricerFunction ## Name); static bigint __eth_registerPricerFunction ## Name
70 }
71 }
dev::vector_ref< byte const >
dev::eth::PrecompiledExecutor
std::function< std::pair< bool, bytes >(bytesConstRef _in)> PrecompiledExecutor
Definition: Precompiled.h:34
dev::eth::PrecompiledRegistrar::registerPricer
static PrecompiledPricer registerPricer(std::string const &_name, PrecompiledPricer const &_exec)
Register a pricer. In general just use ETH_REGISTER_PRECOMPILED_PRICER.
Definition: Precompiled.h:55
dev::eth::PrecompiledRegistrar::executor
static PrecompiledExecutor const & executor(std::string const &_name)
Get the executor object for _name function or.
Definition: Precompiled.cpp:35
dev::eth::PrecompiledRegistrar::unregisterPricer
static void unregisterPricer(std::string const &_name)
Unregister a pricer. Shouldn't generally be necessary.
Definition: Precompiled.h:57
dev::eth::PrecompiledRegistrar::registerExecutor
static PrecompiledExecutor registerExecutor(std::string const &_name, PrecompiledExecutor const &_exec)
Register an executor. In general just use ETH_REGISTER_PRECOMPILED.
Definition: Precompiled.h:50
dev::eth::PrecompiledRegistrar::unregisterExecutor
static void unregisterExecutor(std::string const &_name)
Unregister an executor. Shouldn't generally be necessary.
Definition: Precompiled.h:52
dev::eth::PrecompiledPricer
std::function< bigint(bytesConstRef _in)> PrecompiledPricer
Definition: Precompiled.h:35
Exceptions.h
CommonData.h
dev::eth::PrecompiledRegistrar
Definition: Precompiled.h:41
dev::bigint
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<> > bigint
Definition: Common.h:118
dev::eth::PrecompiledRegistrar::pricer
static PrecompiledPricer const & pricer(std::string const &_name)
Get the price calculator object for _name function or.
Definition: Precompiled.cpp:42
dev::eth::DEV_SIMPLE_EXCEPTION
DEV_SIMPLE_EXCEPTION(NoHashRecorded)
dev
Definition: Address.cpp:21