From d1c555fede4d0b18822926a5013bf192f4be112f Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Tue, 15 Nov 2016 08:42:33 -0800 Subject: [PATCH] Autogenerate enum defenitions for all languages Summary: Keeping enums in sync between all the different bindings is tedious and error prone. Getting one of the values in the incorrect order could lead to many hours of debugging as they are passed as ints to C. This diff adds a simple python script to generate these enums for all languages. This also makes it much easier to add support for more languages in the future Reviewed By: gkassabli Differential Revision: D4174263 fbshipit-source-id: 478961a8f683e196704d3c6ea1505a05c85fcb10 --- CSSLayout/CSSEnums.h | 106 ++++++++++ CSSLayout/CSSLayout.c | 5 + CSSLayout/CSSLayout.h | 91 +-------- csharp/Facebook.CSSLayout/CSSAlign.cs | 4 +- csharp/Facebook.CSSLayout/CSSDIrection.cs | 10 +- csharp/Facebook.CSSLayout/CSSDimension.cs | 4 +- csharp/Facebook.CSSLayout/CSSEdge.cs | 3 +- csharp/Facebook.CSSLayout/CSSFlexDirection.cs | 2 +- csharp/Facebook.CSSLayout/CSSJustify.cs | 2 +- csharp/Facebook.CSSLayout/CSSLogLevel.cs | 2 +- csharp/Facebook.CSSLayout/CSSMeasureMode.cs | 3 +- csharp/Facebook.CSSLayout/CSSOverflow.cs | 3 +- csharp/Facebook.CSSLayout/CSSPositionType.cs | 4 +- csharp/Facebook.CSSLayout/CSSPrintOptions.cs | 2 +- csharp/Facebook.CSSLayout/CSSWrap.cs | 2 +- enums.py | 182 ++++++++++++++++++ java/com/facebook/csslayout/CSSAlign.java | 31 ++- java/com/facebook/csslayout/CSSDimension.java | 33 ++++ java/com/facebook/csslayout/CSSDirection.java | 25 ++- java/com/facebook/csslayout/CSSEdge.java | 47 +++++ .../csslayout/CSSExperimentalFeature.java | 16 ++ .../facebook/csslayout/CSSFlexDirection.java | 28 ++- java/com/facebook/csslayout/CSSJustify.java | 31 ++- java/com/facebook/csslayout/CSSLogLevel.java | 39 ++++ java/com/facebook/csslayout/CSSLogger.java | 10 +- .../facebook/csslayout/CSSMeasureMode.java | 25 ++- java/com/facebook/csslayout/CSSNode.java | 22 +-- java/com/facebook/csslayout/CSSOverflow.java | 25 ++- .../facebook/csslayout/CSSPositionType.java | 22 ++- .../facebook/csslayout/CSSPrintOptions.java | 35 ++++ java/com/facebook/csslayout/CSSStyle.java | 2 +- java/com/facebook/csslayout/CSSWrap.java | 22 ++- java/jni/CSSJNI.cpp | 11 +- .../com/facebook/csslayout/CSSNodeTest.java | 16 +- 34 files changed, 699 insertions(+), 166 deletions(-) create mode 100644 CSSLayout/CSSEnums.h create mode 100644 enums.py create mode 100644 java/com/facebook/csslayout/CSSDimension.java create mode 100644 java/com/facebook/csslayout/CSSEdge.java create mode 100644 java/com/facebook/csslayout/CSSLogLevel.java create mode 100644 java/com/facebook/csslayout/CSSPrintOptions.java diff --git a/CSSLayout/CSSEnums.h b/CSSLayout/CSSEnums.h new file mode 100644 index 00000000..8f9979f5 --- /dev/null +++ b/CSSLayout/CSSEnums.h @@ -0,0 +1,106 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +typedef enum CSSOverflow { + CSSOverflowVisible, + CSSOverflowHidden, + CSSOverflowScroll, + CSSOverflowCount, +} CSSOverflow; + +typedef enum CSSJustify { + CSSJustifyFlexStart, + CSSJustifyCenter, + CSSJustifyFlexEnd, + CSSJustifySpaceBetween, + CSSJustifySpaceAround, + CSSJustifyCount, +} CSSJustify; + +typedef enum CSSFlexDirection { + CSSFlexDirectionColumn, + CSSFlexDirectionColumnReverse, + CSSFlexDirectionRow, + CSSFlexDirectionRowReverse, + CSSFlexDirectionCount, +} CSSFlexDirection; + +typedef enum CSSAlign { + CSSAlignAuto, + CSSAlignFlexStart, + CSSAlignCenter, + CSSAlignFlexEnd, + CSSAlignStretch, + CSSAlignCount, +} CSSAlign; + +typedef enum CSSEdge { + CSSEdgeLeft, + CSSEdgeTop, + CSSEdgeRight, + CSSEdgeBottom, + CSSEdgeStart, + CSSEdgeEnd, + CSSEdgeHorizontal, + CSSEdgeVertical, + CSSEdgeAll, + CSSEdgeCount, +} CSSEdge; + +typedef enum CSSWrap { + CSSWrapNoWrap, + CSSWrapWrap, + CSSWrapCount, +} CSSWrap; + +typedef enum CSSDirection { + CSSDirectionInherit, + CSSDirectionLTR, + CSSDirectionRTL, + CSSDirectionCount, +} CSSDirection; + +typedef enum CSSExperimentalFeature { + CSSExperimentalFeatureCount, +} CSSExperimentalFeature; + +typedef enum CSSLogLevel { + CSSLogLevelError, + CSSLogLevelWarn, + CSSLogLevelInfo, + CSSLogLevelDebug, + CSSLogLevelVerbose, + CSSLogLevelCount, +} CSSLogLevel; + +typedef enum CSSDimension { + CSSDimensionWidth, + CSSDimensionHeight, + CSSDimensionCount, +} CSSDimension; + +typedef enum CSSMeasureMode { + CSSMeasureModeUndefined, + CSSMeasureModeExactly, + CSSMeasureModeAtMost, + CSSMeasureModeCount, +} CSSMeasureMode; + +typedef enum CSSPositionType { + CSSPositionTypeRelative, + CSSPositionTypeAbsolute, + CSSPositionTypeCount, +} CSSPositionType; + +typedef enum CSSPrintOptions { + CSSPrintOptionsLayout = 1, + CSSPrintOptionsStyle = 2, + CSSPrintOptionsChildren = 4, + CSSPrintOptionsCount, +} CSSPrintOptions; diff --git a/CSSLayout/CSSLayout.c b/CSSLayout/CSSLayout.c index 8ba14c0a..dacaceec 100644 --- a/CSSLayout/CSSLayout.c +++ b/CSSLayout/CSSLayout.c @@ -122,6 +122,8 @@ static int _csslayoutAndroidLog(CSSLogLevel level, const char *format, va_list a case CSSLogLevelVerbose: androidLevel = ANDROID_LOG_VERBOSE; break; + case CSSLogLevelCount: + break; } const int result = __android_log_vprint(androidLevel, "css-layout", format, args); return result; @@ -1824,6 +1826,7 @@ static void layoutNodeImpl(const CSSNodeRef node, leadingMainDim = betweenMainDim / 2; break; case CSSJustifyFlexStart: + case CSSJustifyCount: break; } @@ -2015,6 +2018,7 @@ static void layoutNodeImpl(const CSSNodeRef node, break; case CSSAlignAuto: case CSSAlignFlexStart: + case CSSAlignCount: break; } @@ -2074,6 +2078,7 @@ static void layoutNodeImpl(const CSSNodeRef node, break; } case CSSAlignAuto: + case CSSAlignCount: break; } } diff --git a/CSSLayout/CSSLayout.h b/CSSLayout/CSSLayout.h index 7fdde1a8..391a458f 100644 --- a/CSSLayout/CSSLayout.h +++ b/CSSLayout/CSSLayout.h @@ -29,104 +29,15 @@ static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff}; #define CSSUndefined NAN #include "CSSMacros.h" +#include "CSSEnums.h" CSS_EXTERN_C_BEGIN -typedef enum CSSDirection { - CSSDirectionInherit, - CSSDirectionLTR, - CSSDirectionRTL, -} CSSDirection; - -typedef enum CSSFlexDirection { - CSSFlexDirectionColumn, - CSSFlexDirectionColumnReverse, - CSSFlexDirectionRow, - CSSFlexDirectionRowReverse, -} CSSFlexDirection; - -typedef enum CSSJustify { - CSSJustifyFlexStart, - CSSJustifyCenter, - CSSJustifyFlexEnd, - CSSJustifySpaceBetween, - CSSJustifySpaceAround, -} CSSJustify; - -typedef enum CSSOverflow { - CSSOverflowVisible, - CSSOverflowHidden, - CSSOverflowScroll, -} CSSOverflow; - -// Note: auto is only a valid value for alignSelf. It is NOT a valid value for -// alignItems. -typedef enum CSSAlign { - CSSAlignAuto, - CSSAlignFlexStart, - CSSAlignCenter, - CSSAlignFlexEnd, - CSSAlignStretch, -} CSSAlign; - -typedef enum CSSPositionType { - CSSPositionTypeRelative, - CSSPositionTypeAbsolute, -} CSSPositionType; - -typedef enum CSSWrap { - CSSWrapNoWrap, - CSSWrapWrap, -} CSSWrap; - -typedef enum CSSMeasureMode { - CSSMeasureModeUndefined, - CSSMeasureModeExactly, - CSSMeasureModeAtMost, - CSSMeasureModeCount, -} CSSMeasureMode; - -typedef enum CSSDimension { - CSSDimensionWidth, - CSSDimensionHeight, -} CSSDimension; - -typedef enum CSSEdge { - CSSEdgeLeft, - CSSEdgeTop, - CSSEdgeRight, - CSSEdgeBottom, - CSSEdgeStart, - CSSEdgeEnd, - CSSEdgeHorizontal, - CSSEdgeVertical, - CSSEdgeAll, - CSSEdgeCount, -} CSSEdge; - -typedef enum CSSPrintOptions { - CSSPrintOptionsLayout = 1, - CSSPrintOptionsStyle = 2, - CSSPrintOptionsChildren = 4, -} CSSPrintOptions; - typedef struct CSSSize { float width; float height; } CSSSize; -typedef enum CSSLogLevel { - CSSLogLevelError, - CSSLogLevelWarn, - CSSLogLevelInfo, - CSSLogLevelDebug, - CSSLogLevelVerbose, -} CSSLogLevel; - -typedef enum CSSExperimentalFeature { - CSSExperimentalFeatureCount, -} CSSExperimentalFeature; - typedef struct CSSNode *CSSNodeRef; typedef CSSSize (*CSSMeasureFunc)(CSSNodeRef node, float width, diff --git a/csharp/Facebook.CSSLayout/CSSAlign.cs b/csharp/Facebook.CSSLayout/CSSAlign.cs index 685fea75..94cbad3b 100644 --- a/csharp/Facebook.CSSLayout/CSSAlign.cs +++ b/csharp/Facebook.CSSLayout/CSSAlign.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * @@ -15,6 +15,6 @@ namespace Facebook.CSSLayout FlexStart, Center, FlexEnd, - Stretch + Stretch, } } diff --git a/csharp/Facebook.CSSLayout/CSSDIrection.cs b/csharp/Facebook.CSSLayout/CSSDIrection.cs index 1a455709..f350e288 100644 --- a/csharp/Facebook.CSSLayout/CSSDIrection.cs +++ b/csharp/Facebook.CSSLayout/CSSDIrection.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * @@ -12,7 +12,11 @@ namespace Facebook.CSSLayout public enum CSSDirection { Inherit, - LeftToRight, - RightToLeft + LTR, + RTL, + [System.Obsolete("Use LTR instead")] + LeftToRight = LTR, + [System.Obsolete("Use RTL instead")] + RightToLeft = RTL, } } diff --git a/csharp/Facebook.CSSLayout/CSSDimension.cs b/csharp/Facebook.CSSLayout/CSSDimension.cs index a8fd2b26..9ae6512f 100644 --- a/csharp/Facebook.CSSLayout/CSSDimension.cs +++ b/csharp/Facebook.CSSLayout/CSSDimension.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * @@ -12,6 +12,6 @@ namespace Facebook.CSSLayout public enum CSSDimension { Width, - Height + Height, } } diff --git a/csharp/Facebook.CSSLayout/CSSEdge.cs b/csharp/Facebook.CSSLayout/CSSEdge.cs index f4877f87..b639eb9e 100644 --- a/csharp/Facebook.CSSLayout/CSSEdge.cs +++ b/csharp/Facebook.CSSLayout/CSSEdge.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * @@ -20,6 +20,5 @@ namespace Facebook.CSSLayout Horizontal, Vertical, All, - Count, } } diff --git a/csharp/Facebook.CSSLayout/CSSFlexDirection.cs b/csharp/Facebook.CSSLayout/CSSFlexDirection.cs index a06f7533..24c82392 100644 --- a/csharp/Facebook.CSSLayout/CSSFlexDirection.cs +++ b/csharp/Facebook.CSSLayout/CSSFlexDirection.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * diff --git a/csharp/Facebook.CSSLayout/CSSJustify.cs b/csharp/Facebook.CSSLayout/CSSJustify.cs index 93e1566f..5f97f31b 100644 --- a/csharp/Facebook.CSSLayout/CSSJustify.cs +++ b/csharp/Facebook.CSSLayout/CSSJustify.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * diff --git a/csharp/Facebook.CSSLayout/CSSLogLevel.cs b/csharp/Facebook.CSSLayout/CSSLogLevel.cs index a0316b8d..53a5cbe8 100644 --- a/csharp/Facebook.CSSLayout/CSSLogLevel.cs +++ b/csharp/Facebook.CSSLayout/CSSLogLevel.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * diff --git a/csharp/Facebook.CSSLayout/CSSMeasureMode.cs b/csharp/Facebook.CSSLayout/CSSMeasureMode.cs index ff82545c..4b9cdd9c 100644 --- a/csharp/Facebook.CSSLayout/CSSMeasureMode.cs +++ b/csharp/Facebook.CSSLayout/CSSMeasureMode.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * @@ -14,6 +14,5 @@ namespace Facebook.CSSLayout Undefined, Exactly, AtMost, - Count, } } diff --git a/csharp/Facebook.CSSLayout/CSSOverflow.cs b/csharp/Facebook.CSSLayout/CSSOverflow.cs index df1492ec..7207668d 100644 --- a/csharp/Facebook.CSSLayout/CSSOverflow.cs +++ b/csharp/Facebook.CSSLayout/CSSOverflow.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * @@ -13,5 +13,6 @@ namespace Facebook.CSSLayout { Visible, Hidden, + Scroll, } } diff --git a/csharp/Facebook.CSSLayout/CSSPositionType.cs b/csharp/Facebook.CSSLayout/CSSPositionType.cs index ac12484d..11b9ebde 100644 --- a/csharp/Facebook.CSSLayout/CSSPositionType.cs +++ b/csharp/Facebook.CSSLayout/CSSPositionType.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * @@ -12,6 +12,6 @@ namespace Facebook.CSSLayout public enum CSSPositionType { Relative, - Absolute + Absolute, } } diff --git a/csharp/Facebook.CSSLayout/CSSPrintOptions.cs b/csharp/Facebook.CSSLayout/CSSPrintOptions.cs index 8d608f4e..1df7d381 100644 --- a/csharp/Facebook.CSSLayout/CSSPrintOptions.cs +++ b/csharp/Facebook.CSSLayout/CSSPrintOptions.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * diff --git a/csharp/Facebook.CSSLayout/CSSWrap.cs b/csharp/Facebook.CSSLayout/CSSWrap.cs index 15425b28..34d2e4c2 100644 --- a/csharp/Facebook.CSSLayout/CSSWrap.cs +++ b/csharp/Facebook.CSSLayout/CSSWrap.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright (c) 2014-present, Facebook, Inc. * All rights reserved. * diff --git a/enums.py b/enums.py new file mode 100644 index 00000000..40f36d5a --- /dev/null +++ b/enums.py @@ -0,0 +1,182 @@ +# Copyright (c) 2014-present, Facebook, Inc. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. An additional grant +# of patent rights can be found in the PATENTS file in the same directory. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals +import os + +ENUMS = { + 'CSSDirection': [ + 'Inherit', + 'LTR', + 'RTL', + ], + 'CSSFlexDirection': [ + 'Column', + 'ColumnReverse', + 'Row', + 'RowReverse', + ], + 'CSSJustify': [ + 'FlexStart', + 'Center', + 'FlexEnd', + 'SpaceBetween', + 'SpaceAround', + ], + 'CSSOverflow': [ + 'Visible', + 'Hidden', + 'Scroll', + ], + 'CSSAlign': [ + 'Auto', + 'FlexStart', + 'Center', + 'FlexEnd', + 'Stretch', + ], + 'CSSPositionType': [ + 'Relative', + 'Absolute', + ], + 'CSSWrap': [ + 'NoWrap', + 'Wrap', + ], + 'CSSMeasureMode': [ + 'Undefined', + 'Exactly', + 'AtMost', + ], + 'CSSDimension': [ + 'Width', + 'Height', + ], + 'CSSEdge': [ + 'Left', + 'Top', + 'Right', + 'Bottom', + 'Start', + 'End', + 'Horizontal', + 'Vertical', + 'All', + ], + 'CSSLogLevel': [ + 'Error', + 'Warn', + 'Info', + 'Debug', + 'Verbose', + ], + 'CSSExperimentalFeature': [], + 'CSSPrintOptions': [ + ('Layout', 1), + ('Style', 2), + ('Children', 4), + ], +} + +LICENSE = """/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +""" + +def to_java_upper(symbol): + symbol = str(symbol) + out = '' + for i in range(0, len(symbol)): + c = symbol[i] + if str.istitle(c) and i is not 0 and not str.istitle(symbol[i - 1]): + out += '_' + out += c.upper() + return out + + +root = os.path.dirname(__file__) + +# write out C header +with open(root + '/CSSLayout/CSSEnums.h', 'w') as f: + f.write(LICENSE) + remaining = len(ENUMS) + for name, values in ENUMS.items(): + f.write('typedef enum %s {\n' % name) + for value in values: + if isinstance(value, tuple): + f.write(' %s%s = %d,\n' % (name, value[0], value[1])) + else: + f.write(' %s%s,\n' % (name, value)) + f.write(' %sCount,\n' % name) + f.write('} %s;\n' % name) + if remaining > 1: + f.write('\n') + remaining = remaining - 1 + +# write out java files +for name, values in ENUMS.items(): + with open(root + '/java/com/facebook/csslayout/%s.java' % name, 'w') as f: + f.write(LICENSE) + f.write('package com.facebook.csslayout;\n\n') + f.write('public enum %s {\n' % name) + if len(values) > 0: + for value in values: + if isinstance(value, tuple): + f.write(' %s(%d)' % (to_java_upper(value[0]), value[1])) + else: + f.write(' %s(%d)' % (to_java_upper(value), values.index(value))) + if values.index(value) is len(values) - 1: + f.write(';\n') + else: + f.write(',\n') + else: + f.write('__EMPTY(-1);') + f.write('\n') + f.write(' private int mIntValue;\n') + f.write('\n') + f.write(' %s(int intValue) {\n' % name) + f.write(' mIntValue = intValue;\n') + f.write(' }\n') + f.write('\n') + f.write(' public int intValue() {\n') + f.write(' return mIntValue;\n') + f.write(' }\n') + f.write('\n') + f.write(' public static %s fromInt(int value) {\n' % name) + f.write(' switch (value) {\n') + for value in values: + if isinstance(value, tuple): + f.write(' case %d: return %s;\n' % (value[1], to_java_upper(value[0]))) + else: + f.write(' case %d: return %s;\n' % (values.index(value), to_java_upper(value))) + f.write(' default: throw new IllegalArgumentException("Unkown enum value: " + value);\n') + f.write(' }\n') + f.write(' }\n') + f.write('}\n') + +# write out csharp files +for name, values in ENUMS.items(): + with open(root + '/csharp/Facebook.CSSLayout/%s.cs' % name, 'w') as f: + f.write(LICENSE) + f.write('namespace Facebook.CSSLayout\n{\n') + f.write(' public enum %s\n {\n' % name) + for value in values: + if isinstance(value, tuple): + f.write(' %s = %d,\n' % (value[0], value[1])) + else: + f.write(' %s,\n' % value) + f.write(' }\n') + f.write('}\n') diff --git a/java/com/facebook/csslayout/CSSAlign.java b/java/com/facebook/csslayout/CSSAlign.java index 3e3cb0e9..2dedee0b 100644 --- a/java/com/facebook/csslayout/CSSAlign.java +++ b/java/com/facebook/csslayout/CSSAlign.java @@ -10,9 +10,30 @@ package com.facebook.csslayout; public enum CSSAlign { - AUTO, - FLEX_START, - CENTER, - FLEX_END, - STRETCH, + AUTO(0), + FLEX_START(1), + CENTER(2), + FLEX_END(3), + STRETCH(4); + + private int mIntValue; + + CSSAlign(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSAlign fromInt(int value) { + switch (value) { + case 0: return AUTO; + case 1: return FLEX_START; + case 2: return CENTER; + case 3: return FLEX_END; + case 4: return STRETCH; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } } diff --git a/java/com/facebook/csslayout/CSSDimension.java b/java/com/facebook/csslayout/CSSDimension.java new file mode 100644 index 00000000..e3fde021 --- /dev/null +++ b/java/com/facebook/csslayout/CSSDimension.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.csslayout; + +public enum CSSDimension { + WIDTH(0), + HEIGHT(1); + + private int mIntValue; + + CSSDimension(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSDimension fromInt(int value) { + switch (value) { + case 0: return WIDTH; + case 1: return HEIGHT; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } +} diff --git a/java/com/facebook/csslayout/CSSDirection.java b/java/com/facebook/csslayout/CSSDirection.java index 0c27a929..c0e84f1a 100644 --- a/java/com/facebook/csslayout/CSSDirection.java +++ b/java/com/facebook/csslayout/CSSDirection.java @@ -10,7 +10,26 @@ package com.facebook.csslayout; public enum CSSDirection { - INHERIT, - LTR, - RTL, + INHERIT(0), + LTR(1), + RTL(2); + + private int mIntValue; + + CSSDirection(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSDirection fromInt(int value) { + switch (value) { + case 0: return INHERIT; + case 1: return LTR; + case 2: return RTL; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } } diff --git a/java/com/facebook/csslayout/CSSEdge.java b/java/com/facebook/csslayout/CSSEdge.java new file mode 100644 index 00000000..50d184e3 --- /dev/null +++ b/java/com/facebook/csslayout/CSSEdge.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.csslayout; + +public enum CSSEdge { + LEFT(0), + TOP(1), + RIGHT(2), + BOTTOM(3), + START(4), + END(5), + HORIZONTAL(6), + VERTICAL(7), + ALL(8); + + private int mIntValue; + + CSSEdge(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSEdge fromInt(int value) { + switch (value) { + case 0: return LEFT; + case 1: return TOP; + case 2: return RIGHT; + case 3: return BOTTOM; + case 4: return START; + case 5: return END; + case 6: return HORIZONTAL; + case 7: return VERTICAL; + case 8: return ALL; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } +} diff --git a/java/com/facebook/csslayout/CSSExperimentalFeature.java b/java/com/facebook/csslayout/CSSExperimentalFeature.java index a92d522d..e0b269f9 100644 --- a/java/com/facebook/csslayout/CSSExperimentalFeature.java +++ b/java/com/facebook/csslayout/CSSExperimentalFeature.java @@ -10,4 +10,20 @@ package com.facebook.csslayout; public enum CSSExperimentalFeature { +__EMPTY(-1); + private int mIntValue; + + CSSExperimentalFeature(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSExperimentalFeature fromInt(int value) { + switch (value) { + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } } diff --git a/java/com/facebook/csslayout/CSSFlexDirection.java b/java/com/facebook/csslayout/CSSFlexDirection.java index 30f92bd0..10e5cd6a 100644 --- a/java/com/facebook/csslayout/CSSFlexDirection.java +++ b/java/com/facebook/csslayout/CSSFlexDirection.java @@ -10,8 +10,28 @@ package com.facebook.csslayout; public enum CSSFlexDirection { - COLUMN, - COLUMN_REVERSE, - ROW, - ROW_REVERSE + COLUMN(0), + COLUMN_REVERSE(1), + ROW(2), + ROW_REVERSE(3); + + private int mIntValue; + + CSSFlexDirection(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSFlexDirection fromInt(int value) { + switch (value) { + case 0: return COLUMN; + case 1: return COLUMN_REVERSE; + case 2: return ROW; + case 3: return ROW_REVERSE; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } } diff --git a/java/com/facebook/csslayout/CSSJustify.java b/java/com/facebook/csslayout/CSSJustify.java index dfcc1cd2..2d36a85f 100644 --- a/java/com/facebook/csslayout/CSSJustify.java +++ b/java/com/facebook/csslayout/CSSJustify.java @@ -10,9 +10,30 @@ package com.facebook.csslayout; public enum CSSJustify { - FLEX_START, - CENTER, - FLEX_END, - SPACE_BETWEEN, - SPACE_AROUND, + FLEX_START(0), + CENTER(1), + FLEX_END(2), + SPACE_BETWEEN(3), + SPACE_AROUND(4); + + private int mIntValue; + + CSSJustify(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSJustify fromInt(int value) { + switch (value) { + case 0: return FLEX_START; + case 1: return CENTER; + case 2: return FLEX_END; + case 3: return SPACE_BETWEEN; + case 4: return SPACE_AROUND; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } } diff --git a/java/com/facebook/csslayout/CSSLogLevel.java b/java/com/facebook/csslayout/CSSLogLevel.java new file mode 100644 index 00000000..94551bc0 --- /dev/null +++ b/java/com/facebook/csslayout/CSSLogLevel.java @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.csslayout; + +public enum CSSLogLevel { + ERROR(0), + WARN(1), + INFO(2), + DEBUG(3), + VERBOSE(4); + + private int mIntValue; + + CSSLogLevel(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSLogLevel fromInt(int value) { + switch (value) { + case 0: return ERROR; + case 1: return WARN; + case 2: return INFO; + case 3: return DEBUG; + case 4: return VERBOSE; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } +} diff --git a/java/com/facebook/csslayout/CSSLogger.java b/java/com/facebook/csslayout/CSSLogger.java index 44536c46..f1f28fe2 100644 --- a/java/com/facebook/csslayout/CSSLogger.java +++ b/java/com/facebook/csslayout/CSSLogger.java @@ -13,15 +13,9 @@ import com.facebook.proguard.annotations.DoNotStrip; /** * Inteface for recieving logs from native layer. Use by setting CSSNode.setLogger(myLogger); - * LOG_LEVEL_ERROR indicated a fatal error. + * See CSSLogLevel for the different log levels. */ public interface CSSLogger { - public final int LOG_LEVEL_ERROR = 0; - public final int LOG_LEVEL_WARN = 1; - public final int LOG_LEVEL_INFO = 2; - public final int LOG_LEVEL_DEBUG = 3; - public final int LOG_LEVEL_VERBOSE = 4; - @DoNotStrip - void log(int level, String message); + void log(CSSLogLevel level, String message); } diff --git a/java/com/facebook/csslayout/CSSMeasureMode.java b/java/com/facebook/csslayout/CSSMeasureMode.java index e8ebb34f..d2c69e6f 100644 --- a/java/com/facebook/csslayout/CSSMeasureMode.java +++ b/java/com/facebook/csslayout/CSSMeasureMode.java @@ -10,7 +10,26 @@ package com.facebook.csslayout; public enum CSSMeasureMode { - UNDEFINED, - EXACTLY, - AT_MOST, + UNDEFINED(0), + EXACTLY(1), + AT_MOST(2); + + private int mIntValue; + + CSSMeasureMode(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSMeasureMode fromInt(int value) { + switch (value) { + case 0: return UNDEFINED; + case 1: return EXACTLY; + case 2: return AT_MOST; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } } diff --git a/java/com/facebook/csslayout/CSSNode.java b/java/com/facebook/csslayout/CSSNode.java index 6c3419bc..e92f9267 100644 --- a/java/com/facebook/csslayout/CSSNode.java +++ b/java/com/facebook/csslayout/CSSNode.java @@ -46,12 +46,12 @@ public class CSSNode implements CSSNodeAPI { public static void setExperimentalFeatureEnabled( CSSExperimentalFeature feature, boolean enabled) { - jni_CSSLayoutSetExperimentalFeatureEnabled(feature.ordinal(), enabled); + jni_CSSLayoutSetExperimentalFeatureEnabled(feature.intValue(), enabled); } private static native boolean jni_CSSLayoutIsExperimentalFeatureEnabled(int feature); public static boolean isExperimentalFeatureEnabled(CSSExperimentalFeature feature) { - return jni_CSSLayoutIsExperimentalFeatureEnabled(feature.ordinal()); + return jni_CSSLayoutIsExperimentalFeatureEnabled(feature.intValue()); } private CSSNode mParent; @@ -199,7 +199,7 @@ public class CSSNode implements CSSNodeAPI { private native void jni_CSSNodeStyleSetDirection(long nativePointer, int direction); @Override public void setDirection(CSSDirection direction) { - jni_CSSNodeStyleSetDirection(mNativePointer, direction.ordinal()); + jni_CSSNodeStyleSetDirection(mNativePointer, direction.intValue()); } private native int jni_CSSNodeStyleGetFlexDirection(long nativePointer); @@ -211,7 +211,7 @@ public class CSSNode implements CSSNodeAPI { private native void jni_CSSNodeStyleSetFlexDirection(long nativePointer, int flexDirection); @Override public void setFlexDirection(CSSFlexDirection flexDirection) { - jni_CSSNodeStyleSetFlexDirection(mNativePointer, flexDirection.ordinal()); + jni_CSSNodeStyleSetFlexDirection(mNativePointer, flexDirection.intValue()); } private native int jni_CSSNodeStyleGetJustifyContent(long nativePointer); @@ -223,7 +223,7 @@ public class CSSNode implements CSSNodeAPI { private native void jni_CSSNodeStyleSetJustifyContent(long nativePointer, int justifyContent); @Override public void setJustifyContent(CSSJustify justifyContent) { - jni_CSSNodeStyleSetJustifyContent(mNativePointer, justifyContent.ordinal()); + jni_CSSNodeStyleSetJustifyContent(mNativePointer, justifyContent.intValue()); } private native int jni_CSSNodeStyleGetAlignItems(long nativePointer); @@ -235,7 +235,7 @@ public class CSSNode implements CSSNodeAPI { private native void jni_CSSNodeStyleSetAlignItems(long nativePointer, int alignItems); @Override public void setAlignItems(CSSAlign alignItems) { - jni_CSSNodeStyleSetAlignItems(mNativePointer, alignItems.ordinal()); + jni_CSSNodeStyleSetAlignItems(mNativePointer, alignItems.intValue()); } private native int jni_CSSNodeStyleGetAlignSelf(long nativePointer); @@ -247,7 +247,7 @@ public class CSSNode implements CSSNodeAPI { private native void jni_CSSNodeStyleSetAlignSelf(long nativePointer, int alignSelf); @Override public void setAlignSelf(CSSAlign alignSelf) { - jni_CSSNodeStyleSetAlignSelf(mNativePointer, alignSelf.ordinal()); + jni_CSSNodeStyleSetAlignSelf(mNativePointer, alignSelf.intValue()); } private native int jni_CSSNodeStyleGetAlignContent(long nativePointer); @@ -259,7 +259,7 @@ public class CSSNode implements CSSNodeAPI { private native void jni_CSSNodeStyleSetAlignContent(long nativePointer, int alignContent); @Override public void setAlignContent(CSSAlign alignContent) { - jni_CSSNodeStyleSetAlignContent(mNativePointer, alignContent.ordinal()); + jni_CSSNodeStyleSetAlignContent(mNativePointer, alignContent.intValue()); } private native int jni_CSSNodeStyleGetPositionType(long nativePointer); @@ -271,13 +271,13 @@ public class CSSNode implements CSSNodeAPI { private native void jni_CSSNodeStyleSetPositionType(long nativePointer, int positionType); @Override public void setPositionType(CSSPositionType positionType) { - jni_CSSNodeStyleSetPositionType(mNativePointer, positionType.ordinal()); + jni_CSSNodeStyleSetPositionType(mNativePointer, positionType.intValue()); } private native void jni_CSSNodeStyleSetFlexWrap(long nativePointer, int wrapType); @Override public void setWrap(CSSWrap flexWrap) { - jni_CSSNodeStyleSetFlexWrap(mNativePointer, flexWrap.ordinal()); + jni_CSSNodeStyleSetFlexWrap(mNativePointer, flexWrap.intValue()); } private native int jni_CSSNodeStyleGetOverflow(long nativePointer); @@ -289,7 +289,7 @@ public class CSSNode implements CSSNodeAPI { private native void jni_CSSNodeStyleSetOverflow(long nativePointer, int overflow); @Override public void setOverflow(CSSOverflow overflow) { - jni_CSSNodeStyleSetOverflow(mNativePointer, overflow.ordinal()); + jni_CSSNodeStyleSetOverflow(mNativePointer, overflow.intValue()); } private native void jni_CSSNodeStyleSetFlex(long nativePointer, float flex); diff --git a/java/com/facebook/csslayout/CSSOverflow.java b/java/com/facebook/csslayout/CSSOverflow.java index 9aae8225..8ba708ae 100644 --- a/java/com/facebook/csslayout/CSSOverflow.java +++ b/java/com/facebook/csslayout/CSSOverflow.java @@ -10,7 +10,26 @@ package com.facebook.csslayout; public enum CSSOverflow { - VISIBLE, - HIDDEN, - SCROLL, + VISIBLE(0), + HIDDEN(1), + SCROLL(2); + + private int mIntValue; + + CSSOverflow(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSOverflow fromInt(int value) { + switch (value) { + case 0: return VISIBLE; + case 1: return HIDDEN; + case 2: return SCROLL; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } } diff --git a/java/com/facebook/csslayout/CSSPositionType.java b/java/com/facebook/csslayout/CSSPositionType.java index 19e27dd4..4dcaa0df 100644 --- a/java/com/facebook/csslayout/CSSPositionType.java +++ b/java/com/facebook/csslayout/CSSPositionType.java @@ -10,6 +10,24 @@ package com.facebook.csslayout; public enum CSSPositionType { - RELATIVE, - ABSOLUTE, + RELATIVE(0), + ABSOLUTE(1); + + private int mIntValue; + + CSSPositionType(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSPositionType fromInt(int value) { + switch (value) { + case 0: return RELATIVE; + case 1: return ABSOLUTE; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } } diff --git a/java/com/facebook/csslayout/CSSPrintOptions.java b/java/com/facebook/csslayout/CSSPrintOptions.java new file mode 100644 index 00000000..ec83ab47 --- /dev/null +++ b/java/com/facebook/csslayout/CSSPrintOptions.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.csslayout; + +public enum CSSPrintOptions { + LAYOUT(1), + STYLE(2), + CHILDREN(4); + + private int mIntValue; + + CSSPrintOptions(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSPrintOptions fromInt(int value) { + switch (value) { + case 1: return LAYOUT; + case 2: return STYLE; + case 4: return CHILDREN; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } +} diff --git a/java/com/facebook/csslayout/CSSStyle.java b/java/com/facebook/csslayout/CSSStyle.java index c17ce527..916e54e2 100644 --- a/java/com/facebook/csslayout/CSSStyle.java +++ b/java/com/facebook/csslayout/CSSStyle.java @@ -54,7 +54,7 @@ public class CSSStyle { alignItems = CSSAlign.STRETCH; alignSelf = CSSAlign.AUTO; positionType = CSSPositionType.RELATIVE; - flexWrap = CSSWrap.NOWRAP; + flexWrap = CSSWrap.NO_WRAP; overflow = CSSOverflow.VISIBLE; flexGrow = 0; flexShrink = 0; diff --git a/java/com/facebook/csslayout/CSSWrap.java b/java/com/facebook/csslayout/CSSWrap.java index 895bb5d5..69a59056 100644 --- a/java/com/facebook/csslayout/CSSWrap.java +++ b/java/com/facebook/csslayout/CSSWrap.java @@ -10,6 +10,24 @@ package com.facebook.csslayout; public enum CSSWrap { - NOWRAP, - WRAP, + NO_WRAP(0), + WRAP(1); + + private int mIntValue; + + CSSWrap(int intValue) { + mIntValue = intValue; + } + + public int intValue() { + return mIntValue; + } + + public static CSSWrap fromInt(int value) { + switch (value) { + case 0: return NO_WRAP; + case 1: return WRAP; + default: throw new IllegalArgumentException("Unkown enum value: " + value); + } + } } diff --git a/java/jni/CSSJNI.cpp b/java/jni/CSSJNI.cpp index 65a994c1..f3bad806 100644 --- a/java/jni/CSSJNI.cpp +++ b/java/jni/CSSJNI.cpp @@ -68,14 +68,21 @@ static CSSSize _jniMeasureFunc(CSSNodeRef node, return CSSSize{measuredWidth, measuredHeight}; } +struct JCSSLogLevel : public JavaClass { + static constexpr auto kJavaDescriptor = "Lcom/facebook/csslayout/CSSLogLevel;"; +}; + static global_ref *jLogger; static int _jniLog(CSSLogLevel level, const char *format, va_list args) { char buffer[256]; int result = vsnprintf(buffer, sizeof(buffer), format, args); static auto logFunc = - findClassLocal("com/facebook/csslayout/CSSLogger")->getMethod("log"); - logFunc(jLogger->get(), static_cast(level), Environment::current()->NewStringUTF(buffer)); + findClassLocal("com/facebook/csslayout/CSSLogger")->getMethod, jstring)>("log"); + + static auto logLevelFromInt = JCSSLogLevel::javaClassStatic()->getStaticMethod("fromInt"); + + logFunc(jLogger->get(), logLevelFromInt(JCSSLogLevel::javaClassStatic(), static_cast(level)), Environment::current()->NewStringUTF(buffer)); return result; } diff --git a/java/tests/com/facebook/csslayout/CSSNodeTest.java b/java/tests/com/facebook/csslayout/CSSNodeTest.java index 5f2d3ae2..3b857279 100644 --- a/java/tests/com/facebook/csslayout/CSSNodeTest.java +++ b/java/tests/com/facebook/csslayout/CSSNodeTest.java @@ -40,35 +40,35 @@ public class CSSNodeTest { assertEquals(100, (int) node.getLayoutHeight()); } - private int mLogLevel; + private CSSLogLevel mLogLevel; private String mLogMessage; @Test public void testLogger() { CSSNode.setLogger(new CSSLogger() { - public void log(int level, String message) { + public void log(CSSLogLevel level, String message) { mLogLevel = level; mLogMessage = message; } }); - CSSNode.jni_CSSLog(CSSLogger.LOG_LEVEL_DEBUG, "Hello"); - assertEquals(CSSLogger.LOG_LEVEL_DEBUG, mLogLevel); + CSSNode.jni_CSSLog(CSSLogLevel.DEBUG.intValue(), "Hello"); + assertEquals(CSSLogLevel.DEBUG, mLogLevel); assertEquals("Hello", mLogMessage); } @Test public void testUpdateLogger() { CSSNode.setLogger(new CSSLogger() { - public void log(int level, String message) {} + public void log(CSSLogLevel level, String message) {} }); CSSNode.setLogger(new CSSLogger() { - public void log(int level, String message) { + public void log(CSSLogLevel level, String message) { mLogLevel = level; mLogMessage = message; } }); - CSSNode.jni_CSSLog(CSSLogger.LOG_LEVEL_VERBOSE, "Flexbox"); - assertEquals(CSSLogger.LOG_LEVEL_VERBOSE, mLogLevel); + CSSNode.jni_CSSLog(CSSLogLevel.VERBOSE.intValue(), "Flexbox"); + assertEquals(CSSLogLevel.VERBOSE, mLogLevel); assertEquals("Flexbox", mLogMessage); } }