From: Kefu Chai Date: Wed, 28 Feb 2018 01:21:41 +0000 (+0800) Subject: Merge pull request #20273 from adamemerson/wip-any-singleton-will-do X-Git-Tag: v13.0.2~151 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=430c3ca78bfbd430c992bb4f652886645be9bd98;p=ceph.git Merge pull request #20273 from adamemerson/wip-any-singleton-will-do common: Switch singletons to use immobile_any and cleanups Reviewed-by: Jesse Williamson Reviewed-by: Kefu Chai --- 430c3ca78bfbd430c992bb4f652886645be9bd98 diff --cc src/common/ceph_context.h index 72d912205695,1bdef28866cf..b06f6e067e04 --- a/src/common/ceph_context.h +++ b/src/common/ceph_context.h @@@ -126,23 -136,31 +136,31 @@@ public /** * process an admin socket command */ - void do_command(std::string command, cmdmap_t& cmdmap, std::string format, - ceph::bufferlist *out); + void do_command(std::string_view command, const cmdmap_t& cmdmap, + std::string_view format, ceph::bufferlist *out); - template - void lookup_or_create_singleton_object(T*& p, const std::string &name) { - std::lock_guard lg(_associated_objs_lock); - - if (!_associated_objs.count(name)) { - p = new T(this); - _associated_objs[name] = new TypedSingletonWrapper(p); - } else { - TypedSingletonWrapper *wrapper = - dynamic_cast *>(_associated_objs[name]); - assert(wrapper != NULL); - p = wrapper->singleton; + static constexpr std::size_t largest_singleton = sizeof(void*) * 72; + + template + T& lookup_or_create_singleton_object(std::string_view name, + Args&&... args) { + static_assert(sizeof(T) <= largest_singleton, + "Please increase largest singleton."); + std::lock_guard lg(associated_objs_lock); + std::type_index type = typeid(T); + + auto i = associated_objs.find(std::make_pair(name, type)); + if (i == associated_objs.cend()) { + i = associated_objs.emplace_hint( + i, + std::piecewise_construct, + std::forward_as_tuple(name, type), + std::forward_as_tuple(std::in_place_type, + std::forward(args)...)); } + return ceph::any_cast(i->second); } + /** * get a crypto handler */