OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/base/class_property.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <utility> |
| 9 |
| 10 namespace ui { |
| 11 |
| 12 PropertyHandler::PropertyHandler() {} |
| 13 |
| 14 PropertyHandler::~PropertyHandler() {} |
| 15 |
| 16 int64_t PropertyHandler::SetPropertyInternal(const void* key, |
| 17 const char* name, |
| 18 PropertyDeallocator deallocator, |
| 19 int64_t value, |
| 20 int64_t default_value) { |
| 21 // This code may be called before |port_| has been created. |
| 22 std::unique_ptr<PropertyData> data = BeforePropertyChange(key); |
| 23 int64_t old = GetPropertyInternal(key, default_value); |
| 24 if (value == default_value) { |
| 25 prop_map_.erase(key); |
| 26 } else { |
| 27 Value prop_value; |
| 28 prop_value.name = name; |
| 29 prop_value.value = value; |
| 30 prop_value.deallocator = deallocator; |
| 31 prop_map_[key] = prop_value; |
| 32 } |
| 33 AfterPropertyChange(key, old, std::move(data)); |
| 34 return old; |
| 35 } |
| 36 |
| 37 std::unique_ptr<PropertyData> PropertyHandler::BeforePropertyChange( |
| 38 const void* key) { |
| 39 return nullptr; |
| 40 } |
| 41 |
| 42 void PropertyHandler::ClearProperties() { |
| 43 // Clear properties. |
| 44 for (std::map<const void*, Value>::const_iterator iter = prop_map_.begin(); |
| 45 iter != prop_map_.end(); |
| 46 ++iter) { |
| 47 if (iter->second.deallocator) |
| 48 (*iter->second.deallocator)(iter->second.value); |
| 49 } |
| 50 prop_map_.clear(); |
| 51 } |
| 52 |
| 53 int64_t PropertyHandler::GetPropertyInternal(const void* key, |
| 54 int64_t default_value) const { |
| 55 std::map<const void*, Value>::const_iterator iter = prop_map_.find(key); |
| 56 if (iter == prop_map_.end()) |
| 57 return default_value; |
| 58 return iter->second.value; |
| 59 } |
| 60 |
| 61 std::set<const void*> PropertyHandler::GetAllPropertKeys() const { |
| 62 std::set<const void*> keys; |
| 63 for (auto& pair : prop_map_) |
| 64 keys.insert(pair.first); |
| 65 return keys; |
| 66 } |
| 67 |
| 68 } // namespace ui |
OLD | NEW |