1 // Copyright (c) 2011 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 #ifndef CHROME_TEST_AUTOMATION_VALUE_CONVERSION_TRAITS_H_ 6 #define CHROME_TEST_AUTOMATION_VALUE_CONVERSION_TRAITS_H_ 7 8 #include <string> 9 10 namespace base { 11 class DictionaryValue; 12 class ListValue; 13 class Value; 14 } 15 16 // ValueConversionTraits contains functions for converting between a base::Value 17 // and a particular type. See particular function descriptions below. 18 template <typename T> 19 struct ValueConversionTraits { 20 // Create a value from |t|. Will not return NULL. 21 // static base::Value* CreateValueFrom(const T& t); 22 23 // Attempts to set |t| from the given value. Returns true on success. 24 // |t| will not be modified unless successful. 25 // static bool SetFromValue(const base::Value* value, T* t); 26 27 // Returns whether |value| can be converted to type |T|. 28 // If true, |SetFromValue| will succeed. 29 // static bool CanConvert(const base::Value* value); 30 }; 31 32 template <> 33 struct ValueConversionTraits<int> { 34 static base::Value* CreateValueFrom(int t); 35 static bool SetFromValue(const base::Value* value, int* t); 36 static bool CanConvert(const base::Value* value); 37 }; 38 39 template <> 40 struct ValueConversionTraits<bool> { 41 static base::Value* CreateValueFrom(bool t); 42 static bool SetFromValue(const base::Value* value, bool* t); 43 static bool CanConvert(const base::Value* value); 44 }; 45 46 template <> 47 struct ValueConversionTraits<std::string> { 48 static base::Value* CreateValueFrom(const std::string& t); 49 static bool SetFromValue(const base::Value* value, std::string* t); 50 static bool CanConvert(const base::Value* value); 51 }; 52 53 // The conversion will involve deep copying, not just casting. 54 template <> 55 struct ValueConversionTraits<base::Value*> { 56 static base::Value* CreateValueFrom(const base::Value* t); 57 static bool SetFromValue(const base::Value* value, base::Value** t); 58 static bool CanConvert(const base::Value* value); 59 }; 60 61 // The conversion will involve deep copying, not just casting. 62 template <> 63 struct ValueConversionTraits<base::ListValue*> { 64 static base::Value* CreateValueFrom(const base::ListValue* t); 65 static bool SetFromValue(const base::Value* value, base::ListValue** t); 66 static bool CanConvert(const base::Value* value); 67 }; 68 69 // The conversion will involve deep copying, not just casting. 70 template <> 71 struct ValueConversionTraits<base::DictionaryValue*> { 72 static base::Value* CreateValueFrom(const base::DictionaryValue* t); 73 static bool SetFromValue(const base::Value* value, base::DictionaryValue** t); 74 static bool CanConvert(const base::Value* value); 75 }; 76 77 #endif // CHROME_TEST_AUTOMATION_VALUE_CONVERSION_TRAITS_H_ 78